Skip to content

Commit afaf4e2

Browse files
docs(builder): add more guides for bundle analyzer (#3758)
Co-authored-by: yangxingyuan <[email protected]>
1 parent f0d3c2d commit afaf4e2

File tree

4 files changed

+119
-33
lines changed

4 files changed

+119
-33
lines changed
Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,75 @@
11
- **Type:** `Object | undefined`
22

3-
You have two ways to enable `webpack-bundle-analyzer` to analyze the size of output files:
3+
Used to enable the [webpack-bundle-analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer) plugin to analyze the size of the output.
44

5-
- Add environment variable `BUNDLE_ANALYZE=true`.
6-
- Add `performance.bundleAnalyze` config.
7-
8-
By default, `webpack-bundle-analyzer` is not enabled. When enabled, its configuration is as follows:
5+
By default, Builder does not enable `webpack-bundle-analyzer`. When this feature is enabled, the default configuration is as follows:
96

107
```js
11-
{
8+
const defaultConfig = {
129
analyzerMode: 'static',
1310
openAnalyzer: false,
14-
// `target` is the compilation target, such as `web`, `node`, etc.
11+
// target is the compilation target, such as `web`, `node`, etc.
1512
reportFilename: `report-${target}.html`,
16-
}
13+
};
14+
```
15+
16+
### Enable Bundle Analyze
17+
18+
You have two ways to enable `webpack-bundle-analyzer` to analyze the size of the output files:
19+
20+
- Add the environment variable `BUNDLE_ANALYZE=true`, for example:
21+
22+
```bash
23+
BUNDLE_ANALYZE=true pnpm build
24+
```
25+
26+
- Configure `performance.bundleAnalyze` to enable it permanently:
27+
28+
```js
29+
export default {
30+
performance: {
31+
bundleAnalyze: {},
32+
},
33+
};
1734
```
1835

19-
You can override the default config through `performance.bundleAnalyze`, for example:
36+
After enabling it, Builder will generate an HTML file that analyzes the size of the output files, and print the following log in the Terminal:
37+
38+
```bash
39+
Webpack Bundle Analyzer saved report to /Project/my-project/dist/report-web.html
40+
```
41+
42+
You can manually open the file in the browser and view the detail of the bundle size. When an area is larger, it indicates that its corresponding bundle size is larger.
43+
44+
![](https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/mwa-build-analyze-8784f762c1ab0cb20935829d5f912c4c.png)
45+
46+
### Override Default Configuration
47+
48+
You can override the default configuration through `performance.bundleAnalyze`, such as enabling the server mode:
2049

2150
```js
2251
export default {
2352
performance: {
24-
bundleAnalyze: {
25-
analyzerMode: 'server',
26-
openAnalyzer: true,
27-
},
53+
bundleAnalyze: process.env.BUNDLE_ANALYZE
54+
? {
55+
analyzerMode: 'server',
56+
openAnalyzer: true,
57+
}
58+
: {},
2859
},
2960
};
3061
```
62+
63+
### Size Types
64+
65+
In the `webpack-bundle-analyzer` panel, you can control size types in the upper left corner (default is `Parsed`):
66+
67+
- `Stat`: The size obtained from the `stats` object of the bundler, which reflects the size of the code before minification.
68+
- `Parsed`: The size of the file on the disk, which reflects the size of the code after minification.
69+
- `Gzipped`: The file size requested in the browser reflects the size of the code after minification and gzip.
70+
71+
### Notes
72+
73+
1. Enabling the server mode will cause the `build` process to not exit normally.
74+
2. Enabling `bundleAnalyzer` will reduce build speed. Therefore, this configuration should not be enabled during daily development, and it is recommended to enable it on demand through the `BUNDLE_ANALYZE` environment variable.
75+
3. Since no code minification and other optimizations are performed in the `dev` phase, the real output size cannot be reflected, so it is recommended to analyze the output size in the `build` phase.

packages/document/builder-doc/docs/en/guide/optimization/optimize-bundle.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@ npx yarn-deduplicate && yarn
3030

3131
It is recommended to using lightweight libraries in your project, such as replacing [moment](https://momentjs.com/) with [day.js](https://day.js.org/).
3232

33-
If you want to find out the large libraries in the project, you can add the [BUNDLE_ANALYZE=true](/en/api/config-performance.html#performancebundleanalyze) environment variable when building:
33+
If you want to find out the large libraries in the project, you can add the `BUNDLE_ANALYZE=true` environment variable when building:
3434

3535
```bash
3636
BUNDLE_ANALYZE=true pnpm build
3737
```
3838

39-
After adding this parameter, Builder will generate a HTML file that analyzes the bundle size, you can manually open the file in the browser and view the detail of the bundle size. When an area is larger, it indicates that its corresponding bundle size is larger.
40-
41-
![](https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/mwa-build-analyze-8784f762c1ab0cb20935829d5f912c4c.png)
39+
See the [performance.bundleAnalyze](/api/config-performance.html#performancebundleanalyze) configuration for details.
4240

4341
## Adjust Browserslist
4442

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,75 @@
11
- **类型:** `Object | undefined`
22

3-
你有两种方式开启 `webpack-bundle-analyzer` 来分析构建产物的体积:
4-
5-
- 添加环境变量 `BUNDLE_ANALYZE=true`
6-
- 配置 `performance.bundleAnalyze`
3+
用于开启 [webpack-bundle-analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer) 插件来分析产物体积。
74

8-
默认情况下,不会开启 `webpack-bundle-analyzer`,当开启之后它的配置如下:
5+
默认情况下,Builder 不会开启 `webpack-bundle-analyzer`。当开启该功能后,内部的默认配置如下:
96

107
```js
11-
{
8+
const defaultConfig = {
129
analyzerMode: 'static',
1310
openAnalyzer: false,
1411
// target 为编译目标,如 `web`、`node` 等
1512
reportFilename: `report-${target}.html`,
16-
}
13+
};
14+
```
15+
16+
### 启用 Bundle Analyze
17+
18+
你有两种方式开启 `webpack-bundle-analyzer` 来分析构建产物的体积:
19+
20+
- 添加环境变量 `BUNDLE_ANALYZE=true`,比如:
21+
22+
```bash
23+
BUNDLE_ANALYZE=true pnpm build
24+
```
25+
26+
- 配置 `performance.bundleAnalyze` 来固定开启:
27+
28+
```js
29+
export default {
30+
performance: {
31+
bundleAnalyze: {},
32+
},
33+
};
1734
```
1835

19-
你可以通过 `performance.bundleAnalyze` 来覆盖默认配置,比如:
36+
在启用后,Builder 会生成一个分析构建产物体积的 HTML 文件,并在 Terminal 中打印以下日志:
37+
38+
```bash
39+
Webpack Bundle Analyzer saved report to /Project/my-project/dist/report-web.html
40+
```
41+
42+
手动在浏览器中打开该文件,可以看到打包产物的瓦片图;区块的面积越大,说明该模块的体积越大。
43+
44+
![](https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/mwa-build-analyze-8784f762c1ab0cb20935829d5f912c4c.png)
45+
46+
### 覆盖默认配置
47+
48+
你可以通过 `performance.bundleAnalyze` 来覆盖默认配置,比如开启 `server` 模式:
2049

2150
```js
2251
export default {
2352
performance: {
24-
bundleAnalyze: {
25-
analyzerMode: 'server',
26-
openAnalyzer: true,
27-
},
53+
bundleAnalyze: process.env.BUNDLE_ANALYZE
54+
? {
55+
analyzerMode: 'server',
56+
openAnalyzer: true,
57+
}
58+
: {},
2859
},
2960
};
3061
```
62+
63+
### Size 类型
64+
65+
`webpack-bundle-analyzer` 的面板中,你可以在左上角控制 Size 类型(默认为 `Parsed`):
66+
67+
- `Stat`:从打包工具的 `stats` 对象中获取的体积,它反映了代码在压缩之前的体积。
68+
- `Parsed`:磁盘上的文件体积,它反映了代码在压缩之后的体积。
69+
- `Gzipped`:浏览器里请求的文件体积,它反映了代码在压缩和 gzip 后的体积。
70+
71+
### 注意事项
72+
73+
1. 开启 Server 模式会导致 `build` 进程不能正常退出。
74+
2. 开启 bundleAnalyzer 会降低构建性能。因此,在日常开发过程中不应该开启此配置项,建议通过 `BUNDLE_ANALYZE` 环境变量来按需开启。
75+
3. 由于 `dev` 阶段不会进行代码压缩等优化,无法反映真实的产物体积,因此建议在 `build` 阶段分析产物体积。

packages/document/builder-doc/docs/zh/guide/optimization/optimize-bundle.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@ npx yarn-deduplicate && yarn
3030

3131
建议将项目中体积较大的三方库替换为更轻量的库,比如将 [moment](https://momentjs.com/) 替换为 [day.js](https://day.js.org/)
3232

33-
如果你需要找出项目中体积较大的三方库,可以在执行构建时添加 [BUNDLE_ANALYZE=true](/api/config-performance.html#performancebundleanalyze) 环境变量:
33+
如果你需要找出项目中体积较大的三方库,可以在执行构建时添加 `BUNDLE_ANALYZE=true` 环境变量:
3434

3535
```bash
3636
BUNDLE_ANALYZE=true pnpm build
3737
```
3838

39-
添加该参数后,Builder 会生成一个分析构建产物体积的 HTML 文件,手动在浏览器中打开该文件,可以看到打包产物的瓦片图。区块的面积越大,说明该模块的体积越大。
40-
41-
![](https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/mwa-build-analyze-8784f762c1ab0cb20935829d5f912c4c.png)
39+
详见 [performance.bundleAnalyze](/api/config-performance.html#performancebundleanalyze) 配置项。
4240

4341
## 提升 Browserslist 范围
4442

0 commit comments

Comments
 (0)