-
Notifications
You must be signed in to change notification settings - Fork 30
/
webpack.config.js
479 lines (390 loc) · 11.4 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
var path = require('path');
var webpack = require('webpack');
/*
* verify config
* (验证config文件是否正确)
* */
var validate = require('webpack-validator');
/*
* clean publishing directory
* (清空发布目录)
* */
var CleanWebpackPlugin = require('clean-webpack-plugin');
/*
* create html
* (创建html文件)
* */
var HtmlWebpackPlugin = require('html-webpack-plugin');
/*
* extract css
* (提取css文件)
* */
var ExtractTextPlugin = require("extract-text-webpack-plugin");
/*
* merge config
* (合并config文件)
* */
var Merge = require('webpack-merge');
/*
* auto open browser
* (自动打开浏览器)
* */
var OpenBrowserPlugin = require('open-browser-webpack-plugin');
/*
* Detect how npm is run and branch based on that
* (当前 npm 运行)
* */
var currentTarget = process.env.npm_lifecycle_event;
var debug, // is debug
devServer, // is hrm mode
minimize; // is minimize
if (currentTarget == "build") { // online mode (线上模式)
debug = false, devServer = false, minimize = true;
} else if (currentTarget == "dev") { // dev mode (开发模式)
debug = true, devServer = false, minimize = false;
} else if (currentTarget == "dev-hrm") { // dev HRM mode (热更新模式)
debug = true, devServer = true, minimize = false;
}
/*
* proxy target address
* (代理访问地址)
* */
var proxyTarget = 'http://localhost:8888/';
var PATHS = {
/*
* publish path
* (发布目录)
* */
publicPath: devServer ? '/webpackForSPA/dist/' : './',
/*
* public resource path
* (公共资源目录)
* */
libsPath: path.resolve(process.cwd(), './libs'),
/*
* resource path
* (src 目录)
* */
srcPath: path.resolve(process.cwd(), 'src'),
/*
* node_modules path
*/
node_modulesPath: path.resolve('./node_modules'),
}
var resolve = {
/*
* An array of extensions that should be used to resolve modules
* (引用时可以忽略后缀)
* */
extensions: ['', '.js', '.css', '.scss', '.ejs', '.png', '.jpg'],
/*
* The directory (absolute path) that contains your modules
* */
root: [
PATHS.node_modulesPath
],
/*
* Replace modules with other modules or paths.
* (别名,引用时直接可以通过别名引用)
* */
alias: {
/*
* js
*/
jquery: path.join(PATHS.libsPath, "js/jquery/jquery"),
underscore: path.join(PATHS.libsPath, "js/underscore/underscore.js"),
/*
* css
*/
bootstrapcss: path.join(PATHS.libsPath, "css/bootstrap/bootstrap-3.3.5.css"),
indexcss: path.join(PATHS.srcPath, "css/index.css"),
}
}
/*
* The entry point for the bundle.
* (入口)
* */
var entry = {
index: './src/js/index.js',
common: [
path.join(PATHS.libsPath, "js/jquery/jquery.js"),
path.join(PATHS.libsPath, "js/underscore/underscore.js")
],
};
/*
* output options tell Webpack how to write the compiled files to disk
* (webpack 编译后输出标识)
* */
var output = {
/*
* determines the location on disk the files are written to
* (输出目录)
* */
path: path.join(__dirname, 'dist'),
/*
* The publicPath specifies the public URL address of the output files when referenced in a browser
* (发布后,资源的引用目录)
* */
publicPath: PATHS.publicPath,
/*
* Specifies the name of each output file on disk
* (文件名称)
* */
filename: devServer ? 'js/[name].js' : 'js/[name]-[chunkhash:8].js',
/*
* The filename of non-entry chunks as relative path inside the output.path directory.
* (按需加载模块时输出的文件名称)
* */
chunkFilename: devServer ? 'js/[name].js' : 'js/[name]-[chunkhash:8].js'
}
var loaders = [
/*
* Exports HTML as string, require references to static resources.
* (html loader)
* */
{
test: /\.html$/,
loader: "html"
// loader: "html?-minimize"
},
/*
* img loader
* */
{
test: /\.(png|gif|jpe?g)$/,
loader: 'url-loader',
query: {
/*
* limit=10000 : 10kb
* 图片大小小于10kb 采用内联的形式,否则输出图片
* */
limit: 10000,
name: '/img/[name]-[hash:8].[ext]'
}
},
/*
* font loader
* */
{
test: /\.(eot|woff|woff2|ttf|svg)$/,
loader: 'url-loader',
query: {
limit: 5000,
name: '/font/[name]-[hash:8].[ext]'
}
},
/*
* Extract css files
* (提取css到单独文件loader)
*/
{
test: /\.css$/,
loader: ExtractTextPlugin.extract("style-loader", "css-loader!postcss-loader", {
publicPath: '../'
})
},
];
var plugins = [
/*
* gloabal flag
* (全局标识)
* */
new webpack.DefinePlugin({
/*
* dev flag
* (开发标识)
* */
__DEV__: debug,
/*
* proxy flag
* (代理的标识)
* */
__DEVAPI__: devServer ? "/devApi/" : "''",
}),
/*
* common js
* (公共js)
* */
new webpack.optimize.CommonsChunkPlugin(
devServer ?
{name: "common", filename: "js/common.js"}:
{names: ["common", "webpackAssets"]}
),
/*
* Module (value) is loaded when the identifier (key) is used as free variable in a module
* (如:使用jquery 可以直接使用符号 "$")
* */
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery",
"_": "underscore"
}),
/*
* Search for equal or similar files and deduplicate them in the output
* (删除重复依赖的文件)
*/
new webpack.optimize.DedupePlugin(),
/*
* Using this config the vendor chunk should not be changing its hash unless you change its code or dependencies
* (避免在文件不改变的情况下hash值不变化)
* */
new webpack.optimize.OccurenceOrderPlugin(),
/*
* clean publishing directory
* (发布前清空发布目录)
* */
new CleanWebpackPlugin(['dist'], {
root: '', // An absolute path for the root of webpack.config.js
verbose: true,// Write logs to console.
dry: false // Do not delete anything, good for testing.
}),
/*
* extract css
* (提取css文件到单独的文件中)
*/
new ExtractTextPlugin(devServer ? "css/[name].css" : "css/[name]-[chunkhash:8].css", {allChunks: true}),
/*
*create html file
* (创建html文件)
* */
new HtmlWebpackPlugin({
filename: 'index.html',
template: __dirname + '/src/index.html',
/*
* inject: true | 'head' | 'body' | false Inject all assets into the given template or templateContent -
* When passing true or 'body' all javascript resources will be placed at the bottom of the body element.
* 'head' will place the scripts in the head element.
* */
inject: 'true',
// 需要依赖的模块
chunks: ['common', 'index', 'webpackAssets'],
// 根据依赖自动排序
chunksSortMode: 'dependency'
}),
new HtmlWebpackPlugin({
filename: 'html/hrm.html',
template: __dirname + '/src/html/hrm.html',
inject: false,
}),
new HtmlWebpackPlugin({
filename: 'html/home.html',
template: __dirname + '/src/html/home.html',
inject: false,
}),
new HtmlWebpackPlugin({
filename: 'html/menu1.html',
template: __dirname + '/src/html/menu1.html',
inject: false,
}),
new HtmlWebpackPlugin({
filename: 'html/menu2.html',
template: __dirname + '/src/html/menu2.html',
inject: false,
}),
new HtmlWebpackPlugin({
filename: 'html/menu3.html',
template: __dirname + '/src/html/menu3.html',
inject: false,
}),
new HtmlWebpackPlugin({
filename: 'html/menu5.html',
template: __dirname + '/src/html/menu5.html',
inject: false,
}),
new HtmlWebpackPlugin({
filename: 'html/menu6.html',
template: __dirname + '/src/html/menu6.html',
inject: false,
})
];
if (minimize) {
plugins.push(
/*
* Uglify
* (压缩)
* */
new webpack.optimize.UglifyJsPlugin({ // js、css都会压缩
mangle: {
except: ['$super', '$', 'exports', 'require', 'module', '_']
},
compress: {
warnings: false
},
output: {
comments: false,
}
})
)
}
var config = {
entry: entry,
/*
* Like resolve but for loaders.
* (查找loader 的位置)
* */
resolveLoader: {root: path.join(__dirname, "node_modules")},
output: output,
module: {
loaders: loaders
},
resolve: resolve,
plugins: plugins,
}
/*
* Hrm setting
* (开启热更新,并自动打开浏览器)
* */
if (devServer) {
config = Merge(
config,
{
plugins: [
// Enable multi-pass compilation for enhanced performance
// in larger projects. Good default.
new webpack.HotModuleReplacementPlugin({
multiStep: true
}),
new OpenBrowserPlugin({url: 'http://localhost:8080' + PATHS.publicPath + 'index.html'})
],
devServer: {
// Enable history API fallback so HTML5 History API based
// routing works. This is a good default that will come
// in handy in more complicated setups.
historyApiFallback: true,
// Unlike the cli flag, this doesn't set
// HotModuleReplacementPlugin!
hot: true,
inline: true,
// Display only errors to reduce the amount of output.
stats: 'errors-only',
// Parse host and port from env to allow customization.
//
// If you use Vagrant or Cloud9, set
// host: options.host || '0.0.0.0';
//
// 0.0.0.0 is available to all network devices
// unlike default `localhost`.
host: "localhost", // Defaults to `localhost` process.env.HOST
port: "8080", // Defaults to 8080 process.env.PORT
/*
* 代理访问
* 1、可以绕过同源策略 和 webpack '热更新'结合使用
*/
proxy: {
'/devApi/*': {
target: proxyTarget,
secure: true,
/*
* rewrite 的方式扩展性更强,不限制服务的名称
* */
rewrite: function (req) {
req.url = req.url.replace(/^\/devApi/, '');
}
}
}
}
}
);
}
module.exports = validate(config);