-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.prod.js
249 lines (242 loc) · 7.31 KB
/
webpack.prod.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
const path = require('path');
const glob = require('glob');
// const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const autoprefixer = require('autoprefixer');
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const webpack = require('webpack');
// const HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin');
// const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
// const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
// const threadLoader = require('thread-loader');
// const smp = new SpeedMeasurePlugin(); // 分析速度
// threadLoader.warmup(
// {
// },
// [
// 'babel-loader',
// 'less-loader',
// ],
// );
const setMAP = () => {
const entry = {};
const htmlWebpackPlugin = [];
const entryFiles = glob.sync(path.join(__dirname, './src/*/index.jsx'));
Object.keys(entryFiles).forEach((index) => {
const entryFile = entryFiles[index];
const match = entryFile.match(/src\/(.*)\/index\.jsx/);
const pageName = match && match[1];
entry[pageName] = entryFile;
htmlWebpackPlugin.push(
new HtmlWebpackPlugin({
template: path.join(__dirname, `src/${pageName}/index.html`),
filename: `${pageName}.html`,
chunks: ['venders', pageName], // 需要引入venders
inject: true,
minify: {
html5: true,
collapseWhitespace: true,
preserveLineBreaks: false,
minifyCSS: true,
removeComments: true,
},
}),
);
});
return {
entry,
htmlWebpackPlugin,
};
};
const { entry, htmlWebpackPlugin } = setMAP();
module.exports = {
// mode: 'none',
mode: 'production',
stats: 'errors-only', // 发生错误时,才打印日志
entry,
output: {
clean: true,
filename: '[name]_[chunkhash:6].js', // chunkhash
path: path.join(__dirname, 'dist/js'),
},
resolve: {
extensions: ['.js', '.jsx', '.less'],
aliasFields: ['browser'],
},
module: {
rules: [
{ test: /\.txt$/, use: ['cache-loader', 'raw-loader'] },
{
test: /\.(js|jsx)$/,
// use: ['thread-loader', 'babel-loader'],
use: [
{
loader: 'cache-loader',
},
{
loader: 'thread-loader',
options: {
// 产生的 worker 的数量,默认是 (cpu 核心数 - 1),或者,
// 在 require('os').cpus() 是 undefined 时回退至 1
workers: 2,
// 一个 worker 进程中并行执行工作的数量
// 默认为 20
workerParallelJobs: 50,
// 额外的 node.js 参数
workerNodeArgs: ['--max-old-space-size=1024'],
// 允许重新生成一个僵死的 work 池
// 这个过程会降低整体编译速度
// 并且开发环境应该设置为 false
poolRespawn: false,
// 闲置时定时删除 worker 进程
// 默认为 500(ms)
// 可以设置为无穷大,这样在监视模式(--watch)下可以保持 worker 持续存在
poolTimeout: 2000,
// 池分配给 worker 的工作数量
// 默认为 200
// 降低这个数值会降低总体的效率,但是会提升工作分布更均一
poolParallelJobs: 50,
// 池的名称
// 可以修改名称来创建其余选项都一样的池
name: 'my-pool',
},
},
{
loader: 'babel-loader?cacheDirectory=true', // babel-loader 构建缓存
},
],
},
// { test: /\.css$/, use: ['style-loader', 'css-loader'] },
// { test: /\.less$/, use: ['style-loader', 'css-loader', 'less-loader'] },
{
test: /\.css$/,
use: ['cache-loader', MiniCssExtractPlugin.loader, 'css-loader'],
},
{
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'less-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [
autoprefixer({
overrideBrowserslist: [
'last 10 Chrome versions',
'last 5 Firefox versions',
'Safari >= 6',
'ie> 8',
],
}),
],
},
},
},
{
loader: 'px2rem-loader',
options: {
remUnit: 75,
remPrecesion: 8, // 小数点位数
},
},
],
},
{
test: /\.(png|jpg|gif|jpeg|svg)$/,
use: [
// {
// loader: 'url-loader',
// options: {
// limit: 20280, // 20k, 小于20k 自动base64
// }
// }
{
loader: 'cache-loader',
},
{
loader: 'file-loader',
options: {
name: '[name]_[hash:8][emoji].[ext]',
},
},
],
},
{ test: /\.(woff|woff2|eot|ttf|otf)$/, use: 'file-loader' },
],
},
plugins: [
new webpack.DllReferencePlugin({
manifest: require('./build/libary/library.json'),
}),
new TerserPlugin({
parallel: false,
terserOptions: {
nameCache: null,
},
}),
new MiniCssExtractPlugin({
filename: '[name][contenthash:6].css',
}),
new CssMinimizerPlugin(),
new FriendlyErrorsWebpackPlugin(),
// new BundleAnalyzerPlugin(),
// function () {
// this.hooks.done.tap('done', (stats) => {
// if (stats.compilation.errors) {
// console.log('??');
// }
// });
// },
// new webpack.optimize.ModuleConcatenationPlugin(), // mode --> production 默认开启
// new HtmlWebpackExternalsPlugin({
// externals: [
// {
// module: 'react',
// entry: 'https://unpkg.com/react@18/umd/react.production.min.js',
// global: 'React'
// },
// {
// module: 'react-dom',
// entry: 'https://unpkg.com/react-dom@18/umd/react-dom.production.min.js',
// global: 'ReactDOM'
// }
// ]
// })
// 多页面打包,就复制多个htmlWebpackPlugin
].concat(htmlWebpackPlugin),
// devtool: 'inline-source-map', // eval source-map inline-source-map
optimization: {
splitChunks: {
chunks: 'all',
minSize: 20000, // 最小包体积
minRemainingSize: 0,
minChunks: 1,
maxAsyncRequests: 30,
maxInitialRequests: 30,
enforceSizeThreshold: 50000,
cacheGroups: {
commons: {
test: /(react|react-dom)/,
name: 'venders',
chunks: 'all',
},
defaultVendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
reuseExistingChunk: true,
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
},
},
},
},
};