-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathwebpack.config.js
516 lines (423 loc) · 14.1 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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
var path = require('path');
var glob = require('glob');
var pkg=require("./package.json");
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 = false;
} else if (currentTarget == "build-dev") { // dev mode (开发模式)
debug = true, devServer = false, minimize = false;
} else if (currentTarget == "dev-hrm") { // dev HRM mode (热更新模式)
debug = true, devServer = true, minimize = false;
}
/**
* 获得路径
* @param globPath: str
* @param pathDir: str 对比路径
* @returns {{}}
*/
function getEntry(globPath, pathDir) {
var files = glob.sync(globPath);
var entries = {},
entry, dirname, basename, pathname, extname;
for (var i = 0; i < files.length; i++) {
entry = files[i];
dirname = path.dirname(entry);
extname = path.extname(entry);
basename = path.basename(entry, extname);
pathname = path.normalize(path.join(dirname, basename));
pathDir = path.normalize(pathDir);
if(pathname.startsWith(pathDir)){
pathname = pathname.substring(pathDir.length)
}
entries[pathname] = ['./' + entry];
}
return entries;
}
//获取 html 多模块入口文件
var file_css = getEntry('./src/css/**/*.?(css|scss)', './src/css/');
var file_components = getEntry('./src/components/**/*.?(js|html)', './src/components/');
var file_js = getEntry('./src/js/**/*.js', './src/js/');
var file_html = getEntry('./src/html/**/*.html', './src/html/');
var pages = Object.keys(file_html);
var PATHS = {
/*
* publish path
* (发布目录)
* */
publicPath: devServer ? '/webpack-avalon2-SPA-seed/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.js"),
// avalon2: path.join(PATHS.libsPath, "js/avalon/avalon.modern.js"),
// avalon2: path.join(PATHS.libsPath, "js/avalon/avalon2.2.7.js"),
avalon2: path.join(PATHS.libsPath, "js/avalon/avalon2.2.5.js"),
mmRouter: path.join(PATHS.libsPath, "js/avalon/mmRouter.js"),
lodash: path.join(PATHS.libsPath, "js/lodash.min.js"),
/*
* components
*/
heredoc: path.join(PATHS.srcPath, "components/heredoc.js"),
pagination: path.join(PATHS.srcPath, "components/pagination.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 = Object.assign(file_js,file_components, {
// 路由入口
index: ['./src/index.js'],
// 用到什么公共lib(例如jquery.js),就把它加进common去,目的是将公用库单独提取打包
common: [
'jquery','avalon2','lodash','mmRouter'
]
});
/*
* 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: "",
/*
* 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",
"avalon": "avalon2",
"window.avalon": "avalon2",
"_": "lodash"
}),
/*
* 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: 'body',
// 需要依赖的模块
chunks: ['common', 'index', 'webpackAssets'],
// 根据依赖自动排序
chunksSortMode: 'dependency'
})
];
//html 模板插件
pages.forEach(function(pathname) {
var conf = {
filename: 'html/' + pathname + '.html', //生成的html存放路径,相对于path
template: path.resolve(__dirname, './src/html/' + pathname + '.html'), //html模板路径
inject: false
/*
* 压缩这块,调用了html-minify,会导致压缩时候的很多html语法检查问题,
* 如在html标签属性上使用{{...}}表达式,很多情况下并不需要在此配置压缩项,
* 另外,UglifyJsPlugin会在压缩代码的时候连同html一起压缩。
* 为避免压缩html,需要在html-loader上配置'html?-minimize',见loaders中html-loader的配置。
*/
/* minify: { //压缩HTML文件
removeComments: true, //移除HTML中的注释
collapseWhitespace: false //删除空白符与换行符
}*/
};
plugins.push(new HtmlWebpackPlugin(conf));
});
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) {
var webpackHot='webpack/hot/dev-server';
var webpackClient='webpack-dev-server/client?http://'+pkg.config.devHost+':'+pkg.config.devPort;
/*pages.forEach(function (e) {
file_css[e].unshift(webpackClient,webpackHot);
file_html[e].unshift(webpackClient,webpackHot);
file_js[e].unshift(webpackClient,webpackHot);
});*/
// config.entry.index.unshift('webpack-dev-server/client?http://'+pkg.config.devHost+':'+pkg.config.devPort+'', "webpack/hot/dev-server");
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://' + pkg.config.devHost + ':' + pkg.config.devPort + PATHS.publicPath + 'index.html'})
],
devServer: {
contentBase: PATHS.publicPath,
// 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: pkg.config.devHost, // Defaults to `localhost` process.env.HOST
port: pkg.config.devPort, // Defaults to 8080 process.env.PORT
/*
* 代理访问
* 1、可以绕过同源策略 和 webpack '热更新'结合使用
*/
/*proxy: {
'/devApi/!*': {
target: pkg.config.devApi,
secure: true,
/!*
* rewrite 的方式扩展性更强,不限制服务的名称
* *!/
rewrite: function (req) {
req.url = req.url.replace(/^\/devApi/, '');
}
}
}*/
}
}
);
}
//module.exports = validate(config);
module.exports = config;