-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathwebpack.config.js
329 lines (307 loc) · 9.38 KB
/
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*eslint-disable*/
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const InlineChunkWebpackPlugin = require('html-webpack-inline-chunk-plugin');
// 引入 Html 处理
const HtmlWebpackPlugin = require('html-webpack-plugin');
// 引入 CSS 处理
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
// 引入 JS 处理
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
var autoprefixer = require('autoprefixer');
var precss = require('precss');
// 引入进度查看
var ProgressBarPlugin = require('progress-bar-webpack-plugin');
var chalk = require('chalk');
// 获取本地 ip 地址
var localAddress = require('ip').address();
// 去除警告提示
process.noDeprecation = true;
console.log(`▶️ 当前环境:${process.env.NODE_ENV}, 开始构建... 🚀 `);
// 根据当前 src 获得项目路径
let rootDir = __dirname.substr(0, __dirname.indexOf('src'));
let buildPath = __dirname + '/example';
// 设置 DEBUG 环境变量,用于环境判断
const DEBUG = process.env.NODE_ENV === 'development';
// ================================= 基础配置 =================================
// babel 配置项
const babelOptions = {
"presets": [
["es2015", {
"modules": false
}],
"react"
],
};
const commonConfig = merge([{
entry: {
index: [process.env.NODE_ENV == 'release' ?'./lib/index' : './src/index.js' ],
},
module: {
rules: [{
// 对 js/jsx 文件的处理
test: /\.(js|jsx)$/,
use: {
loader: 'babel-loader',
options: babelOptions,
}
}, {
// 对 scss/css 文件的处理
test: /\.scss$|\.css$/,
loader: [
'style-loader', 'css-loader?modules&localIdentName=[name]__[local]-[hash:base64:5]', 'sass-loader', 'postcss-loader'
],
}, {
test: /\.html$/,
use: ['raw-loader'],
exclude: /node_modules/
}, {
// 处理图片
test: /\.(png|jpg|jpeg|gif|svg)$/,
exclude: /node_modules/,
loaders: [{
loader: 'url-loader',
query: {
limit: 10000,
name: 'img/[name].[ext]?[hash:8]',
}
}, {
loader: 'image-webpack-loader',
query: {
mozjpeg: {
progressive: true
},
bypassOnDebug: DEBUG,
pngquant: {
quality: '75-90'
}
}
}]
},
// 处理字体文件
{
test: /\.(ttf|woff|woff2)(\?.+)?$/,
use: [{
loader: 'url-loader',
options: {
limit: 10000,
name: 'font/[name].[ext]?[hash:8]',
}
}]
}
]
},
plugins: [
new webpack.LoaderOptionsPlugin({
options: {
postcss: [
precss(),
autoprefixer({
browsers: [
'Android >= 4', 'iOS >= 7.1'
],
}),
]
}
}),
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify(process.env.NODE_ENV)
}
}),
new webpack.LoaderOptionsPlugin({
debug: DEBUG
}),
],
resolve: {
extensions: [
'.js', '.jsx'
], //后缀名自动补全
modules: ["libs", path.join(rootDir, "node_modules"), "node_modules"],
},
}]);
// ================================= 发布配置 =================================
const productionConfig = merge([{
watch: false,
cache: false,
output: {
path: buildPath,
filename: "js/[name].bundle.js?[chunkhash:8]",
publicPath: ''
},
stats: {
assets: true,
children: false,
chunks: false,
hash: true,
modules: false,
publicPath: false,
timings: false,
version: false,
warnings: true,
colors: {
green: '\u001b[32m'
}
},
plugins: [
new UglifyJSPlugin({
// 最紧凑的输出
beautify: false,
// 删除所有的注释
comments: false,
compress: {
// 在UglifyJs删除没有用到的代码时不输出警告
warnings: false,
// 删除所有的 `console` 语句
// 还可以兼容ie浏览器
drop_console: true,
// 内嵌定义了但是只用到一次的变量
collapse_vars: true,
// 提取出出现多次但是没有定义成变量去引用的静态值
reduce_vars: true
}
}),
new ProgressBarPlugin({
format: ' build [:bar] ' + chalk.green.bold(':percent') + ' (:elapsed seconds)',
clear: false,
callback: function () {
setTimeout(() => {
process.exit();
}, 0);
},
}),
// 处理 html 文件
new HtmlWebpackPlugin({
filename: 'index.html',
template: './index.html',
inject: true,
hash: false, // 是否增加资源文件指纹 hash
minify: {
collapseWhitespace: true, // 去除多余空格
preserveLineBreaks: true,
html5: true,
minifyJS: true, // 是否压缩 js
},
}),
new webpack.optimize.CommonsChunkPlugin({
name: ["vendor", "manifest"],
minChunks: Infinity,
}),
new ExtractTextPlugin('css/styles.css?[contenthash:8]'),
new InlineChunkWebpackPlugin({
inlineChunks: ['manifest']
}),
new webpack.HashedModuleIdsPlugin(),
new OptimizeCssAssetsPlugin({
assetNameRegExp: /\.css$/g,
cssProcessor: require('cssnano'),
cssProcessorOptions: {
discardComments: {
removeAll: true
}
},
canPrint: true
})
]
}]);
// ================================= 开发配置 =================================
const developmentConfig = merge([{
watch: true,
cache: true,
output: {
path: buildPath,
filename: 'js/[name].bundle.js',
publicPath: ''
},
devtool: 'cheap-module-eval-source-map',
devServer: {
contentBase: buildPath,
clientLogLevel: 'none',
hot: true,
compress: true,
host: localAddress, // 本地调试地址
overlay: true, // 输出编译报错在 window 上
disableHostCheck: true,
stats: {
assets: true,
colors: true,
version: true,
hash: true,
timings: true,
chunks: false,
chunkModules: false,
modules: false,
},
},
plugins: [
new webpack.NamedModulesPlugin(),
new HtmlWebpackPlugin({
filename: 'index.html',
template: './index.html',
}),
new webpack.HotModuleReplacementPlugin(),
]
}]);
const releaseConfig = merge([{
watch: false,
cache: false,
output: {
path: __dirname + '/dist',
filename: "main.js",
publicPath: '',
libraryTarget: "umd",
library: 'react-city-select',
},
externals: {
react: 'react',
},
stats: {
assets: true,
children: false,
chunks: false,
hash: true,
modules: false,
publicPath: false,
timings: false,
version: false,
warnings: true,
colors: {
green: '\u001b[32m'
}
},
plugins: [
new ProgressBarPlugin({
format: ' build [:bar] ' + chalk.green.bold(':percent') + ' (:elapsed seconds)',
clear: false,
callback: function () {
setTimeout(() => {
process.exit();
}, 0);
},
}),
new ExtractTextPlugin('styles.css'),
new webpack.HashedModuleIdsPlugin(),
new OptimizeCssAssetsPlugin({
assetNameRegExp: /\.css$/g,
cssProcessor: require('cssnano'),
cssProcessorOptions: {
discardComments: {
removeAll: true
}
},
canPrint: true
})
]
}]);
// ================================= 合并配置输出 =================================
if (process.env.NODE_ENV == 'release') {
module.exports = merge(commonConfig, releaseConfig);
} else {
if (DEBUG) {
module.exports = merge(commonConfig, developmentConfig);
} else {
module.exports = merge(commonConfig, productionConfig);
}
}