-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.prod.js
168 lines (161 loc) · 5.52 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
const path = require('path');
const webpack = require('webpack'); //webpack 核心
const HtmlWebpackPlugin = require('html-webpack-plugin'); // 生成html
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer'); //webpack 打包大小体积分析
const { CleanWebpackPlugin } = require('clean-webpack-plugin'); // 每次打包前清除旧的build文件夹
const MiniCssExtractPlugin = require('mini-css-extract-plugin'); // 为了单独打包css
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); // 压缩CSS
const TerserPlugin = require('terser-webpack-plugin'); //多线程/多实例-并行压缩js
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin'); //仅报错时日志提示
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin'); //为模块提供中间缓存,第二次构建速度提升
const AntdDayjsWebpackPlugin = require('antd-dayjs-webpack-plugin'); // 使用day.js替代antd中的moment.js
const WebpackBar = require('webpackbar'); //进度条
module.exports = {
mode: 'production',
entry: {
app: './src/index.js',
vendor: ['react', 'react-dom'],
},
output: {
filename: '[name].[chunkhash:8].js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
// .js .jsx用babel解析
test: /\.(js|jsx)$/,
include: path.resolve(__dirname, 'src'), //只解析src下的模块
use: [
{
loader: 'thread-loader', //多进程/多实例的构建
options: { workers: 3 },
},
'babel-loader?cacheDirectory=true', //babel-loader 开启缓存,结合 TerserPlugin、HardSourceWebpackPlugin 提升二次构建速度
],
},
{
// .css 解析
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
},
{
// .less 解析
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
{
loader: 'less-loader',
options: { lessOptions: { javascriptEnabled: true } },
},
],
},
{
// 文件解析
test: /\.(eot|woff|otf|svg|ttf|woff2|appcache|mp3|mp4|pdf)(\?|$)/,
include: path.resolve(__dirname, 'src'),
use: [
{
loader: 'file-loader',
options: {
name: 'assets/[name].[hash:4].[ext]',
},
},
],
},
{
// 图片解析
test: /\.(png|jpg|jpeg|gif)$/i,
include: path.resolve(__dirname, 'src'),
use: [
{
loader: 'url-loader',
options: {
//限制图片大小 8192B,小于限制会将图片转换为 base64格式
limit: 8192,
name: 'assets/[name].[hash:4].[ext]',
},
},
],
},
{
// wasm文件解析
test: /\.wasm$/,
include: path.resolve(__dirname, 'src'),
type: 'webassembly/experimental',
},
{
// xml文件解析
test: /\.xml$/,
include: path.resolve(__dirname, 'src'),
use: ['xml-loader'],
},
],
},
plugins: [
/* 体积分析 */
// new BundleAnalyzerPlugin(),
/* 自动生成HTML,并注入各参数 */
new HtmlWebpackPlugin({
filename: 'index.html', // 生成的html存放路径,相对于 output.path
template: './public/index.html', // html模板路径
inject: true, // 是否将js放在body的末尾
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production'),
}),
/* 提取CSS等样式生成单独的CSS文件 */
new MiniCssExtractPlugin({
filename: '[name].[contenthash:8].css', // 生成的文件名
}),
new AntdDayjsWebpackPlugin(), // dayjs 替代 momentjs
/* 打包进度条展示 */
new WebpackBar(),
/* 缓存机制,第二次构建速度提升 */
new HardSourceWebpackPlugin(),
/* 构建日志提示优化 */
new FriendlyErrorsWebpackPlugin(),
/* 打包前删除上一次打包留下的旧代码(根据output.path) */
new CleanWebpackPlugin(),
],
// 优化打包配置
optimization: {
//设置为 true, 一个chunk打包后就是一个文件,一个chunk对应`一些js css 图片`等,利用浏览器缓存,减少文件更新频率
runtimeChunk: {
name: 'runtime',
},
minimizer: [
new TerserPlugin({
//并行压缩js
parallel: true, // 多线程并行构建
terserOptions: {
compress: {
warnings: false, // 删除无用代码时是否给出警告
drop_debugger: true, // 删除所有的debugger
pure_funcs: ['console.log'], // 删除所有的console.log
},
},
}),
new OptimizeCSSAssetsPlugin({}),
],
splitChunks: {
chunks: 'all', // 默认entry的chunk不会被拆分 配置成all 便全部拆分了
},
},
stats: {
warningsFilter: (warning) => /Conflicting order/gm.test(warning), // 不输出一些警告,多为因CSS引入顺序不同导致的警告
children: false, // 不输出子模块的打包信息
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.less', '.css', '.wasm'], //后缀名自动补全
alias: {
'@': path.resolve(__dirname, 'src'),
},
},
performance: {
hints: false, // 性能设置,文件打包过大时,不报错和警告,只做提示
maxEntrypointSize: 400000,
},
};