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
| // webpack.config.js
const path = require('path')
const TerserPlugin = require('terser-webpack-plugin')
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
module.exports = {
// ========== 1. 构建模式 ==========
mode: 'production',
// ========== 2. 代码分割 ==========
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
// 框架代码单独打包
framework: {
test: /[\\/]node_modules[\\/](react|react-dom|vue)[\\/]/,
name: 'framework',
priority: 40
},
// UI库单独打包
ui: {
test: /[\\/]node_modules[\\/](@mui|@ant-design|element-plus)[\\/]/,
name: 'ui',
priority: 30
},
// 工具库
lib: {
test: /[\\/]node_modules[\\/](lodash|axios|dayjs)[\\/]/,
name: 'lib',
priority: 20
},
// 公共代码
commons: {
minChunks: 2,
priority: 10,
reuseExistingChunk: true
}
}
},
// 运行时代码单独提取
runtimeChunk: 'single',
// 压缩配置
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
parse: { ecma: 8 },
compress: {
ecma: 5,
warnings: false,
comparisons: false,
inline: 2,
drop_console: true
},
mangle: { safari10: true },
output: {
ecma: 5,
comments: false,
ascii_only: true
}
}
}),
new CssMinimizerPlugin()
]
},
// ========== 3. 模块解析 ==========
resolve: {
// 路径别名
alias: {
'@': path.resolve(__dirname, 'src'),
'@components': path.resolve(__dirname, 'src/components'),
'@utils': path.resolve(__dirname, 'src/utils')
},
// 扩展名
extensions: ['.js', '.jsx', '.ts', '.tsx', '.vue', '.json'],
// 模块查找路径
modules: ['node_modules', path.resolve(__dirname, 'src')]
},
// ========== 4. 缓存配置 ==========
cache: {
type: 'filesystem',
buildDependencies: {
config: [__filename]
}
},
// ========== 5. 输出配置 ==========
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'js/[name].[contenthash:8].js',
chunkFilename: 'js/[name].[contenthash:8].chunk.js',
// 资源文件名
assetModuleFilename: 'assets/[name].[hash:8][ext]'
},
// ========== 6. 性能优化 ==========
performance: {
hints: 'warning',
maxAssetSize: 244 * 1024, // 244KB
maxEntrypointSize: 244 * 1024
}
}
|