-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.prod.js
173 lines (158 loc) · 5.58 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
const { merge } = require('webpack-merge');
const webpack = require('webpack');
const path = require('path');
// const glob = require('glob');
// const PurgecssPlugin = require('purgecss-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyPlugin = require("copy-webpack-plugin");
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const BaseConfig = require('./webpack.config');
const BundleAnalyzerPlugin =
require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const HtmlMinimizerPlugin = require("html-minimizer-webpack-plugin");
const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
const { extendDefaultPlugins } = require("svgo");
const analyzer = process.env.analyzer;
module.exports = merge(BaseConfig, {
mode: 'production',
optimization: {
/** 开始最小化 */
minimize: true,
minimizer: [
/** 压缩html */
new HtmlMinimizerPlugin({
test: /index\.html/i,
}),
// 压缩js
new TerserPlugin({
extractComments: false,
terserOptions: {
toplevel: true,
ie8: true,
safari10: true,
format: {
comments: false,
}
},
}),
// 压缩css
new CssMinimizerPlugin({
exclude: /[\\/]node_modules[\\/]/,
minimizerOptions: {
preset: [
'default',
{
discardComments: { removeAll: true },
},
],
},
minify: CssMinimizerPlugin.cleanCssMinify,
}),
],
splitChunks: {
/** 默认提取的分割的类型 选项: all、async、initial 默认:async */
chunks: 'all',
/** 最大提取字节, 单位 bytes */
maxSize: 150000,
/** 最小提取字节,如果模块的大小大于多少的话才需要提取 单位 bytes */
minSize: 30000,
maxAsyncSize: 50000,
minChunks: 1,
automaticNameDelimiter: '~',
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'initial',
maxSize: 150000,
priority: -10,
/** 是否可以复用现有的模块 */
reuseExistingChunk: true,
},
common: {
chunks: 'initial',
name: "common",
/** 优先级 */
priority: -20,
minChunks: 2,
minSize: 0,
},
styles: {
name: 'styles',
type: "css/mini-extract",
test: /\.less$/,
priority: 20,
enforce: true,
}
}
},
runtimeChunk: {
name: 'runtime',
},
},
module: {
rules: []
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production'),
}),
new CleanWebpackPlugin(),
new CopyPlugin({
patterns: [
{ from: path.resolve(__dirname, 'src/assets/other'), to: "static" },
],
}),
/** 提取公共css */
new MiniCssExtractPlugin({
filename: 'style/[name].[contenthash:8].css',
chunkFilename: 'style/[id].[contenthash:8].css',
}),
/** 移除无用的内容 */
// new PurgecssPlugin({
// paths: glob.sync(`${path.join(__dirname, 'src')}/**/*`, { nodir: true }),
// }),
/** 图片压缩 */
new ImageMinimizerPlugin({
test: /.(jpe?g|png|gif|tif|webp|svg|avif)$/i,
include: path.resolve(__dirname, 'src'),
minimizerOptions: {
plugins: [
["gifsicle", { interlaced: true }],
["mozjpeg", {
quality: 50,
progressive: false
}],
["pngquant", {
quality: [0.6, 0.8],
optimizationLevel: 5
}],
// Svgo configuration here https://github.com/svg/svgo#configuration
[
"svgo",
{
plugins: extendDefaultPlugins([
{
name: "removeViewBox",
active: false,
},
{
name: "addAttributesToSVGElement",
params: {
attributes: [{ xmlns: "http://www.w3.org/2000/svg" }],
},
},
]),
},
],
],
}
}),
].concat(analyzer == 1 ? [
new BundleAnalyzerPlugin({
analyzerPort: 7777,
})
] : [])
})