This repository has been archived by the owner on Jul 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.bundle.prod.js
102 lines (93 loc) · 2.58 KB
/
webpack.bundle.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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* This Source Code Form is “Incompatible With Secondary Licenses”, as
* defined by the Mozilla Public License, v. 2.0.
*/
/* eslint-disable @typescript-eslint/no-var-requires */
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const path = require('path')
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
const merge = require('webpack-merge')
const { StatsWriterPlugin } = require('webpack-stats-plugin')
const VENDORS_CHUNKNAME = 'vendors'
const bundleProdConfig = {
mode: 'production',
entry: ['./src/client/index.tsx'],
output: {
filename: '[name].[contenthash].js',
path: path.join(__dirname, 'build/assets'),
},
module: {
rules: [
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader],
exclude: /node_modules/,
},
],
},
optimization: {
moduleIds: 'hashed',
runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: VENDORS_CHUNKNAME,
chunks: 'all',
},
},
},
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
}),
new StatsWriterPlugin({
filename: '../dynamicAssets.json',
stats: {
all: false,
assets: true,
},
transform: ({ assetsByChunkName }) => {
const TARGET_CHUNKNAMES = ['main', 'runtime', VENDORS_CHUNKNAME]
const scripts = []
const styles = []
const includeAsset = asset => {
if (/.+\.js$/.test(asset)) {
scripts.push(asset)
} else if (/.+\.css$/.test(asset)) {
styles.push(asset)
}
}
Object.entries(assetsByChunkName).forEach(([key, value]) => {
if (TARGET_CHUNKNAMES.includes(key)) {
if (Array.isArray(value)) {
value.forEach(includeAsset)
} else {
includeAsset(value)
}
}
})
return JSON.stringify({ scripts, styles })
},
}),
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
reportFilename: '../report.html',
}),
],
stats: {
children: false,
entrypoints: false,
modules: false,
},
}
module.exports = merge.smartStrategy({ 'module.rules.use': 'prepend' })(
require('./webpack.bundle.base'),
bundleProdConfig,
)