-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
138 lines (131 loc) · 3.54 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
const glob = require('glob');
const path = require('path');
// minicss 要在 html 前面
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCssPlugin = require('optimize-css-assets-webpack-plugin');
const development = process.env.NODE_ENV === 'development';
const webpackConfig = {
mode: development ? 'development' : 'production',
devtool: development ? 'eval-source-map' : 'cheap-source-map',
entry: {},
output: {
path: path.resolve('./public'),
filename: development ? 'js/[name].js' : 'js/[name].[chunkhash:6].js',
chunkFilename: development ? 'js/[name].js' : 'js/[name].[chunkhash:6].js',
publicPath: '/public/'
},
module: {
rules: [{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
]
},
{
test: /\.less/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'less-loader'
]
},
{
test: /\.(png|ico|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 1024,
name: development ? 'img/[name].[ext]' : 'img/[name].[hash:8].[ext]',
}
},
{
test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
loader: 'url-loader',
options: {
limit: 1024,
name: development ? 'font/[name].[ext]' : 'font/[name].[hash:8].[ext]',
}
},
{
test: /\.html$/,
loader: 'html-loader',
}]
},
plugins: [
new MiniCssExtractPlugin({
filename: development ? 'css/[name].css' : 'css/[name].[contenthash:8].css',
chunkFilename: development ? 'css/[id].css' : 'css/[name].[contenthash:8].css',
}),
new CleanWebpackPlugin({
root: __dirname,
verbose: true,
dry: false
})
],
};
// 获取指定路径下的入口文件
function getEntries(globPath) {
const files = glob.sync(globPath);
const entries = {};
files.forEach(filepath => {
const split = filepath.split('/');
const name = split[split.length - 2];
entries[name] = `./${filepath}`;
});
return entries;
}
const entries = getEntries('web/index.js');
Object.keys(entries).forEach(name => {
webpackConfig.entry[name] = entries[name];
const plugin = new HtmlWebpackPlugin({
filename: `${name}.html`,
template: './web/index.html',
inject: true,
chunks: [name],
minify: development ? false : {
removeComments: true, // 移除HTML中的注释
collapseWhitespace: true, // 折叠空白区域 也就是压缩代码
removeAttributeQuotes: true, // 去除属性引用
},
});
webpackConfig.plugins.push(plugin);
});
if (!development) {
webpackConfig.optimization = {
minimizer: [
// 压缩js https://github.com/mishoo/UglifyJS2/tree/harmony#compress-options
new UglifyJsPlugin({
parallel: 2,
cache: true,
sourceMap: true,
uglifyOptions: {
warnings: false,
compress: {
drop_debugger: false,
drop_console: true
},
output: {
comments: false
}
}
}),
// 压缩css
new OptimizeCssPlugin({
cssProcessorOptions: {
// mergeLonghand: false,
map: { inline: false },
safe: true
}
}),
],
};
}
module.exports = webpackConfig;