Skip to content

Commit cdd0b6a

Browse files
authored
fix(core): upgrade webpack-bundle-analyzer to 5.3.0 (#7389)
1 parent 67c4f25 commit cdd0b6a

5 files changed

Lines changed: 91 additions & 71 deletions

File tree

e2e/cases/performance/bundle-analyzer/index.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,22 @@ test('should emit bundle analyze report correctly when build', async ({
3232
await rsbuild.expectLog('Webpack Bundle Analyzer saved report to');
3333
expect(filePaths.length).toBe(1);
3434
});
35+
36+
test('should surface stats file write errors when statsFilename points to a directory', async ({
37+
build,
38+
}) => {
39+
const rsbuild = await build({
40+
config: {
41+
performance: {
42+
bundleAnalyze: {
43+
generateStatsFile: true,
44+
statsFilename: '.',
45+
},
46+
},
47+
},
48+
});
49+
50+
await rsbuild.expectLog('Webpack Bundle Analyzer error saving stats file to');
51+
await rsbuild.expectLog('EISDIR');
52+
expect(rsbuild.buildError).toBeFalsy();
53+
});

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
"tinyglobby": "0.2.14",
101101
"typescript": "^5.9.3",
102102
"webpack": "^5.104.1",
103-
"webpack-bundle-analyzer": "^4.10.2",
103+
"webpack-bundle-analyzer": "^5.3.0",
104104
"webpack-merge": "6.0.1",
105105
"ws": "^8.18.3"
106106
},

packages/core/prebundle.config.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,34 @@ export default {
116116
webpack: 'webpack',
117117
},
118118
afterBundle(task) {
119+
const distDir = join(task.distPath, 'dist');
120+
const bundledEntryPath = join(task.distPath, 'index.js');
121+
const distEntryPath = join(distDir, 'index.js');
122+
123+
// Keep the package entry at the root, but execute the bundled file from
124+
// `dist/index.js` so webpack-bundle-analyzer still resolves `../public`
125+
// relative to its own package directory.
126+
fs.mkdirSync(distDir, { recursive: true });
127+
fs.renameSync(bundledEntryPath, distEntryPath);
128+
fs.copyFileSync(
129+
join(task.distPath, 'package.json'),
130+
join(distDir, 'package.json'),
131+
);
132+
fs.writeFileSync(
133+
bundledEntryPath,
134+
"module.exports = require('./dist/index.js');\n",
135+
);
136+
119137
// webpack type does not exist, use `@rspack/core` instead
120138
replaceFileContent(join(task.distPath, 'index.d.ts'), (content) =>
121139
content.replace("from 'webpack'", 'from "@rspack/core"'),
122140
);
141+
142+
// Static report generation in webpack-bundle-analyzer v5 resolves
143+
// `public/viewer.js` from the package root at runtime.
144+
fs.cpSync(join(task.depPath, 'public'), join(task.distPath, 'public'), {
145+
recursive: true,
146+
});
123147
},
124148
},
125149
{

packages/core/tests/__snapshots__/bundleAnalyzer.test.ts.snap

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ exports[`plugin-bundle-analyze > should add bundle analyze plugin 1`] = `
77
"name": "RsbuildCorePlugin",
88
},
99
BundleAnalyzerPlugin {
10+
"compiler": null,
1011
"logger": Logger {
1112
"activeLevels": Set {
1213
"info",
@@ -20,6 +21,7 @@ exports[`plugin-bundle-analyze > should add bundle analyze plugin 1`] = `
2021
"analyzerMode": "static",
2122
"analyzerPort": 8888,
2223
"analyzerUrl": [Function],
24+
"compressionAlgorithm": "gzip",
2325
"defaultSizes": "parsed",
2426
"excludeAssets": null,
2527
"generateStatsFile": false,
@@ -29,7 +31,7 @@ exports[`plugin-bundle-analyze > should add bundle analyze plugin 1`] = `
2931
"reportTitle": [Function],
3032
"startAnalyzer": true,
3133
"statsFilename": "stats.json",
32-
"statsOptions": null,
34+
"statsOptions": undefined,
3335
},
3436
"server": null,
3537
},
@@ -44,6 +46,7 @@ exports[`plugin-bundle-analyze > should add bundle analyze plugin when bundle an
4446
"name": "RsbuildCorePlugin",
4547
},
4648
BundleAnalyzerPlugin {
49+
"compiler": null,
4750
"logger": Logger {
4851
"activeLevels": Set {
4952
"info",
@@ -57,6 +60,7 @@ exports[`plugin-bundle-analyze > should add bundle analyze plugin when bundle an
5760
"analyzerMode": "static",
5861
"analyzerPort": 8888,
5962
"analyzerUrl": [Function],
63+
"compressionAlgorithm": "gzip",
6064
"defaultSizes": "parsed",
6165
"excludeAssets": null,
6266
"generateStatsFile": false,
@@ -66,7 +70,7 @@ exports[`plugin-bundle-analyze > should add bundle analyze plugin when bundle an
6670
"reportTitle": [Function],
6771
"startAnalyzer": true,
6872
"statsFilename": "stats.json",
69-
"statsOptions": null,
73+
"statsOptions": undefined,
7074
},
7175
"server": null,
7276
},
@@ -81,6 +85,7 @@ exports[`plugin-bundle-analyze > should enable bundle analyze plugin when perfor
8185
"name": "RsbuildCorePlugin",
8286
},
8387
BundleAnalyzerPlugin {
88+
"compiler": null,
8489
"logger": Logger {
8590
"activeLevels": Set {
8691
"info",
@@ -94,6 +99,7 @@ exports[`plugin-bundle-analyze > should enable bundle analyze plugin when perfor
9499
"analyzerMode": "disabled",
95100
"analyzerPort": 8888,
96101
"analyzerUrl": [Function],
102+
"compressionAlgorithm": "gzip",
97103
"defaultSizes": "parsed",
98104
"excludeAssets": null,
99105
"generateStatsFile": true,
@@ -103,7 +109,7 @@ exports[`plugin-bundle-analyze > should enable bundle analyze plugin when perfor
103109
"reportTitle": [Function],
104110
"startAnalyzer": true,
105111
"statsFilename": "stats.json",
106-
"statsOptions": null,
112+
"statsOptions": undefined,
107113
},
108114
"server": null,
109115
},

0 commit comments

Comments
 (0)