-
Notifications
You must be signed in to change notification settings - Fork 6
/
webpack.config.js
64 lines (58 loc) · 1.65 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
// 参考 https://github.com/sorrycc/roadhog/issues/69 进行 hash 配置。
// const fs = require('fs');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WebpackChunkHash = require('webpack-chunk-hash');
module.exports = (config, env) => {
config.module.loaders[0].exclude.push(/\.ejs$/);
config.module.loaders.forEach((loader) => {
if ('babel' === loader.loader) {
// eslint-disable-next-line
loader.exclude = [/node_modules/];
}
});
if ('production' === env) {
Object.assign(config.output, {
filename: '[name].[chunkhash].js',
chunkFilename: '[chunkhash].async.js',
});
const plugins = config.plugins.map((plugin) => {
if (plugin instanceof ExtractTextPlugin) {
return new ExtractTextPlugin('[name].[contenthash:20].css');
}
else if (plugin instanceof webpack.optimize.CommonsChunkPlugin) {
return new webpack.optimize.CommonsChunkPlugin({
name: 'common',
filename: 'common.[chunkhash:20].js',
});
}
else {
return plugin;
}
});
plugins.push(
new HtmlWebpackPlugin({
template: 'ejs!src/index.ejs',
inject: true,
minify: {
collapseWhitespace: true,
},
production: true,
}),
new WebpackChunkHash({ algorithm: 'md5' }),
);
Object.assign(config, {
plugins,
});
}
else {
config.plugins.push(
new HtmlWebpackPlugin({
template: 'ejs!src/index.ejs',
inject: true,
}),
);
}
return config;
};