Skip to content

Commit 129e615

Browse files
committed
add build config, add PackingGenerateFilePlugin
1 parent 7626ebf commit 129e615

File tree

6 files changed

+58
-9
lines changed

6 files changed

+58
-9
lines changed

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"antd": "^4.12.3",
1616
"classnames": "^2.2.6",
1717
"connected-react-router": "^6.8.0",
18+
"lodash": "^4.17.21",
1819
"react": "^17.0.1",
1920
"react-dom": "^17.0.1",
2021
"react-redux": "^7.2.2",
@@ -24,7 +25,7 @@
2425
"redux-logger": "^3.0.6",
2526
"redux-promise": "^0.6.0",
2627
"redux-thunk": "^2.3.0",
27-
"typescript": "^4.1.3"
28+
"typescript": "4.1.3"
2829
},
2930
"devDependencies": {
3031
"@babel/core": "^7.12.17",
@@ -59,6 +60,7 @@
5960
"html-webpack-plugin": "^5.2.0",
6061
"less": "^4.1.1",
6162
"less-loader": "^8.0.0",
63+
"lodash.isequalwith": "^4.4.0",
6264
"mini-css-extract-plugin": "^1.3.8",
6365
"postcss-loader": "^5.0.0",
6466
"prettier": "^2.2.1",

scripts/helper.js

+17
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,24 @@ function getProcessEnv() {
3535
return result;
3636
}
3737

38+
function processSize(size) {
39+
let idx = 0;
40+
let sizeUnit = ['Byte', 'Kb', 'Mb', 'Gb'];
41+
while (size > 1024) {
42+
size = size / 1024;
43+
idx++;
44+
}
45+
46+
return {
47+
size: parseFloat(Number(size).toFixed(2)),
48+
unit: sizeUnit[idx],
49+
};
50+
}
51+
52+
53+
3854
module.exports = {
3955
getProcessEnv,
56+
processSize,
4057
};
4158

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const helper = require('../helper');
2+
3+
class PackingGenerateFilePlugin {
4+
apply(compiler) {
5+
compiler.hooks.emit.tap('PackingGenerateFilePlugin', (compilation) => {
6+
let assets = compilation.assets;
7+
let contentList = [];
8+
// statObj.size() 的单位是 bit
9+
Object.entries(assets).forEach(([filename, statObj]) => {
10+
const { size, unit } = helper.processSize(statObj.size())
11+
contentList.push({filename, size: `${size}${unit}` });
12+
});
13+
console.table(contentList);
14+
});
15+
}
16+
}
17+
18+
module.exports = PackingGenerateFilePlugin;

scripts/webpack.config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ module.exports = {
3434
plugins: [
3535
new webpack.DefinePlugin({'process.env': JSON.stringify(envObj)}),
3636
new webpack.EnvironmentPlugin(envObj),
37-
]
37+
],
38+
39+
stats: 'errors-only',
40+
3841
};
3942

scripts/webpack.config.prod.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const {CleanWebpackPlugin} = require('clean-webpack-plugin');
66
const TerserPlugin = require('terser-webpack-plugin');
77
const HtmlWebpackPlugin = require('html-webpack-plugin');
88

9+
const PackingGenerateFilePlugin = require('./plugins/PackingGenerateFilePlugin');
10+
911
const baseConfig = require('./webpack.config');
1012
const pkg = require('../package.json');
1113

@@ -16,7 +18,7 @@ module.exports = merge(baseConfig, {
1618

1719
output: {
1820
path: path.resolve(__dirname, '../dist'),
19-
filename: 'js/main-[name]-[contenthash:8].js'
21+
filename: 'js/[contenthash:8].js'
2022
},
2123

2224
devServer: {
@@ -80,6 +82,7 @@ module.exports = merge(baseConfig, {
8082
chunkFilename: 'css/[name].[contenthash:8].chunk.css',
8183
}),
8284
new webpack.PrefetchPlugin('react'),
85+
new PackingGenerateFilePlugin()
8386
],
8487

8588
optimization: {
@@ -105,6 +108,8 @@ module.exports = merge(baseConfig, {
105108
ascii_only: true,
106109
},
107110
},
111+
parallel: true,
112+
extractComments: false,
108113
}),
109114
],
110115
splitChunks: {
@@ -115,5 +120,4 @@ module.exports = merge(baseConfig, {
115120
name: entrypoint => `runtime-${entrypoint.name}`,
116121
},
117122
}
118-
119123
});

yarn.lock

+10-5
Original file line numberDiff line numberDiff line change
@@ -3929,7 +3929,12 @@ lodash-es@^4.2.1:
39293929
resolved "https://registry.npm.taobao.org/lodash-es/download/lodash-es-4.17.21.tgz#43e626c46e6591b7750beb2b50117390c609e3ee"
39303930
integrity sha1-Q+YmxG5lkbd1C+srUBFzkMYJ4+4=
39313931

3932-
lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.2.1:
3932+
lodash.isequalwith@^4.4.0:
3933+
version "4.4.0"
3934+
resolved "https://registry.npm.taobao.org/lodash.isequalwith/download/lodash.isequalwith-4.4.0.tgz#266726ddd528f854f21f4ea98a065606e0fbc6b0"
3935+
integrity sha1-Jmcm3dUo+FTyH06pigZWBuD7xrA=
3936+
3937+
lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.21, lodash@^4.2.1:
39333938
version "4.17.21"
39343939
resolved "https://registry.npm.taobao.org/lodash/download/lodash-4.17.21.tgz?cache=0&sync_timestamp=1613835860585&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Flodash%2Fdownload%2Flodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
39353940
integrity sha1-Z5WRxWTDv/quhFTPCz3zcMPWkRw=
@@ -6178,10 +6183,10 @@ type-is@~1.6.17, type-is@~1.6.18:
61786183
media-typer "0.3.0"
61796184
mime-types "~2.1.24"
61806185

6181-
typescript@^4.1.3:
6182-
version "4.1.5"
6183-
resolved "https://registry.npm.taobao.org/typescript/download/typescript-4.1.5.tgz?cache=0&sync_timestamp=1613978455569&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Ftypescript%2Fdownload%2Ftypescript-4.1.5.tgz#123a3b214aaff3be32926f0d8f1f6e704eb89a72"
6184-
integrity sha1-Ejo7IUqv874ykm8Njx9ucE64mnI=
6186+
6187+
version "4.1.3"
6188+
resolved "https://registry.npm.taobao.org/typescript/download/typescript-4.1.3.tgz?cache=0&sync_timestamp=1613978455569&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Ftypescript%2Fdownload%2Ftypescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"
6189+
integrity sha1-UZ1YK9lMugz4k0x9joRn5HP1O7c=
61856190

61866191
unicode-canonical-property-names-ecmascript@^1.0.4:
61876192
version "1.0.4"

0 commit comments

Comments
 (0)