|
1 | 1 | - **Type:** `Object | undefined`
|
2 | 2 |
|
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. |
4 | 4 |
|
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: |
9 | 6 |
|
10 | 7 | ```js
|
11 |
| -{ |
| 8 | +const defaultConfig = { |
12 | 9 | analyzerMode: 'static',
|
13 | 10 | openAnalyzer: false,
|
14 |
| - // `target` is the compilation target, such as `web`, `node`, etc. |
| 11 | + // target is the compilation target, such as `web`, `node`, etc. |
15 | 12 | 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 | +}; |
17 | 34 | ```
|
18 | 35 |
|
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 | + |
| 45 | + |
| 46 | +### Override Default Configuration |
| 47 | + |
| 48 | +You can override the default configuration through `performance.bundleAnalyze`, such as enabling the server mode: |
20 | 49 |
|
21 | 50 | ```js
|
22 | 51 | export default {
|
23 | 52 | 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 | + : {}, |
28 | 59 | },
|
29 | 60 | };
|
30 | 61 | ```
|
| 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. |
0 commit comments