-
Notifications
You must be signed in to change notification settings - Fork 10
/
webpack.config.prod.js
308 lines (306 loc) · 8.92 KB
/
webpack.config.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
const webpack = require("webpack");
const TerserPlugin = require("terser-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin"); //打包压缩css
const HtmlWebpackPlugin = require("html-webpack-plugin"); //生成html并注入
const CopyWebpackPlugin = require("copy-webpack-plugin"); //拷贝资源文件
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const AutoImport = require("unplugin-auto-import/webpack");
const path = require("path");
// 设置node.js生产环境变量
const GLOBALS = {
"process.env.NODE_ENV": JSON.stringify("production"),
"process.env.BUILD_TYPE": JSON.stringify("webpack"),
__DEV__: false
};
const config = {
resolve: {
//识别扩展文件名
extensions: ["*", ".js", ".jsx", ".json"],
alias: {
components: path.resolve(__dirname, "src/components/"),
constants: path.resolve(__dirname, "src/store/constants/"),
actions: path.resolve(__dirname, "src/store/actions/"),
reducers: path.resolve(__dirname, "src/store/reducers/"),
router: path.resolve(__dirname, "src/router/"),
style: path.resolve(__dirname, "src/style/"),
store: path.resolve(__dirname, "src/store/"),
utils: path.resolve(__dirname, "src/utils/"),
assets: path.resolve(__dirname, "src/public/assets/")
}
},
//开启调试
entry: {
app: path.resolve(__dirname, "src/index.jsx") // 定位客户端目标
},
target: "web", // 目标是web服务
mode: "production",
output: {
//输出目录
path: path.resolve(__dirname, "dist"),
publicPath: "/",
filename: "js/[name]-[hash].js?v=[chunkhash]",
chunkFilename: "js/[name]-[hash].js?v=[chunkhash]",
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false,
exclude: /\/excludes/,
parallel: 4,
terserOptions: {
ecma: undefined,
parse: {},
compress: true,
mangle: true, // Note `mangle.properties` is `false` by default.
module: false,
// Deprecated
output: null,
format: null,
toplevel: false,
nameCache: false,
ie8: false,
keep_classnames: undefined,
keep_fnames: false,
safari10: false,
},
}),
new CssMinimizerPlugin()
],
splitChunks: {
chunks: "all",
minSize: {
javascript: 30000, // 模块要大于30kb才会进行提取
style: 50000, // 模块要大于50kb才会进行提取
},
minRemainingSize: 0,
minChunks: 1,
maxAsyncRequests: 5,
maxInitialRequests: 3,
// name: true,
automaticNameDelimiter: "~",
cacheGroups: {
vendor: {//node_modules内的依赖库
chunks: "all",
test: /[\\/]node_modules[\\/]/,
name: "vendor",
minChunks: 1, //被不同entry引用次数(import),1次的话没必要提取
maxInitialRequests: 5,
minSize: 0,
priority: 100,
reuseExistingChunk: true
// enforce: true?
},
common: {// ‘src/js’ 下的js文件
chunks: "all",
test: /[\\/]src[\\/]js[\\/]/,//也可以值文件/[\\/]src[\\/]js[\\/].*\.js/,
name: "common", //生成文件名,依据output规则
minChunks: 1,
maxInitialRequests: 5,
minSize: 0,
priority: 1
}
}
}
},
cache: {
type: "filesystem",
allowCollectingMemory: true,
},
plugins: [
AutoImport.default({
imports: ["react","react-router-dom"],
}),
// 编译环境变量
new webpack.DefinePlugin(GLOBALS),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional eea1d28b685828b67788
filename: "css/[name]-[hash].css?v=[chunkhash]",
chunkFilename: "css/[id]-[hash].css?v=[chunkhash]"
}),
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, "src/public/assets"),
to: path.resolve(__dirname, "dist/assets"),
// ignore: ['.*']
}
]
}),
new HtmlWebpackPlugin({
template: "src/index.html",
filename: "index.html",
// title: '阿布云首页',
// chunks: ['vendor','index'],
// excludeChunks: ['app'],
favicon: "src/favicon.ico",
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
},
inject: true,
// Note that you can add custom options here if you need to handle other custom logic in index.html
// To track JavaScript errors via TrackJS, sign up for a free trial at TrackJS.com and enter your token below.
trackJSToken: ""
}),
],
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: [{ loader: "babel-loader", options: { cacheDirectory: true } }]
},
{
test: /\.eot(\?v=\d+.\d+.\d+)?$/,
type: "asset/resource",
generator: {
filename: "[name].[ext]"
}
// use: [
// {
// loader: 'url-loader',
// options: {
// name: '[name].[ext]'
// }
// }
// ]
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
type: "asset/resource",
generator: {
filename: "[name].[ext]"
}
// use: [
// {
// loader: 'url-loader',
// options: {
// limit: 10000,
// mimetype: 'application/font-woff',
// name: '[name].[ext]'
// }
// }
// ]
},
{
test: /\.[ot]tf(\?v=\d+.\d+.\d+)?$/,
type: "asset/resource",
generator: {
filename: "[name].[ext]"
}
// use: [
// {
// loader: 'url-loader',
// options: {
// limit: 10000,
// mimetype: 'application/octet-stream',
// name: '[name].[ext]'
// }
// }
// ]
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
type: "javascript/auto",
generator: {
filename: "[name].[ext]"
}
// use: [
// {
// loader: 'url-loader',
// options: {
// limit: 10000,
// mimetype: 'image/svg+xml',
// name: '[name].[ext]'
// }
// }
// ]
},
{
test: /\.(jpe?g|png|gif|ico)$/i,
type: "asset/resource",
generator: {
filename: "[name].[ext]"
}
// use: [
// {
// // loader: 'file-loader',
// type: 'asset/resource',
// generator: {
// filename: 'assets/[hash][ext][query]'
// }
// // options: {
// // name: '[name].[ext]'
// // }
// }
// ]
},
{
test: /\.css|\.scss$/,
// exclude: /node_modules/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
sourceMap: false,
url: false
// importLoaders: 2,
// modules: {
// auto: (resourcePath) => resourcePath.endsWith('.scss'),
// localIdentName: '[path][name]__[local]--[hash:base64:5]'
// },
}
}, {
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [
["autoprefixer",{/*options*/}],
require("postcss-pxtorem")({
rootValue: 16,
unitPrecision: 5,
propList: ["*"],
selectorBlackList: [],
replace: true,
mediaQuery: false,
minPixelValue: 0
})
],
sourceMap: false
}
}
}, {
loader: "sass-loader",
options: {
sassOptions: {
// webpackImporter: false,
indentWidth: 4,
includePaths: [path.resolve(__dirname, "src", "scss"),"node_modules"],
},
}
// loader: 'less-loader',
// options: {
// lessOptions: {
// paths: [path.resolve(__dirname, 'src'), path.resolve(__dirname, 'node_modules')],
// javascriptEnabled: true,
// sourceMap: false
// }
// }
}
]
}
]
}
};
module.exports = config;