-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
701 lines (622 loc) · 18.6 KB
/
webpack.config.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
const path = require('path')
const fs = require('fs')
const webpack = require('webpack')
// webpack配置项:https://webpack.js.org/configuration/
class Webpack5RecommendConfig {
constructor(env, argv, options) {
let cwd = process.cwd()
let isTsProject = fs.existsSync(path.join(cwd, 'tsconfig.json'))
this.mode = argv.mode || 'development'
this.isProduction = this.mode === 'production'
let _options = {
cwd: cwd, // 当前运行webpack所在位置
srcPath: path.resolve(cwd, 'src'), // 源码目录文件位置
distPath: path.resolve(cwd, 'dist'),// 输出文件位置
publicPath: '/', // 发布时URL访问路径
packageJSON: require(path.join(cwd, 'package.json')), // package.json文件信息对象
isTsProject: isTsProject, // 是否为ts项目
isEntryJSX: false, // 定义入口文件是否是JSX或者TSX
scriptExt: isTsProject ? '.ts' : '.js', // 入口脚本扩展名称
entryDefaultName: 'main', // 入口默认名,webpack默认入口为index.js,输出为main.js
entryDefaultFileName: null, // // 入口文件默认名称
staticFolderPath: path.join(cwd, 'public'), // 静态文件public目录
title: 'Webpack App', // 主页标题
enableProfile: false, // 是否统计并打印webpack打包详细信息
enableProxy: false, // 是否启用代理配置
enableThread: false, // 是否启用多线程
enableBabel: this.isProduction, // 默认生产环境进行babel编译,如果你使用React JSX那么需要永久启用并添加@babel/preset-react
enableHash: true, // 是否启用HASH
emitCss: this.isProduction, // 是否分离css
emitHtml: true, // 是否弹出HTML文件
emitPublic: true, // 是否复制public中静态文件
isSplitChunk: true, // 是否做代码Chunk切分
libraryName: false, // 是否作为库函数进行发布
externals: [], // 需要做排除的库,目前支持react
skipCheckBabel: false // 跳过babel编译检查
}
if (Array.isArray(options)) {
if (options.length === 1) {
Object.assign(_options, options[0])
} else if (options.length === 3) {
if (this.isProduction) {
Object.assign(_options, options[0], options[1]) // 生产配置
} else {
Object.assign(_options, options[0], options[2]) // 开发配置
}
} else {
throw TypeError('Incorrect number of options parameters.')
}
} else if (typeof options === 'object') {
Object.assign(_options, options)
}
this.cwd = _options.cwd
this.srcPath = _options.srcPath
this.distPath = _options.distPath
this.publicPath = _options.publicPath
this.packageJSON = _options.packageJSON
this.dependencies = Object.keys({ // 项目依赖库数组,用于判定包含什么框架
...this.packageJSON['devDependencies'],
...this.packageJSON['dependencies']
})
this.isTsProject = _options.isTsProject
this.isEntryJSX = _options.isEntryJSX
this.scriptExt = _options.scriptExt
if (this.isEntryJSX === true || this.isEntryJSX === 'auto' && this.isInclude('react')) {
this.scriptExt += 'x'
}
this.entryDefaultName = _options.entryDefaultName
this.entryDefaultFileName = _options.entryDefaultFileName || `${this.entryDefaultName}${this.scriptExt}`
this.staticFolderPath = _options.staticFolderPath
this.enableHash = _options.enableHash
this.enableProfile = _options.enableProfile
this.enableProxy = _options.enableProxy
this.enableThread = _options.enableThread
this.enableBabel = _options.enableBabel
this.skipCheckBabel = _options.skipCheckBabel
if (this.isProduction) {
this.checkEnableBabel()
}
if (!this.isTsProject && this.isInclude('react')) {
this.checkEnableBabel()
}
if (this.enableBabel) {
this.checkBabelCompileReact()
}
this.title = _options.title
this.emitCss = _options.emitCss
this.emitHtml = _options.emitHtml
this.emitPublic = _options.emitPublic
this.isSplitChunk = _options.isSplitChunk
this.externals = _options.externals
this.libraryName = _options.libraryName
if (this.libraryName === true) {
this.libraryName = this.camelCase(this.packageJSON['name']) || 'library'
}
this._config = {}
}
static buildLibraryOptions(libraryName) {
return [
{
emitCss: false,
emitHtml: false,
libraryName: libraryName || true,
isSplitChunk: false,
enableHash: false
},
{emitPublic: false},
null
]
}
static buildReactLibraryOptions(componentName) {
let options = Webpack5RecommendConfig.buildLibraryOptions(componentName)
options[0].externals = ['react']
return options
}
static newLibrary(env, argv, libraryName) {
return new Webpack5RecommendConfig(env, argv, Webpack5RecommendConfig.buildLibraryOptions(libraryName))
}
static newReactLibrary(env, argv, componentName) {
return new Webpack5RecommendConfig(env, argv, Webpack5RecommendConfig.buildReactLibraryOptions(componentName))
}
build(buildCallback) {
// webpack5配置文档:https://webpack.js.org/configuration/
this.buildBasic()
this.buildInsAndOuts()
this.buildExternals()
this.buildResolve()
this.buildDevServer()
this.buildImprove()
this.buildRules()
this.buildPlugins()
if (buildCallback) {
buildCallback.call(this, this._config)
}
return this
}
buildBasic() {
this._config.mode = this.mode
this._config.stats = 'errors-only'
this._config.devtool = this.isProduction ? false : 'eval-source-map'
this._config.context = this.cwd
if (!this.isProduction) {
this._config.target = 'web' // 默认值:browserslist
}
return this
}
buildInsAndOuts() {
this._config.entry = {
[this.entryDefaultName]: path.join(this.srcPath, this.entryDefaultFileName)
}
this._config.output = {
path: this.distPath,
publicPath: this.publicPath,
compareBeforeEmit: false,
iife: true,
clean: true
}
if (this.enableHash) {
Object.assign(this._config.output, {
filename: '[name].bundle.[chunkhash:8].js',
chunkFilename: '[name].chunk.[chunkhash:8].js',
assetModuleFilename: 'assets/[name][hash:8][ext]'
})
} else {
Object.assign(this._config.output, {
filename: '[name].bundle.js',
chunkFilename: '[name].chunk.js',
assetModuleFilename: 'assets/[name][ext]'
})
}
// https://webpack.js.org/configuration/output/#outputlibrary
if (this.libraryName) {
this._config.output.globalObject = 'this'
this._config.output.library = {
name: this.libraryName,
// https://webpack.js.org/configuration/output/#outputlibrarytype
type: 'umd2',
export: 'default',
umdNamedDefine: true,
auxiliaryComment: {
root: 'Root Export',
commonjs: 'CommonJS Export',
commonjs2: 'CommonJS2 Export',
amd: 'AMD Export'
}
}
}
return this
}
buildExternals() {
// https://webpack.js.org/configuration/externals/
this._config.externals = {}
if (this.externals.includes('react')) {
Object.assign(this._config.externals, {
react: {
root: 'React',
commonjs: 'react',
commonjs2: 'react',
amd: 'react'
},
'react-dom': {
root: 'ReactDOM',
commonjs: 'react-dom',
commonjs2: 'react-dom',
amd: 'react-dom'
}
})
}
return this
}
buildResolve() {
this._config.resolve = {
extensions: ['.js'],
alias: {
'@': path.join(this.cwd, 'src')
}
}
if (this.isTsProject) {
this._config.resolve.extensions.push('.ts')
}
if (this.isInclude('react')) {
this._config.resolve.extensions.push('.jsx')
if (this.isTsProject) {
this._config.resolve.extensions.push('.tsx')
}
}
if (this.isInclude('vue')) {
this._config.resolve.extensions.push('.vue')
}
return this
}
buildDevServer() {
// https://webpack.js.org/configuration/dev-server/
let port = 8080
this._config.devServer = {
allowedHosts: 'all',
historyApiFallback: false,
host: '0.0.0.0',
liveReload: true,
hot: false, // HMR(Hot Module Replacement),JS文件内需要调用accept()
open: [`http://localhost:${port}/`],
port: port,
magicHtml: false,
// https://github.com/webpack/webpack-dev-middleware
devMiddleware: {
stats: false
}
}
if (this.enableProxy) {
this._config.devServer.proxy = this.configProxy()
}
return this
}
buildImprove() {
this._config.performance = {
hints: 'warning',
maxAssetSize: (this.isProduction ? 3 : 30) * 1024 * 1024,
maxEntrypointSize: (this.isProduction ? 3 : 30) * 1024 * 1024
}
this._config.optimization = {}
if (this.isSplitChunk) {
/**
* https://webpack.js.org/plugins/split-chunks-plugin/#configuration
*
* 产生chunk的3种方式
* 1. 手动设置规则进行切割
* 2. 多个入口文件会被当成多个Chunk处理
* 3. 使用import()进行异步导入
*/
this._config.optimization.splitChunks = {
/**
* all: 所有方式引入的Module中的符合手动切割Chunk规则的Module都会被解析分离
* initial: 异步引入的Module中符合手动切割Chunk规则的Module不做解析分离
* async: 同步引入的Module中符合手动切割Chunk规则的Module不做解析分离
*/
chunks: 'all',
automaticNameDelimiter: '~',
cacheGroups: this.getSplitChunksGroup()
}
if (this.isProduction) {
this._config.optimization.runtimeChunk = 'single'
}
}
Object.assign(this._config.optimization, {
minimize: this.isProduction,
minimizer: [
new (require('terser-webpack-plugin'))() //
]
})
return this
}
buildRules() {
this._config.module = {rules: []}
/**
* 添加js解析
*/
let jsUse = this.enableBabel ? ['babel-loader'] : []
if (this.enableThread) {
jsUse.unshift('thread-loader')
}
this._config.module.rules.push({
test: /\.js$/,
exclude: /[\\/]node_modules[\\/]/,
use: jsUse
})
/**
* 添加jsx解析
*/
let jsxUse = this.enableBabel ? ['babel-loader'] : []
if (this.enableThread) {
jsxUse.unshift('thread-loader')
}
this._config.module.rules.push({
test: /\.jsx$/,
use: jsxUse
})
if (this.isTsProject) {
/**
* 添加ts/tsx解析
*/
let tsUse = this.enableBabel ? ['babel-loader'] : []
let jsx = 'preserve'
if (!this.enableBabel && this.isInclude('react')) {
jsx = 'react-jsxdev'
}
tsUse.push({
loader: 'ts-loader',
options: {
// https://github.com/TypeStrong/ts-loader#happypackmode
happyPackMode: this.enableThread,
transpileOnly: true,
compilerOptions: {
jsx: jsx,
noEmit: true
}
}
})
if (this.enableThread) {
tsUse.unshift('thread-loader')
}
this._config.module.rules.push({
test: /\.tsx?$/,
use: tsUse
})
}
/**
* 添加css解析
*/
this._config.module.rules.push({
test: /\.css$/,
use: this.getCssLoader()
})
/**
* 添加sass解析
*/
if (this.isInclude('sass')) {
this._config.module.rules.push({
test: /\.s[ca]ss$/,
use: this.getCssLoader('sass')
})
}
/**
* 添加sass解析
*/
if (this.isInclude('less')) {
this._config.module.rules.push({
test: /\.less$/,
use: this.getCssLoader('less')
})
}
/**
* https://webpack.js.org/guides/asset-modules/
*
* 添加图片资源解析
*/
this._config.module.rules.push({
test: /\.(png|jpe?g|gif|svg)$/,
type: 'asset',
generator: {
filename: this.enableHash ? 'images/[name].[hash:8][ext]' : 'images/[name][ext]'
}
})
/**
* https://webpack.js.org/guides/asset-modules/
*
* 添加媒体资源解析
*/
this._config.module.rules.push({
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
type: 'asset',
generator: {
filename: this.enableHash ? 'media/[name].[hash:8][ext]' : 'images/[name][ext]'
}
})
/**
* https://webpack.js.org/guides/asset-modules/
*
* 添加字体资源解析
*/
this._config.module.rules.push({
test: /\.(woff|woff2|eot|ttf|otf)$/,
type: 'asset',
generator: {
filename: this.enableHash ? 'font/[name].[hash:8][ext]' : 'images/[name][ext]'
}
})
return this
}
buildPlugins() {
this._config.plugins = []
if (this.isProduction) {
/**
* 将 CSS 提取到单独的文件中
* https://webpack.js.org/plugins/mini-css-extract-plugin/
*/
this._config.plugins.push(
new (require('mini-css-extract-plugin'))({
filename: this.enableHash ? '[name].style.[chunkhash:8].css' : '[name].style.css'
})
)
}
/**
* 自动添加HTML插件
* https://github.com/jantimon/html-webpack-plugin
*/
if (this.emitHtml) {
let htmWebpackPluginOptions = {
title: this.title,
filename: 'index.html',
inject: 'body',
scriptLoading: 'defer', // 'blocking'|'defer'|'module'
hash: false,
// https://github.com/terser/html-minifier-terser
minify: this.isProduction
? {
removeComments: true,
collapseWhitespace: true,
minifyCSS: true
}
: false
}
this._config.plugins.push(new (require('html-webpack-plugin'))(htmWebpackPluginOptions))
}
if (this.emitPublic && fs.existsSync(this.staticFolderPath)) {
/**
* 将已存在的单个文件或整个目录复制到构建目录
* https://webpack.js.org/plugins/copy-webpack-plugin
*/
this._config.plugins.push(
new (require('copy-webpack-plugin'))({
patterns: [
{
from: this.staticFolderPath,
to: '.'
}
]
})
)
}
if (this.enableProfile) {
/**
* Elegant ProgressBar and Profiler for Webpack 3 , 4 and 5
* https://github.com/unjs/webpackbar
*/
this._config.plugins.push(
new (require('webpackbar'))({
reporters: ['fancy', 'profile'],
profile: true
})
)
} else {
this._config.plugins.push(new (require('webpackbar'))())
}
/**
* 允许在编译时配置全局常量
* https://webpack.js.org/plugins/define-plugin
*/
this._config.plugins.push(
new webpack.DefinePlugin({
webpack5RecommendConfigOptions: {
author: '"SystemLight"'
}
})
)
/**
* 在监视模式下忽略指定的文件
* https://webpack.js.org/plugins/watch-ignore-plugin/
*/
let ignorePaths = []
if (this.isTsProject) {
// https://github.com/TypeStrong/ts-loader#usage-with-webpack-watch
ignorePaths.push(/\.js$/, /\.d\.ts$/)
this._config.plugins.push(
new webpack.WatchIgnorePlugin({
paths: ignorePaths
})
)
}
return this
}
checkEnableBabel() {
if (!this.skipCheckBabel && !this.enableBabel) {
throw TypeError('Please start Babel in the production environment to compile.')
}
}
checkBabelCompileReact() {
if (this.isInclude('react') && !this.isInclude('@babel/preset-react')) {
throw TypeError('Please add and configure @babel/preset-react in Babel to compile the react project.')
}
}
camelCase(content) {
if (!content) {
return ''
}
return content.split('/').slice(-1)[0].replace(/-(\w)/g, (_, $1) => $1.toUpperCase())
}
isInclude(libraryName) {
return this.dependencies.includes(libraryName)
}
getSplitChunksGroup() {
// 内置定义Chunk切割分离规则
let cacheGroups = {
common: {
name: 'common',
minChunks: 2,
reuseExistingChunk: true,
priority: -20
},
vendors: {
name: 'vendors',
test: /[\\/]node_modules[\\/]/,
priority: -10
}
}
if (this.dependencies.includes('vue')) {
Object.assign(cacheGroups, {
vue: {
name: 'vue',
test: /[\\/]node_modules[\\/](vue|vue-router|vuex)/,
chunks: 'all',
enforce: true
}
})
}
if (this.dependencies.includes('element-ui')) {
Object.assign(cacheGroups, {
elementUI: {
name: 'element-ui',
test: /[\\/]node_modules[\\/](element-ui)/,
chunks: 'all'
}
})
}
if (this.dependencies.includes('react')) {
Object.assign(cacheGroups, {
react: {
name: 'react',
test: /[\\/]node_modules[\\/](react|react-dom|scheduler|prop-types)/,
chunks: 'all'
}
})
}
if (this.dependencies.includes('antd')) {
cacheGroups = Object.assign(cacheGroups, {
antd: {
name: 'antd',
test: /[\\/]node_modules[\\/](@ant-design|antd)/,
chunks: 'all'
}
})
}
return cacheGroups
}
getCssLoader(cssPreprocessing) {
let cssRuleLoaders = []
if (this.emitCss) {
cssRuleLoaders.push(require('mini-css-extract-plugin').loader)
} else {
cssRuleLoaders.push('style-loader')
}
cssRuleLoaders.push('css-loader')
if (this.isProduction && this.isInclude('postcss-loader')) {
cssRuleLoaders.push('postcss-loader')
}
switch (cssPreprocessing) {
case 'sass':
cssRuleLoaders.push('sass-loader')
break
case 'less':
cssRuleLoaders.push('less-loader')
break
}
return cssRuleLoaders
}
configProxy() {
return {
// https://github.com/chimurai/http-proxy-middleware
'/api': {
target: 'http://localhost:5000/api',
changeOrigin: true,
secure: false
}
}
}
toConfig(debug) {
if (debug) {
console.log(this._config)
}
return this._config
}
}
module.exports = (env, argv) => Webpack5RecommendConfig
.newLibrary(env, argv)
.build(function (config) {
config.devtool = false
config.externals['spark-md5'] = {
root: 'SparkMD5',
commonjs: 'spark-md5',
commonjs2: 'spark-md5',
amd: 'spark-md5'
}
})
.toConfig()