-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-webpack-config.js
160 lines (153 loc) · 4.9 KB
/
make-webpack-config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');//打包css为一个文件
const HtmlWebpackPlugin = require('html-webpack-plugin'); // Html文件处理
const rucksack = require('rucksack-css');
const autoprefixer = require('autoprefixer');
const ROOT_PATH = path.resolve(__dirname)
const SRC_PATH = path.join(ROOT_PATH, 'src')
const DIST_PATH = path.join(ROOT_PATH, 'dist')
const TEM_PATH = path.join(ROOT_PATH, 'templates')
const webserverPort = 3001
module.exports = function (options) {
const entry = {
main: [path.join(SRC_PATH, 'index')],
vendor: ['react'],
};
const theme = {};
const module = {
loaders: [
{
test: /\.js$/,
loaders: ['react-hot', 'babel'],
include: SRC_PATH,
},
{
test: /\.css$/,
exclude: /node_modules/,
loader: ExtractTextPlugin.extract('style-loader',
'css?sourceMap&-restructuring&modules&localIdentName=[local]___[hash:base64:5]!'
+ 'postcss'),
},
{
test: /\.css$/, //组件中的CSS如果使用modules模式,可能导致无法找到对应的css名称
loader: ExtractTextPlugin.extract('style-loader', 'css-loader'),
include: /node_modules/,
},
{
test: /\.json$/,
loaders: ['json'],
include: SRC_PATH,
},
// {
// test: /\.less$/,
// loader: ExtractTextPlugin.extract(
// 'css?sourceMap&modules&localIdentName=[local]___[hash:base64:5]!!' +
// 'postcss!' +
// `less-loader?{"sourceMap":true,"modifyVars":${JSON.stringify(theme)}}`
// ),
// },
{ test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&minetype=application/font-woff' },
{ test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&minetype=application/font-woff' },
{ test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&minetype=application/octet-stream' },
{ test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file' },
{ test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&minetype=image/svg+xml' },
{ test: /\.(png|jpg|jpeg|gif)(\?v=\d+\.\d+\.\d+)?$/i, loader: 'url?limit=10000&name=img/[name].[ext]' },
],
}
const additionalLoaders = [
];
const alias = {
};
const aliasLoader = {
};
const externals = [
];
const modulesDirectories = ['node_modules', 'components', 'src', 'containers'];
const extensions = ['', '.js', '.jsx'];
const publicPath = options.devServer ?
'http://127.0.0.1:' + webserverPort + '/dist/' :
'';
const output = {
path: DIST_PATH,
publicPath,
filename: '[name].js' + (options.longTermCaching ? '?[chunkhash:8]' : ''),
chunkFilename: (options.devServer ? '[id].js' : '[name].js')
+ (options.longTermCaching ? '?[chunkhash:8]' : ''),
sourceMapFilename: 'debugging/[file].map',
};
const plugins = [
new HtmlWebpackPlugin({
title: 'Redux-Example',
template: path.join(TEM_PATH, 'index.html'),
filename: 'index.html',
inject: 'body',
favicon: 'favicon.ico',
}),
// new webpack.PrefetchPlugin("react"), //预取插件,据说可以加快打包,然而并没有,也许是姿势不对
// new webpack.PrefetchPlugin("react/lib/ReactComponentBrowserEnvironment")
];
if (options.commonsChunk) { // 分析以下模块的共用代码, 单独打一个包到common.js
plugins.push(new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js'
+ (options.longTermCaching ? '?[chunkhash:8]' : '')));
}
if (options.separateStylesheet) {
plugins.push(new ExtractTextPlugin('[name].css' +
(options.longTermCaching ? '?[contenthash:8]' : '')));
}
else {
plugins.push(new ExtractTextPlugin('style.css' +
(options.longTermCaching ? '?[contenthash:8]' : ''), { allChunks: true }));
}
if (options.minimize) { //优化打包尺寸
plugins.push(
new webpack.optimize.UglifyJsPlugin({
output: {
comments: false, // remove all comments
},
compressor: {
warnings: false,
},
}),
new webpack.optimize.DedupePlugin()
);
}
if (options.minimize) {
plugins.push(
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production'),
},
}),
new webpack.NoErrorsPlugin()
);
}
const postcss = [
rucksack(),
autoprefixer({
browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8', 'iOS >= 8', 'Android >= 4'],
}),
]
return {
entry,
output,
target: 'web',
module,
devtool: options.devtool,
debug: options.debug,
resolveLoader: {
root: path.join(__dirname, 'node_modules'),
alias: aliasLoader,
},
externals,
resolve: {
root: ROOT_PATH,
modulesDirectories,
extensions,
alias,
},
plugins,
webserverPort,
postcss,
}
};