From ebb09e8bf1518a04d3d714dbae03b7788d4c0cfe Mon Sep 17 00:00:00 2001 From: Guru Narayanan Date: Thu, 1 Oct 2026 23:07:09 +0530 Subject: [PATCH 01/10] test(start-plugin-core): pin rspack client build normalization output --- ...ormalized-client-build-rspack.test.ts.snap | 1106 +++++++++++++++++ .../fixtures/rspack-client-build/src/index.js | 37 + .../rspack-client-build/src/islands/chart.css | 3 + .../rspack-client-build/src/islands/chart.js | 4 + .../src/islands/counter.css | 3 + .../src/islands/counter.js | 4 + .../src/islands/tooltip.css | 3 + .../src/islands/tooltip.js | 4 + .../rspack-client-build/src/lazy/plain.js | 3 + .../src/lazy/tsr-split-notes.js | 1 + .../fixtures/rspack-client-build/src/root.css | 3 + .../rspack-client-build/src/routes/about.js | 5 + .../rspack-client-build/src/routes/post.js | 14 + .../src/routes/posts-helper.js | 2 + .../rspack-client-build/src/routes/posts.css | 3 + .../rspack-client-build/src/routes/posts.js | 15 + .../src/routes/settings.css | 3 + .../src/routes/settings.js | 6 + .../fixtures/rspack-client-build/src/rsc.css | 3 + .../fixtures/rspack-client-build/src/rsc.js | 4 + .../src/shared/big-shared.js | 1 + .../src/shared/runtime-shared.js | 1 + .../src/shared/shared-styles.css | 3 + .../src/shared/vendor-a.js | 1 + .../src/shared/vendor-b.js | 6 + .../rspack-client-build/src/theme.css | 3 + .../normalized-client-build-rspack.test.ts | 126 ++ .../rsbuild/rspack-client-build-fixture.ts | 176 +++ 28 files changed, 1543 insertions(+) create mode 100644 packages/start-plugin-core/tests/rsbuild/__snapshots__/normalized-client-build-rspack.test.ts.snap create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/index.js create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/chart.css create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/chart.js create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/counter.css create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/counter.js create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/tooltip.css create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/tooltip.js create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/lazy/plain.js create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/lazy/tsr-split-notes.js create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/root.css create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/about.js create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/post.js create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts-helper.js create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts.css create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts.js create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/settings.css create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/settings.js create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/rsc.css create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/rsc.js create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/big-shared.js create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/runtime-shared.js create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/shared-styles.css create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/vendor-a.js create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/vendor-b.js create mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/theme.css create mode 100644 packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts create mode 100644 packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts diff --git a/packages/start-plugin-core/tests/rsbuild/__snapshots__/normalized-client-build-rspack.test.ts.snap b/packages/start-plugin-core/tests/rsbuild/__snapshots__/normalized-client-build-rspack.test.ts.snap new file mode 100644 index 00000000000..6e99acf8a37 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/__snapshots__/normalized-client-build-rspack.test.ts.snap @@ -0,0 +1,1106 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`development > matches the recorded normalized client build 1`] = ` +{ + "chunksByFileName": [ + [ + "index.js", + { + "css": [ + "index.css", + ], + "dynamicImports": [ + "vendors.js", + "posts.js", + "big-shared.js", + "post.js", + "about.js", + "settings.js", + "plain.js", + "notes.js", + ], + "fileName": "index.js", + "hydrationIds": [], + "imports": [], + "isEntry": true, + "routeFilePaths": [], + }, + ], + [ + "counter.js", + { + "css": [ + "counter.css", + ], + "dynamicImports": [], + "fileName": "counter.js", + "hydrationIds": [ + "counter", + ], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "about.js", + { + "css": [ + "assets/theme.css", + ], + "dynamicImports": [ + "tooltip.js", + ], + "fileName": "about.js", + "hydrationIds": [], + "imports": [ + "vendors.js", + ], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/about.js", + ], + }, + ], + [ + "vendors.js", + { + "css": [], + "dynamicImports": [ + "counter.js", + "tooltip.js", + "chart.js", + ], + "fileName": "vendors.js", + "hydrationIds": [], + "imports": [ + "posts.js", + "big-shared.js", + "post.js", + "about.js", + ], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "notes.js", + { + "css": [], + "dynamicImports": [], + "fileName": "notes.js", + "hydrationIds": [], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "post.js", + { + "css": [], + "dynamicImports": [ + "counter.js", + "chart.js", + ], + "fileName": "post.js", + "hydrationIds": [], + "imports": [ + "vendors.js", + "big-shared.js", + ], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/post.js", + ], + }, + ], + [ + "chart.js", + { + "css": [ + "chart.css", + ], + "dynamicImports": [], + "fileName": "chart.js", + "hydrationIds": [ + "chart", + ], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "plain.js", + { + "css": [], + "dynamicImports": [], + "fileName": "plain.js", + "hydrationIds": [], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "posts.js", + { + "css": [ + "posts.css", + ], + "dynamicImports": [ + "counter.js", + "tooltip.js", + ], + "fileName": "posts.js", + "hydrationIds": [], + "imports": [ + "vendors.js", + ], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/posts.js", + ], + }, + ], + [ + "big-shared.js", + { + "css": [], + "dynamicImports": [ + "counter.js", + "chart.js", + ], + "fileName": "big-shared.js", + "hydrationIds": [], + "imports": [ + "vendors.js", + "post.js", + ], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "tooltip.js", + { + "css": [ + "tooltip.css", + ], + "dynamicImports": [], + "fileName": "tooltip.js", + "hydrationIds": [ + "tooltip", + ], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "settings.js", + { + "css": [ + "settings.css", + ], + "dynamicImports": [], + "fileName": "settings.js", + "hydrationIds": [], + "imports": [], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/settings.js", + ], + }, + ], + ], + "cssContentByFileName": undefined, + "entryChunkFileName": "index.js", +} +`; + +exports[`development > matches the recorded start manifest 1`] = ` +{ + "routes": { + "/about": { + "css": [ + "/assets/assets/theme.css", + "/assets/counter.css", + "/assets/tooltip.css", + "/assets/chart.css", + ], + "filePath": "/src/routes/about.js", + "preloads": [ + "/assets/about.js", + "/assets/vendors.js", + ], + }, + "/post": { + "css": [ + "/assets/counter.css", + "/assets/tooltip.css", + "/assets/chart.css", + ], + "filePath": "/src/routes/post.js", + "preloads": [ + "/assets/post.js", + "/assets/vendors.js", + "/assets/big-shared.js", + ], + }, + "/posts": { + "css": [ + "/assets/assets/theme.css", + "/assets/posts.css", + "/assets/counter.css", + "/assets/chart.css", + "/assets/tooltip.css", + ], + "filePath": "/src/routes/posts.js", + "preloads": [ + "/assets/posts.js", + "/assets/vendors.js", + ], + }, + "/settings": { + "css": [ + "/assets/settings.css", + ], + "filePath": "/src/routes/settings.js", + "preloads": [ + "/assets/settings.js", + ], + }, + "__root__": { + "css": [ + "/assets/index.css", + ], + "preloads": [ + "/assets/index.js", + ], + "scripts": [ + { + "attrs": { + "async": true, + "src": "/assets/index.js", + "type": "module", + }, + }, + ], + }, + }, +} +`; + +exports[`matches the recorded client build with an rsc entry 1`] = ` +{ + "chunksByFileName": [ + [ + "post.js", + { + "css": [], + "dynamicImports": [ + "counter.js", + "chart.js", + ], + "fileName": "post.js", + "hydrationIds": [], + "imports": [ + "vendors.js", + "big-shared.js", + ], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/post.js", + ], + }, + ], + [ + "chart.js", + { + "css": [ + "chart.css", + ], + "dynamicImports": [], + "fileName": "chart.js", + "hydrationIds": [ + "chart", + ], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "plain.js", + { + "css": [], + "dynamicImports": [], + "fileName": "plain.js", + "hydrationIds": [], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "posts.js", + { + "css": [ + "posts.css", + ], + "dynamicImports": [ + "counter.js", + "tooltip.js", + ], + "fileName": "posts.js", + "hydrationIds": [], + "imports": [ + "vendors.js", + ], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/posts.js", + ], + }, + ], + [ + "big-shared.js", + { + "css": [], + "dynamicImports": [ + "counter.js", + "chart.js", + ], + "fileName": "big-shared.js", + "hydrationIds": [], + "imports": [ + "vendors.js", + "post.js", + ], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "tooltip.js", + { + "css": [ + "tooltip.css", + ], + "dynamicImports": [], + "fileName": "tooltip.js", + "hydrationIds": [ + "tooltip", + ], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "settings.js", + { + "css": [ + "settings.css", + ], + "dynamicImports": [], + "fileName": "settings.js", + "hydrationIds": [], + "imports": [], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/settings.js", + ], + }, + ], + [ + "rsc.js", + { + "css": [ + "rsc.css", + ], + "dynamicImports": [], + "fileName": "rsc.js", + "hydrationIds": [], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "counter.js", + { + "css": [ + "counter.css", + ], + "dynamicImports": [], + "fileName": "counter.js", + "hydrationIds": [ + "counter", + ], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "about.js", + { + "css": [ + "assets/theme.css", + ], + "dynamicImports": [ + "tooltip.js", + ], + "fileName": "about.js", + "hydrationIds": [], + "imports": [ + "vendors.js", + ], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/about.js", + ], + }, + ], + [ + "index.js", + { + "css": [ + "index.css", + "rsc.css", + ], + "dynamicImports": [ + "vendors.js", + "posts.js", + "big-shared.js", + "post.js", + "about.js", + "settings.js", + "plain.js", + "notes.js", + ], + "fileName": "index.js", + "hydrationIds": [], + "imports": [], + "isEntry": true, + "routeFilePaths": [], + }, + ], + [ + "vendors.js", + { + "css": [], + "dynamicImports": [ + "counter.js", + "tooltip.js", + "chart.js", + ], + "fileName": "vendors.js", + "hydrationIds": [], + "imports": [ + "posts.js", + "big-shared.js", + "post.js", + "about.js", + ], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "notes.js", + { + "css": [], + "dynamicImports": [], + "fileName": "notes.js", + "hydrationIds": [], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + ], + "cssContentByFileName": undefined, + "entryChunkFileName": "index.js", +} +`; + +exports[`matches the recorded client build with inline CSS 1`] = ` +{ + "chunksByFileName": [ + [ + "index.js", + { + "css": [ + "index.css", + ], + "dynamicImports": [ + "vendors.js", + "posts.js", + "big-shared.js", + "post.js", + "about.js", + "settings.js", + "plain.js", + "notes.js", + ], + "fileName": "index.js", + "hydrationIds": [], + "imports": [], + "isEntry": true, + "routeFilePaths": [], + }, + ], + [ + "counter.js", + { + "css": [ + "counter.css", + ], + "dynamicImports": [], + "fileName": "counter.js", + "hydrationIds": [ + "counter", + ], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "about.js", + { + "css": [ + "assets/theme.css", + ], + "dynamicImports": [ + "tooltip.js", + ], + "fileName": "about.js", + "hydrationIds": [], + "imports": [ + "vendors.js", + ], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/about.js", + ], + }, + ], + [ + "vendors.js", + { + "css": [], + "dynamicImports": [ + "counter.js", + "tooltip.js", + "chart.js", + ], + "fileName": "vendors.js", + "hydrationIds": [], + "imports": [ + "posts.js", + "big-shared.js", + "post.js", + "about.js", + ], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "notes.js", + { + "css": [], + "dynamicImports": [], + "fileName": "notes.js", + "hydrationIds": [], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "post.js", + { + "css": [], + "dynamicImports": [ + "counter.js", + "chart.js", + ], + "fileName": "post.js", + "hydrationIds": [], + "imports": [ + "vendors.js", + "big-shared.js", + ], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/post.js", + ], + }, + ], + [ + "chart.js", + { + "css": [ + "chart.css", + ], + "dynamicImports": [], + "fileName": "chart.js", + "hydrationIds": [ + "chart", + ], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "plain.js", + { + "css": [], + "dynamicImports": [], + "fileName": "plain.js", + "hydrationIds": [], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "posts.js", + { + "css": [ + "posts.css", + ], + "dynamicImports": [ + "counter.js", + "tooltip.js", + ], + "fileName": "posts.js", + "hydrationIds": [], + "imports": [ + "vendors.js", + ], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/posts.js", + ], + }, + ], + [ + "tooltip.js", + { + "css": [ + "tooltip.css", + ], + "dynamicImports": [], + "fileName": "tooltip.js", + "hydrationIds": [ + "tooltip", + ], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "big-shared.js", + { + "css": [], + "dynamicImports": [ + "counter.js", + "chart.js", + ], + "fileName": "big-shared.js", + "hydrationIds": [], + "imports": [ + "vendors.js", + "post.js", + ], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "settings.js", + { + "css": [ + "settings.css", + ], + "dynamicImports": [], + "fileName": "settings.js", + "hydrationIds": [], + "imports": [], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/settings.js", + ], + }, + ], + ], + "cssContentByFileName": [ + [ + "counter.css", + ".counter { + color: blue; +} + +", + ], + [ + "tooltip.css", + ".tooltip { + color: gray; +} + +", + ], + [ + "chart.css", + ".chart { + color: orange; +} + +", + ], + [ + "index.css", + ".root { + color: black; +} + +", + ], + [ + "assets/theme.css", + ".theme { + background: white; +} +", + ], + [ + "settings.css", + ".settings { + color: green; +} + +", + ], + [ + "shared-styles.css", + ".shared { + font-weight: bold; +} + +", + ], + [ + "posts.css", + ".posts { + color: red; +} + +", + ], + ], + "entryChunkFileName": "index.js", +} +`; + +exports[`production > matches the recorded normalized client build 1`] = ` +{ + "chunksByFileName": [ + [ + "plain.js", + { + "css": [], + "dynamicImports": [], + "fileName": "plain.js", + "hydrationIds": [], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "posts.js", + { + "css": [ + "posts.css", + ], + "dynamicImports": [ + "counter.js", + "tooltip.js", + ], + "fileName": "posts.js", + "hydrationIds": [], + "imports": [ + "vendors.js", + ], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/posts.js", + ], + }, + ], + [ + "tooltip.js", + { + "css": [ + "tooltip.css", + ], + "dynamicImports": [], + "fileName": "tooltip.js", + "hydrationIds": [ + "tooltip", + ], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "big-shared.js", + { + "css": [], + "dynamicImports": [ + "counter.js", + "chart.js", + ], + "fileName": "big-shared.js", + "hydrationIds": [], + "imports": [ + "vendors.js", + "post.js", + ], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "settings.js", + { + "css": [ + "settings.css", + ], + "dynamicImports": [], + "fileName": "settings.js", + "hydrationIds": [], + "imports": [], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/settings.js", + ], + }, + ], + [ + "index.js", + { + "css": [ + "index.css", + ], + "dynamicImports": [ + "vendors.js", + "posts.js", + "big-shared.js", + "post.js", + "about.js", + "settings.js", + "plain.js", + "notes.js", + ], + "fileName": "index.js", + "hydrationIds": [], + "imports": [], + "isEntry": true, + "routeFilePaths": [], + }, + ], + [ + "counter.js", + { + "css": [ + "counter.css", + ], + "dynamicImports": [], + "fileName": "counter.js", + "hydrationIds": [ + "counter", + ], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "about.js", + { + "css": [ + "assets/theme.css", + ], + "dynamicImports": [ + "tooltip.js", + ], + "fileName": "about.js", + "hydrationIds": [], + "imports": [ + "vendors.js", + ], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/about.js", + ], + }, + ], + [ + "vendors.js", + { + "css": [], + "dynamicImports": [ + "counter.js", + "tooltip.js", + "chart.js", + ], + "fileName": "vendors.js", + "hydrationIds": [], + "imports": [ + "posts.js", + "big-shared.js", + "post.js", + "about.js", + ], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "notes.js", + { + "css": [], + "dynamicImports": [], + "fileName": "notes.js", + "hydrationIds": [], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "post.js", + { + "css": [], + "dynamicImports": [ + "counter.js", + "chart.js", + ], + "fileName": "post.js", + "hydrationIds": [], + "imports": [ + "vendors.js", + "big-shared.js", + ], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/post.js", + ], + }, + ], + [ + "chart.js", + { + "css": [ + "chart.css", + ], + "dynamicImports": [], + "fileName": "chart.js", + "hydrationIds": [ + "chart", + ], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + ], + "cssContentByFileName": undefined, + "entryChunkFileName": "index.js", +} +`; + +exports[`production > matches the recorded start manifest 1`] = ` +{ + "routes": { + "/about": { + "css": [ + "/assets/assets/theme.css", + "/assets/counter.css", + "/assets/tooltip.css", + "/assets/chart.css", + ], + "filePath": "/src/routes/about.js", + "preloads": [ + "/assets/about.js", + "/assets/vendors.js", + ], + }, + "/post": { + "css": [ + "/assets/counter.css", + "/assets/tooltip.css", + "/assets/chart.css", + ], + "filePath": "/src/routes/post.js", + "preloads": [ + "/assets/post.js", + "/assets/vendors.js", + "/assets/big-shared.js", + ], + }, + "/posts": { + "css": [ + "/assets/assets/theme.css", + "/assets/posts.css", + "/assets/counter.css", + "/assets/chart.css", + "/assets/tooltip.css", + ], + "filePath": "/src/routes/posts.js", + "preloads": [ + "/assets/posts.js", + "/assets/vendors.js", + ], + }, + "/settings": { + "css": [ + "/assets/settings.css", + ], + "filePath": "/src/routes/settings.js", + "preloads": [ + "/assets/settings.js", + ], + }, + "__root__": { + "css": [ + "/assets/index.css", + ], + "preloads": [ + "/assets/index.js", + ], + "scripts": [ + { + "attrs": { + "async": true, + "src": "/assets/index.js", + "type": "module", + }, + }, + ], + }, + }, +} +`; diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/index.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/index.js new file mode 100644 index 00000000000..bdbb9d0e7e0 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/index.js @@ -0,0 +1,37 @@ +// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. +import './root.css' +import { runtime } from './shared/runtime-shared.js' + +export const app = { + runtime, + posts: () => + import( + // @ts-expect-error Rspack resolves resource queries in this fixture input. + /* webpackChunkName: "posts" */ './routes/posts.js?tsr-split=component' + ), + // Only the component split uses posts-helper, allowing production concatenation. + postsError: () => + import( + // @ts-expect-error Rspack resolves resource queries in this fixture input. + /* webpackChunkName: "posts", webpackExports: ["errorComponent"] */ './routes/posts.js?tsr-split=errorComponent' + ), + post: () => + import( + // @ts-expect-error Rspack resolves resource queries in this fixture input. + /* webpackChunkName: "post" */ './routes/post.js?tsr-split=component' + ), + about: () => + import( + // @ts-expect-error Rspack resolves resource queries in this fixture input. + /* webpackChunkName: "about" */ './routes/about.js?tsr-split=component' + ), + settings: () => + import( + // @ts-expect-error Rspack resolves resource queries in this fixture input. + /* webpackChunkName: "settings" */ './routes/settings.js?tsr-split=component' + ), + plain: () => import(/* webpackChunkName: "plain" */ './lazy/plain.js'), + notes: () => + import(/* webpackChunkName: "notes" */ './lazy/tsr-split-notes.js'), +} +Object.assign(globalThis, { fixtureApp: app }) diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/chart.css b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/chart.css new file mode 100644 index 00000000000..ae060598221 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/chart.css @@ -0,0 +1,3 @@ +.chart { + color: orange; +} diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/chart.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/chart.js new file mode 100644 index 00000000000..9613ae091d3 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/chart.js @@ -0,0 +1,4 @@ +// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. +import './chart.css' + +export const chart = 'chart' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/counter.css b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/counter.css new file mode 100644 index 00000000000..54e2be6982b --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/counter.css @@ -0,0 +1,3 @@ +.counter { + color: blue; +} diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/counter.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/counter.js new file mode 100644 index 00000000000..4b5a6770327 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/counter.js @@ -0,0 +1,4 @@ +// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. +import './counter.css' + +export const counter = 'counter' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/tooltip.css b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/tooltip.css new file mode 100644 index 00000000000..3f6134b01ba --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/tooltip.css @@ -0,0 +1,3 @@ +.tooltip { + color: gray; +} diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/tooltip.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/tooltip.js new file mode 100644 index 00000000000..17caeefd75e --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/tooltip.js @@ -0,0 +1,4 @@ +// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. +import './tooltip.css' + +export const tooltip = 'tooltip' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/lazy/plain.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/lazy/plain.js new file mode 100644 index 00000000000..a718f6a1e79 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/lazy/plain.js @@ -0,0 +1,3 @@ +import { runtime } from '../shared/runtime-shared.js' + +export const plain = `plain:${runtime}` diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/lazy/tsr-split-notes.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/lazy/tsr-split-notes.js new file mode 100644 index 00000000000..39c93cc7ff9 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/lazy/tsr-split-notes.js @@ -0,0 +1 @@ +export const notes = 'not a route split' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/root.css b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/root.css new file mode 100644 index 00000000000..25f0ffad60b --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/root.css @@ -0,0 +1,3 @@ +.root { + color: black; +} diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/about.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/about.js new file mode 100644 index 00000000000..8267380a9d0 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/about.js @@ -0,0 +1,5 @@ +// @ts-expect-error Rspack resolves resource queries in this fixture input. +import themeUrl from '../theme.css?url' +import { vendorB } from '../shared/vendor-b.js' + +export const about = () => ({ themeUrl, vendorB }) diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/post.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/post.js new file mode 100644 index 00000000000..39824be86b7 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/post.js @@ -0,0 +1,14 @@ +import { vendorA } from '../shared/vendor-a.js' +import { bigShared } from '../shared/big-shared.js' + +export const post = () => vendorA + bigShared +export const counter = () => + import( + // @ts-expect-error Rspack resolves resource queries in this fixture input. + /* webpackChunkName: "counter" */ '../islands/counter.js?tss-hydrate=counter' + ) +export const chart = () => + import( + // @ts-expect-error Rspack resolves resource queries in this fixture input. + /* webpackChunkName: "chart" */ '../islands/chart.js?tss-hydrate=chart' + ) diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts-helper.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts-helper.js new file mode 100644 index 00000000000..23bae658a36 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts-helper.js @@ -0,0 +1,2 @@ +/** @param {string} value */ +export const postsHelper = (value) => `posts:${value}` diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts.css b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts.css new file mode 100644 index 00000000000..e9d9ef1e7fc --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts.css @@ -0,0 +1,3 @@ +.posts { + color: red; +} diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts.js new file mode 100644 index 00000000000..5192083c966 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts.js @@ -0,0 +1,15 @@ +// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. +import './posts.css' +// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. +import '../shared/shared-styles.css' +import { vendorA } from '../shared/vendor-a.js' +import { vendorB } from '../shared/vendor-b.js' +import { postsHelper } from './posts-helper.js' + +export const errorComponent = 'posts-error' +export const posts = () => postsHelper(vendorA + vendorB) +export const counter = () => + import( + // @ts-expect-error Rspack resolves resource queries in this fixture input. + /* webpackChunkName: "counter" */ '../islands/counter.js?tss-hydrate=counter' + ) diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/settings.css b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/settings.css new file mode 100644 index 00000000000..9cad3b2d1b8 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/settings.css @@ -0,0 +1,3 @@ +.settings { + color: green; +} diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/settings.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/settings.js new file mode 100644 index 00000000000..e4b2ba60325 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/settings.js @@ -0,0 +1,6 @@ +// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. +import './settings.css' +// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. +import '../shared/shared-styles.css' + +export const settings = 'settings' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/rsc.css b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/rsc.css new file mode 100644 index 00000000000..8c438ed6dc5 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/rsc.css @@ -0,0 +1,3 @@ +.rsc { + color: navy; +} diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/rsc.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/rsc.js new file mode 100644 index 00000000000..334f1dd6cb0 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/rsc.js @@ -0,0 +1,4 @@ +// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. +import './rsc.css' + +Object.assign(globalThis, { fixtureRsc: 'rsc' }) diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/big-shared.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/big-shared.js new file mode 100644 index 00000000000..292a7efa27e --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/big-shared.js @@ -0,0 +1 @@ +export const bigShared = 'big-shared' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/runtime-shared.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/runtime-shared.js new file mode 100644 index 00000000000..749f652eef1 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/runtime-shared.js @@ -0,0 +1 @@ +export const runtime = 'runtime' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/shared-styles.css b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/shared-styles.css new file mode 100644 index 00000000000..a9f7e595db6 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/shared-styles.css @@ -0,0 +1,3 @@ +.shared { + font-weight: bold; +} diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/vendor-a.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/vendor-a.js new file mode 100644 index 00000000000..043a9c38c01 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/vendor-a.js @@ -0,0 +1 @@ +export const vendorA = 'vendor-a' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/vendor-b.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/vendor-b.js new file mode 100644 index 00000000000..fbba157c724 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/vendor-b.js @@ -0,0 +1,6 @@ +export const vendorB = 'vendor-b' +export const tooltip = () => + import( + // @ts-expect-error Rspack resolves resource queries in this fixture input. + /* webpackChunkName: "tooltip" */ '../islands/tooltip.js?tss-hydrate=tooltip' + ) diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/theme.css b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/theme.css new file mode 100644 index 00000000000..09d808ff40f --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/theme.css @@ -0,0 +1,3 @@ +.theme { + background: white; +} diff --git a/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts b/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts new file mode 100644 index 00000000000..4f4c2026b1b --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts @@ -0,0 +1,126 @@ +import { describe, expect, test } from 'vitest' +import { normalizeRspackClientBuild } from '../../src/rsbuild/normalized-client-build' +import { buildStartManifest } from '../../src/start-manifest-plugin/manifestBuilder' +import { + FIXTURE_ROOT, + compileClientFixture, + fixtureRouteTreeRoutes, + serializeClientBuild, +} from './rspack-client-build-fixture' +import type { Rspack } from '@rsbuild/core' +import type { FixtureMode } from './rspack-client-build-fixture' + +function hasJsFile(chunk: Rspack.Chunk): boolean { + return Array.from(chunk.files).some( + (file) => file.endsWith('.js') || file.endsWith('.mjs'), + ) +} + +describe.each(['development', 'production'])('%s', (mode) => { + test('matches the recorded normalized client build', async () => { + const build = await compileClientFixture({ mode }, (compilation) => + serializeClientBuild(normalizeRspackClientBuild(compilation)), + ) + expect(build).toMatchSnapshot() + }) + + test('matches the recorded start manifest', async () => { + const manifest = await compileClientFixture({ mode }, (compilation) => + buildStartManifest({ + clientBuild: normalizeRspackClientBuild(compilation), + routeTreeRoutes: fixtureRouteTreeRoutes(), + basePath: '/assets', + }), + ) + const serialized = JSON.stringify(manifest).replaceAll( + FIXTURE_ROOT.replace(/\\/g, '/'), + '', + ) + expect(JSON.parse(serialized)).toMatchSnapshot() + }) + + test('fixture exercises the shapes the normalizer must preserve', async () => { + await compileClientFixture({ mode }, (compilation) => { + const chunks = Array.from(compilation.chunks) + const groups = Array.from(compilation.chunkGroups) + + expect( + groups.some((group) => group.chunks.filter(hasJsFile).length >= 2), + 'a group has sibling JS chunks', + ).toBe(true) + expect( + chunks.some((chunk) => Array.from(chunk.groupsIterable).length >= 2), + 'a shared chunk belongs to multiple groups', + ).toBe(true) + expect( + chunks.some((chunk) => !hasJsFile(chunk)), + 'a CSS-only chunk is present', + ).toBe(true) + expect( + chunks.some((chunk) => + Array.from(chunk.auxiliaryFiles).some((file) => + file.endsWith('.css'), + ), + ), + 'CSS assets appear in auxiliaryFiles', + ).toBe(true) + expect( + chunks.some( + (chunk) => + compilation.chunkGraph.getChunkModules(chunk).filter((module) => { + const identifier = module.identifier().replace(/\\/g, '/') + return identifier.includes('/routes/posts.js?tsr-split=') + }).length === 2, + ), + 'two posts split modules share a chunk', + ).toBe(true) + + const hydrationChunks = new Set( + chunks.filter((chunk) => + compilation.chunkGraph + .getChunkModules(chunk) + .some((module) => module.identifier().includes('?tss-hydrate=')), + ), + ) + expect( + groups.some( + (group) => + group.chunks.some((chunk) => chunk.name === 'vendors') && + Array.from(group.childrenIterable).some((child) => + child.chunks.some((chunk) => hydrationChunks.has(chunk)), + ), + ), + 'hydration is reachable through a vendors group', + ).toBe(true) + + if (mode === 'production') { + expect( + Array.from(compilation.modules).some( + (module) => + module.constructor.name === 'ConcatenatedModule' && + module.identifier().includes('posts.js?tsr-split='), + ), + 'a posts split is a concatenation root', + ).toBe(true) + } + }) + }) +}) + +test('matches the recorded client build with an rsc entry', async () => { + const build = await compileClientFixture( + { mode: 'production', withRscEntry: true }, + (compilation) => + serializeClientBuild(normalizeRspackClientBuild(compilation)), + ) + expect(build).toMatchSnapshot() +}) + +test('matches the recorded client build with inline CSS', async () => { + const build = await compileClientFixture( + { mode: 'production' }, + (compilation) => + serializeClientBuild(normalizeRspackClientBuild(compilation, true)), + ) + expect(build).toMatchSnapshot() +}) diff --git a/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts b/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts new file mode 100644 index 00000000000..7e835926e7e --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts @@ -0,0 +1,176 @@ +import { mkdtemp, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { fileURLToPath } from 'node:url' +import { rspack } from '@rsbuild/core' +import type { Rspack } from '@rsbuild/core' +import type { NormalizedClientBuild } from '../../src/types' + +export const FIXTURE_ROOT = fileURLToPath( + new URL('./fixtures/rspack-client-build/', import.meta.url), +).replace(/[\\/]$/, '') + +export type FixtureMode = 'development' | 'production' + +export async function compileClientFixture( + options: { mode: FixtureMode; withRscEntry?: boolean }, + inspect: (compilation: Rspack.Compilation) => T, +): Promise { + const outputPath = await mkdtemp(join(tmpdir(), 'rspack-client-build-')) + let compiler: Rspack.Compiler | undefined + let result: T + + try { + compiler = rspack({ + mode: options.mode, + context: FIXTURE_ROOT, + devtool: false, + entry: { + index: './src/index.js', + ...(options.withRscEntry ? { rsc: './src/rsc.js' } : {}), + }, + output: { + path: outputPath, + filename: '[name].js', + chunkFilename: '[name].js', + cssFilename: '[name].css', + cssChunkFilename: '[name].css', + assetModuleFilename: 'assets/[name][ext]', + }, + module: { + rules: [ + { + test: /\.css$/, + // The enclosing package is sideEffects: false, unlike this fixture app. + sideEffects: true, + oneOf: [ + { resourceQuery: /url/, type: 'asset/resource' }, + { type: 'css/auto' }, + ], + }, + ], + }, + optimization: { + minimize: false, + chunkIds: 'named', + moduleIds: 'named', + splitChunks: { + chunks: 'all', + minSize: 0, + cacheGroups: { + vendors: { + test: /[\\/]shared[\\/]vendor-/, + name: 'vendors', + enforce: true, + }, + bigShared: { + test: /big-shared/, + name: 'big-shared', + enforce: true, + }, + sharedStyles: { + test: /shared-styles\.css$/, + type: 'css/auto', + name: 'shared-styles', + enforce: true, + }, + }, + }, + }, + plugins: [ + { + apply(fixtureCompiler: Rspack.Compiler) { + fixtureCompiler.hooks.thisCompilation.tap( + 'InspectClientFixture', + (compilation: Rspack.Compilation) => { + compilation.hooks.processAssets.tap( + { + name: 'InspectClientFixture', + stage: rspack.Compilation.PROCESS_ASSETS_STAGE_REPORT, + }, + () => { + result = inspect(compilation) + }, + ) + }, + ) + }, + }, + ], + }) + + return await new Promise((resolve, reject) => { + compiler!.run((error, stats) => { + if (error) { + reject(error) + } else if (!stats || stats.hasErrors()) { + reject( + new Error( + stats?.toString({ + all: false, + errors: true, + errorDetails: true, + }) ?? 'Rspack did not return compilation stats', + ), + ) + } else { + resolve(result) + } + }) + }) + } finally { + try { + if (compiler) { + await new Promise((resolve, reject) => { + compiler!.close((error) => { + if (error) { + reject(error) + } else { + resolve() + } + }) + }) + } + } finally { + await rm(outputPath, { recursive: true, force: true }) + } + } +} + +export function serializeClientBuild(build: NormalizedClientBuild): unknown { + const fixtureRoot = FIXTURE_ROOT.replace(/\\/g, '/') + return { + entryChunkFileName: build.entryChunkFileName, + chunksByFileName: Array.from( + build.chunksByFileName, + ([fileName, chunk]) => [ + fileName, + { + ...chunk, + routeFilePaths: chunk.routeFilePaths.map((filePath) => + filePath.replaceAll(fixtureRoot, ''), + ), + }, + ], + ), + cssContentByFileName: build.cssContentByFileName + ? Array.from(build.cssContentByFileName) + : undefined, + } +} + +export function fixtureRouteTreeRoutes(): Record< + string, + { filePath?: string } +> { + const routes: Record = { __root__: {} } + for (const name of ['posts', 'post', 'about', 'settings']) { + routes[`/${name}`] = { + filePath: join(FIXTURE_ROOT, 'src/routes', `${name}.js`).replace( + /\\/g, + '/', + ), + } + } + return routes +} From 5970137fc2ed239d53e74976da61ae3b7d9ff43f Mon Sep 17 00:00:00 2001 From: Guru Narayanan Date: Thu, 1 Oct 2026 23:27:35 +0530 Subject: [PATCH 02/10] test(start-plugin-core): make rspack golden snapshots independent of chunk iteration order --- ...ormalized-client-build-rspack.test.ts.snap | 756 +++++++++--------- .../normalized-client-build-rspack.test.ts | 37 +- .../rsbuild/rspack-client-build-fixture.ts | 38 +- 3 files changed, 440 insertions(+), 391 deletions(-) diff --git a/packages/start-plugin-core/tests/rsbuild/__snapshots__/normalized-client-build-rspack.test.ts.snap b/packages/start-plugin-core/tests/rsbuild/__snapshots__/normalized-client-build-rspack.test.ts.snap index 6e99acf8a37..3422bc8ed36 100644 --- a/packages/start-plugin-core/tests/rsbuild/__snapshots__/normalized-client-build-rspack.test.ts.snap +++ b/packages/start-plugin-core/tests/rsbuild/__snapshots__/normalized-client-build-rspack.test.ts.snap @@ -4,25 +4,56 @@ exports[`development > matches the recorded normalized client build 1`] = ` { "chunksByFileName": [ [ - "index.js", + "about.js", { "css": [ - "index.css", + "assets/theme.css", ], "dynamicImports": [ + "tooltip.js", + ], + "fileName": "about.js", + "hydrationIds": [], + "imports": [ "vendors.js", - "posts.js", - "big-shared.js", - "post.js", - "about.js", - "settings.js", - "plain.js", - "notes.js", ], - "fileName": "index.js", + "isEntry": false, + "routeFilePaths": [ + "/src/routes/about.js", + ], + }, + ], + [ + "big-shared.js", + { + "css": [], + "dynamicImports": [ + "counter.js", + "chart.js", + ], + "fileName": "big-shared.js", "hydrationIds": [], + "imports": [ + "vendors.js", + "post.js", + ], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "chart.js", + { + "css": [ + "chart.css", + ], + "dynamicImports": [], + "fileName": "chart.js", + "hydrationIds": [ + "chart", + ], "imports": [], - "isEntry": true, + "isEntry": false, "routeFilePaths": [], }, ], @@ -43,52 +74,46 @@ exports[`development > matches the recorded normalized client build 1`] = ` }, ], [ - "about.js", + "index.js", { "css": [ - "assets/theme.css", + "index.css", ], "dynamicImports": [ - "tooltip.js", - ], - "fileName": "about.js", - "hydrationIds": [], - "imports": [ "vendors.js", + "posts.js", + "big-shared.js", + "post.js", + "about.js", + "settings.js", + "plain.js", + "notes.js", ], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/about.js", - ], + "fileName": "index.js", + "hydrationIds": [], + "imports": [], + "isEntry": true, + "routeFilePaths": [], }, ], [ - "vendors.js", + "notes.js", { "css": [], - "dynamicImports": [ - "counter.js", - "tooltip.js", - "chart.js", - ], - "fileName": "vendors.js", + "dynamicImports": [], + "fileName": "notes.js", "hydrationIds": [], - "imports": [ - "posts.js", - "big-shared.js", - "post.js", - "about.js", - ], + "imports": [], "isEntry": false, "routeFilePaths": [], }, ], [ - "notes.js", + "plain.js", { "css": [], "dynamicImports": [], - "fileName": "notes.js", + "fileName": "plain.js", "hydrationIds": [], "imports": [], "isEntry": false, @@ -115,34 +140,6 @@ exports[`development > matches the recorded normalized client build 1`] = ` ], }, ], - [ - "chart.js", - { - "css": [ - "chart.css", - ], - "dynamicImports": [], - "fileName": "chart.js", - "hydrationIds": [ - "chart", - ], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "plain.js", - { - "css": [], - "dynamicImports": [], - "fileName": "plain.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], [ "posts.js", { @@ -165,21 +162,19 @@ exports[`development > matches the recorded normalized client build 1`] = ` }, ], [ - "big-shared.js", + "settings.js", { - "css": [], - "dynamicImports": [ - "counter.js", - "chart.js", + "css": [ + "settings.css", ], - "fileName": "big-shared.js", + "dynamicImports": [], + "fileName": "settings.js", "hydrationIds": [], - "imports": [ - "vendors.js", - "post.js", - ], + "imports": [], "isEntry": false, - "routeFilePaths": [], + "routeFilePaths": [ + "/src/routes/settings.js", + ], }, ], [ @@ -199,19 +194,24 @@ exports[`development > matches the recorded normalized client build 1`] = ` }, ], [ - "settings.js", + "vendors.js", { - "css": [ - "settings.css", + "css": [], + "dynamicImports": [ + "counter.js", + "tooltip.js", + "chart.js", ], - "dynamicImports": [], - "fileName": "settings.js", + "fileName": "vendors.js", "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/settings.js", + "imports": [ + "posts.js", + "big-shared.js", + "post.js", + "about.js", ], + "isEntry": false, + "routeFilePaths": [], }, ], ], @@ -297,71 +297,22 @@ exports[`matches the recorded client build with an rsc entry 1`] = ` { "chunksByFileName": [ [ - "post.js", - { - "css": [], - "dynamicImports": [ - "counter.js", - "chart.js", - ], - "fileName": "post.js", - "hydrationIds": [], - "imports": [ - "vendors.js", - "big-shared.js", - ], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/post.js", - ], - }, - ], - [ - "chart.js", - { - "css": [ - "chart.css", - ], - "dynamicImports": [], - "fileName": "chart.js", - "hydrationIds": [ - "chart", - ], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "plain.js", - { - "css": [], - "dynamicImports": [], - "fileName": "plain.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "posts.js", + "about.js", { "css": [ - "posts.css", + "assets/theme.css", ], "dynamicImports": [ - "counter.js", "tooltip.js", ], - "fileName": "posts.js", + "fileName": "about.js", "hydrationIds": [], "imports": [ "vendors.js", ], "isEntry": false, "routeFilePaths": [ - "/src/routes/posts.js", + "/src/routes/about.js", ], }, ], @@ -384,15 +335,15 @@ exports[`matches the recorded client build with an rsc entry 1`] = ` }, ], [ - "tooltip.js", + "chart.js", { "css": [ - "tooltip.css", + "chart.css", ], "dynamicImports": [], - "fileName": "tooltip.js", + "fileName": "chart.js", "hydrationIds": [ - "tooltip", + "chart", ], "imports": [], "isEntry": false, @@ -400,92 +351,153 @@ exports[`matches the recorded client build with an rsc entry 1`] = ` }, ], [ - "settings.js", + "counter.js", { "css": [ - "settings.css", + "counter.css", ], "dynamicImports": [], - "fileName": "settings.js", - "hydrationIds": [], + "fileName": "counter.js", + "hydrationIds": [ + "counter", + ], "imports": [], "isEntry": false, - "routeFilePaths": [ - "/src/routes/settings.js", - ], + "routeFilePaths": [], }, ], [ - "rsc.js", + "index.js", { "css": [ + "index.css", "rsc.css", ], - "dynamicImports": [], - "fileName": "rsc.js", - "hydrationIds": [], - "imports": [], + "dynamicImports": [ + "vendors.js", + "posts.js", + "big-shared.js", + "post.js", + "about.js", + "settings.js", + "plain.js", + "notes.js", + ], + "fileName": "index.js", + "hydrationIds": [], + "imports": [], + "isEntry": true, + "routeFilePaths": [], + }, + ], + [ + "notes.js", + { + "css": [], + "dynamicImports": [], + "fileName": "notes.js", + "hydrationIds": [], + "imports": [], "isEntry": false, "routeFilePaths": [], }, ], [ - "counter.js", + "plain.js", { - "css": [ - "counter.css", - ], + "css": [], "dynamicImports": [], - "fileName": "counter.js", - "hydrationIds": [ - "counter", - ], + "fileName": "plain.js", + "hydrationIds": [], "imports": [], "isEntry": false, "routeFilePaths": [], }, ], [ - "about.js", + "post.js", + { + "css": [], + "dynamicImports": [ + "counter.js", + "chart.js", + ], + "fileName": "post.js", + "hydrationIds": [], + "imports": [ + "vendors.js", + "big-shared.js", + ], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/post.js", + ], + }, + ], + [ + "posts.js", { "css": [ - "assets/theme.css", + "posts.css", ], "dynamicImports": [ + "counter.js", "tooltip.js", ], - "fileName": "about.js", + "fileName": "posts.js", "hydrationIds": [], "imports": [ "vendors.js", ], "isEntry": false, "routeFilePaths": [ - "/src/routes/about.js", + "/src/routes/posts.js", ], }, ], [ - "index.js", + "rsc.js", { "css": [ - "index.css", "rsc.css", ], - "dynamicImports": [ - "vendors.js", - "posts.js", - "big-shared.js", - "post.js", - "about.js", - "settings.js", - "plain.js", - "notes.js", + "dynamicImports": [], + "fileName": "rsc.js", + "hydrationIds": [], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "settings.js", + { + "css": [ + "settings.css", ], - "fileName": "index.js", + "dynamicImports": [], + "fileName": "settings.js", "hydrationIds": [], "imports": [], - "isEntry": true, + "isEntry": false, + "routeFilePaths": [ + "/src/routes/settings.js", + ], + }, + ], + [ + "tooltip.js", + { + "css": [ + "tooltip.css", + ], + "dynamicImports": [], + "fileName": "tooltip.js", + "hydrationIds": [ + "tooltip", + ], + "imports": [], + "isEntry": false, "routeFilePaths": [], }, ], @@ -510,18 +522,6 @@ exports[`matches the recorded client build with an rsc entry 1`] = ` "routeFilePaths": [], }, ], - [ - "notes.js", - { - "css": [], - "dynamicImports": [], - "fileName": "notes.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], ], "cssContentByFileName": undefined, "entryChunkFileName": "index.js", @@ -532,25 +532,56 @@ exports[`matches the recorded client build with inline CSS 1`] = ` { "chunksByFileName": [ [ - "index.js", + "about.js", { "css": [ - "index.css", + "assets/theme.css", ], "dynamicImports": [ + "tooltip.js", + ], + "fileName": "about.js", + "hydrationIds": [], + "imports": [ "vendors.js", - "posts.js", - "big-shared.js", - "post.js", - "about.js", - "settings.js", - "plain.js", - "notes.js", ], - "fileName": "index.js", + "isEntry": false, + "routeFilePaths": [ + "/src/routes/about.js", + ], + }, + ], + [ + "big-shared.js", + { + "css": [], + "dynamicImports": [ + "counter.js", + "chart.js", + ], + "fileName": "big-shared.js", "hydrationIds": [], + "imports": [ + "vendors.js", + "post.js", + ], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "chart.js", + { + "css": [ + "chart.css", + ], + "dynamicImports": [], + "fileName": "chart.js", + "hydrationIds": [ + "chart", + ], "imports": [], - "isEntry": true, + "isEntry": false, "routeFilePaths": [], }, ], @@ -571,52 +602,46 @@ exports[`matches the recorded client build with inline CSS 1`] = ` }, ], [ - "about.js", + "index.js", { "css": [ - "assets/theme.css", + "index.css", ], "dynamicImports": [ - "tooltip.js", - ], - "fileName": "about.js", - "hydrationIds": [], - "imports": [ "vendors.js", + "posts.js", + "big-shared.js", + "post.js", + "about.js", + "settings.js", + "plain.js", + "notes.js", ], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/about.js", - ], + "fileName": "index.js", + "hydrationIds": [], + "imports": [], + "isEntry": true, + "routeFilePaths": [], }, ], [ - "vendors.js", + "notes.js", { "css": [], - "dynamicImports": [ - "counter.js", - "tooltip.js", - "chart.js", - ], - "fileName": "vendors.js", + "dynamicImports": [], + "fileName": "notes.js", "hydrationIds": [], - "imports": [ - "posts.js", - "big-shared.js", - "post.js", - "about.js", - ], + "imports": [], "isEntry": false, "routeFilePaths": [], }, ], [ - "notes.js", + "plain.js", { "css": [], "dynamicImports": [], - "fileName": "notes.js", + "fileName": "plain.js", "hydrationIds": [], "imports": [], "isEntry": false, @@ -643,34 +668,6 @@ exports[`matches the recorded client build with inline CSS 1`] = ` ], }, ], - [ - "chart.js", - { - "css": [ - "chart.css", - ], - "dynamicImports": [], - "fileName": "chart.js", - "hydrationIds": [ - "chart", - ], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "plain.js", - { - "css": [], - "dynamicImports": [], - "fileName": "plain.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], [ "posts.js", { @@ -692,6 +689,22 @@ exports[`matches the recorded client build with inline CSS 1`] = ` ], }, ], + [ + "settings.js", + { + "css": [ + "settings.css", + ], + "dynamicImports": [], + "fileName": "settings.js", + "hydrationIds": [], + "imports": [], + "isEntry": false, + "routeFilePaths": [ + "/src/routes/settings.js", + ], + }, + ], [ "tooltip.js", { @@ -709,61 +722,47 @@ exports[`matches the recorded client build with inline CSS 1`] = ` }, ], [ - "big-shared.js", + "vendors.js", { "css": [], "dynamicImports": [ "counter.js", + "tooltip.js", "chart.js", ], - "fileName": "big-shared.js", + "fileName": "vendors.js", "hydrationIds": [], "imports": [ - "vendors.js", + "posts.js", + "big-shared.js", "post.js", + "about.js", ], "isEntry": false, "routeFilePaths": [], }, ], - [ - "settings.js", - { - "css": [ - "settings.css", - ], - "dynamicImports": [], - "fileName": "settings.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/settings.js", - ], - }, - ], ], "cssContentByFileName": [ [ - "counter.css", - ".counter { - color: blue; + "assets/theme.css", + ".theme { + background: white; } - ", ], [ - "tooltip.css", - ".tooltip { - color: gray; + "chart.css", + ".chart { + color: orange; } ", ], [ - "chart.css", - ".chart { - color: orange; + "counter.css", + ".counter { + color: blue; } ", @@ -777,10 +776,11 @@ exports[`matches the recorded client build with inline CSS 1`] = ` ", ], [ - "assets/theme.css", - ".theme { - background: white; + "posts.css", + ".posts { + color: red; } + ", ], [ @@ -800,9 +800,9 @@ exports[`matches the recorded client build with inline CSS 1`] = ` ", ], [ - "posts.css", - ".posts { - color: red; + "tooltip.css", + ".tooltip { + color: gray; } ", @@ -816,54 +816,25 @@ exports[`production > matches the recorded normalized client build 1`] = ` { "chunksByFileName": [ [ - "plain.js", - { - "css": [], - "dynamicImports": [], - "fileName": "plain.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "posts.js", + "about.js", { "css": [ - "posts.css", + "assets/theme.css", ], "dynamicImports": [ - "counter.js", "tooltip.js", ], - "fileName": "posts.js", + "fileName": "about.js", "hydrationIds": [], "imports": [ "vendors.js", ], "isEntry": false, "routeFilePaths": [ - "/src/routes/posts.js", + "/src/routes/about.js", ], }, ], - [ - "tooltip.js", - { - "css": [ - "tooltip.css", - ], - "dynamicImports": [], - "fileName": "tooltip.js", - "hydrationIds": [ - "tooltip", - ], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], [ "big-shared.js", { @@ -883,19 +854,35 @@ exports[`production > matches the recorded normalized client build 1`] = ` }, ], [ - "settings.js", + "chart.js", { "css": [ - "settings.css", + "chart.css", ], "dynamicImports": [], - "fileName": "settings.js", - "hydrationIds": [], + "fileName": "chart.js", + "hydrationIds": [ + "chart", + ], "imports": [], "isEntry": false, - "routeFilePaths": [ - "/src/routes/settings.js", + "routeFilePaths": [], + }, + ], + [ + "counter.js", + { + "css": [ + "counter.css", ], + "dynamicImports": [], + "fileName": "counter.js", + "hydrationIds": [ + "counter", + ], + "imports": [], + "isEntry": false, + "routeFilePaths": [], }, ], [ @@ -922,107 +909,120 @@ exports[`production > matches the recorded normalized client build 1`] = ` }, ], [ - "counter.js", + "notes.js", { - "css": [ - "counter.css", - ], + "css": [], "dynamicImports": [], - "fileName": "counter.js", - "hydrationIds": [ - "counter", - ], + "fileName": "notes.js", + "hydrationIds": [], "imports": [], "isEntry": false, "routeFilePaths": [], }, ], [ - "about.js", + "plain.js", { - "css": [ - "assets/theme.css", - ], + "css": [], + "dynamicImports": [], + "fileName": "plain.js", + "hydrationIds": [], + "imports": [], + "isEntry": false, + "routeFilePaths": [], + }, + ], + [ + "post.js", + { + "css": [], "dynamicImports": [ - "tooltip.js", + "counter.js", + "chart.js", ], - "fileName": "about.js", + "fileName": "post.js", "hydrationIds": [], "imports": [ "vendors.js", + "big-shared.js", ], "isEntry": false, "routeFilePaths": [ - "/src/routes/about.js", + "/src/routes/post.js", ], }, ], [ - "vendors.js", + "posts.js", { - "css": [], + "css": [ + "posts.css", + ], "dynamicImports": [ "counter.js", "tooltip.js", - "chart.js", ], - "fileName": "vendors.js", + "fileName": "posts.js", "hydrationIds": [], "imports": [ - "posts.js", - "big-shared.js", - "post.js", - "about.js", + "vendors.js", ], "isEntry": false, - "routeFilePaths": [], + "routeFilePaths": [ + "/src/routes/posts.js", + ], }, ], [ - "notes.js", + "settings.js", { - "css": [], + "css": [ + "settings.css", + ], "dynamicImports": [], - "fileName": "notes.js", + "fileName": "settings.js", "hydrationIds": [], "imports": [], "isEntry": false, + "routeFilePaths": [ + "/src/routes/settings.js", + ], + }, + ], + [ + "tooltip.js", + { + "css": [ + "tooltip.css", + ], + "dynamicImports": [], + "fileName": "tooltip.js", + "hydrationIds": [ + "tooltip", + ], + "imports": [], + "isEntry": false, "routeFilePaths": [], }, ], [ - "post.js", + "vendors.js", { "css": [], "dynamicImports": [ "counter.js", + "tooltip.js", "chart.js", ], - "fileName": "post.js", + "fileName": "vendors.js", "hydrationIds": [], "imports": [ - "vendors.js", + "posts.js", "big-shared.js", + "post.js", + "about.js", ], "isEntry": false, - "routeFilePaths": [ - "/src/routes/post.js", - ], - }, - ], - [ - "chart.js", - { - "css": [ - "chart.css", - ], - "dynamicImports": [], - "fileName": "chart.js", - "hydrationIds": [ - "chart", - ], - "imports": [], - "isEntry": false, "routeFilePaths": [], }, ], diff --git a/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts b/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts index 4f4c2026b1b..72fd8987411 100644 --- a/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts +++ b/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts @@ -4,6 +4,8 @@ import { buildStartManifest } from '../../src/start-manifest-plugin/manifestBuil import { FIXTURE_ROOT, compileClientFixture, + cssAssetOrder, + expectedChunkFileOrder, fixtureRouteTreeRoutes, serializeClientBuild, } from './rspack-client-build-fixture' @@ -18,9 +20,14 @@ function hasJsFile(chunk: Rspack.Chunk): boolean { describe.each(['development', 'production'])('%s', (mode) => { test('matches the recorded normalized client build', async () => { - const build = await compileClientFixture({ mode }, (compilation) => - serializeClientBuild(normalizeRspackClientBuild(compilation)), - ) + const build = await compileClientFixture({ mode }, (compilation) => { + const expectedOrder = expectedChunkFileOrder(compilation) + const clientBuild = normalizeRspackClientBuild(compilation) + expect(Array.from(clientBuild.chunksByFileName.keys())).toEqual( + expectedOrder, + ) + return serializeClientBuild(clientBuild) + }) expect(build).toMatchSnapshot() }) @@ -110,8 +117,14 @@ describe.each(['development', 'production'])('%s', (mode) => { test('matches the recorded client build with an rsc entry', async () => { const build = await compileClientFixture( { mode: 'production', withRscEntry: true }, - (compilation) => - serializeClientBuild(normalizeRspackClientBuild(compilation)), + (compilation) => { + const expectedOrder = expectedChunkFileOrder(compilation) + const clientBuild = normalizeRspackClientBuild(compilation) + expect(Array.from(clientBuild.chunksByFileName.keys())).toEqual( + expectedOrder, + ) + return serializeClientBuild(clientBuild) + }, ) expect(build).toMatchSnapshot() }) @@ -119,8 +132,18 @@ test('matches the recorded client build with an rsc entry', async () => { test('matches the recorded client build with inline CSS', async () => { const build = await compileClientFixture( { mode: 'production' }, - (compilation) => - serializeClientBuild(normalizeRspackClientBuild(compilation, true)), + (compilation) => { + const expectedOrder = expectedChunkFileOrder(compilation) + const expectedCssOrder = cssAssetOrder(compilation) + const clientBuild = normalizeRspackClientBuild(compilation, true) + expect(Array.from(clientBuild.chunksByFileName.keys())).toEqual( + expectedOrder, + ) + expect(Array.from(clientBuild.cssContentByFileName!.keys())).toEqual( + expectedCssOrder, + ) + return serializeClientBuild(clientBuild) + }, ) expect(build).toMatchSnapshot() }) diff --git a/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts b/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts index 7e835926e7e..18eea093034 100644 --- a/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts +++ b/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts @@ -137,13 +137,38 @@ export async function compileClientFixture( } } +export function expectedChunkFileOrder( + compilation: Rspack.Compilation, +): Array { + const files: Array = [] + for (const chunk of compilation.chunks) { + for (const file of chunk.files) { + if ( + (file.endsWith('.js') || file.endsWith('.mjs')) && + !file.includes('.hot-update.') + ) { + files.push(file) + } + } + } + return files +} + +export function cssAssetOrder(compilation: Rspack.Compilation): Array { + return compilation + .getAssets() + .filter((asset) => asset.name.endsWith('.css')) + .map((asset) => asset.name) +} + export function serializeClientBuild(build: NormalizedClientBuild): unknown { const fixtureRoot = FIXTURE_ROOT.replace(/\\/g, '/') return { entryChunkFileName: build.entryChunkFileName, - chunksByFileName: Array.from( - build.chunksByFileName, - ([fileName, chunk]) => [ + // Rspack's chunk and asset iteration order varies between compilations in one process. + chunksByFileName: Array.from(build.chunksByFileName) + .sort(([left], [right]) => left.localeCompare(right)) + .map(([fileName, chunk]) => [ fileName, { ...chunk, @@ -151,10 +176,11 @@ export function serializeClientBuild(build: NormalizedClientBuild): unknown { filePath.replaceAll(fixtureRoot, ''), ), }, - ], - ), + ]), cssContentByFileName: build.cssContentByFileName - ? Array.from(build.cssContentByFileName) + ? Array.from(build.cssContentByFileName).sort(([left], [right]) => + left.localeCompare(right), + ) : undefined, } } From 1ea21c3037ec20a660cf8178e978bd468aa8785e Mon Sep 17 00:00:00 2001 From: Guru Narayanan Date: Thu, 1 Oct 2026 23:50:58 +0530 Subject: [PATCH 03/10] perf(start-plugin-core): read rspack chunk graph properties once per client build capture --- .../src/rsbuild/normalized-client-build.ts | 156 ++++++++++-------- .../normalized-client-build-rspack.test.ts | 45 +++++ .../rsbuild/normalized-client-build.test.ts | 25 +++ .../rsbuild/rspack-client-build-fixture.ts | 48 ++++++ 4 files changed, 205 insertions(+), 69 deletions(-) diff --git a/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts b/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts index 126f281f951..4360e87d09d 100644 --- a/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts +++ b/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts @@ -126,83 +126,91 @@ function isHotUpdateAsset(file: string): boolean { * Excludes HMR runtime patches. */ function isManifestJsAsset(file: string): boolean { - if (!file.endsWith('.js') && !file.endsWith('.mjs')) return false + if (!file.endsWith('.js') && !file.endsWith('.mjs')) { + return false + } return !isHotUpdateAsset(file) } -/** - * Get all JS file names from a chunk. - */ -function getChunkJsFiles(chunk: RspackCompilationChunk): Array { - const jsFiles: Array = [] - for (const file of chunk.files) { - if (isManifestJsAsset(file)) { - jsFiles.push(file) +function memoizeGraphRead( + read: (target: TTarget) => Array, +): (target: TTarget) => Array { + const cache = new Map>() + return (target) => { + let value = cache.get(target) + if (!value) { + value = read(target) + cache.set(target, value) } + return value } - return jsFiles } -/** - * Compute dynamicImports for a chunk by traversing its chunk groups' - * childrenIterable (async/dynamic import edges). - * - * In rspack, a chunk belongs to one or more ChunkGroups. Each ChunkGroup - * has childrenIterable — child ChunkGroups representing dynamic import() - * points. The JS files from those child groups' chunks are the - * dynamicImports (analogous to Rollup's OutputChunk.dynamicImports). - */ -function computeDynamicImports(chunk: RspackCompilationChunk): Array { - const dynamicImportFiles: Array = [] - const seen = new Set() - - for (const group of chunk.groupsIterable) { - for (const childGroup of group.childrenIterable) { - for (const childChunk of childGroup.chunks) { - for (const file of childChunk.files) { - if (isManifestJsAsset(file) && !seen.has(file)) { - seen.add(file) - dynamicImportFiles.push(file) - } - } +function mergeJsFiles( + fileLists: Array>, + currentFile?: string, +): Array { + const files: Array = [] + const seen = new Set(currentFile === undefined ? [] : [currentFile]) + for (const list of fileLists) { + for (const file of list) { + if (!seen.has(file)) { + seen.add(file) + files.push(file) } } } - - return dynamicImportFiles + return files } /** - * Compute static imports (sibling chunks) for an async chunk. - * - * In rspack/webpack, an async chunk's ChunkGroup contains ALL chunks needed to - * satisfy that dynamic import — the async chunk itself plus any shared/vendor - * chunks it statically imports. This is analogous to Rollup's - * `OutputChunk.imports` for async chunks. - * - * We collect JS files from all sibling chunks in the group (excluding the - * current chunk's own file) to populate the `imports` field. + * Rspack's graph getters allocate collections on each read. Keep this cache local + * to one capture so rebuilds see fresh graph data. */ -function computeAsyncChunkImports( - chunk: RspackCompilationChunk, - currentFile: string, -): Array { - const imports: Array = [] - const seen = new Set() - seen.add(currentFile) - - for (const group of chunk.groupsIterable) { - for (const siblingChunk of group.chunks) { - for (const file of siblingChunk.files) { - if (isManifestJsAsset(file) && !seen.has(file)) { - seen.add(file) - imports.push(file) - } - } - } - } +function createChunkGraphReader() { + const getFiles = memoizeGraphRead((chunk: RspackCompilationChunk) => + Array.from(chunk.files), + ) + const getAuxiliaryFiles = memoizeGraphRead((chunk: RspackCompilationChunk) => + Array.from(chunk.auxiliaryFiles), + ) + const getGroups = memoizeGraphRead((chunk: RspackCompilationChunk) => + Array.from(chunk.groupsIterable), + ) + const getGroupChunks = memoizeGraphRead((group: Rspack.ChunkGroup) => + Array.from(group.chunks), + ) + const getChildren = memoizeGraphRead((group: Rspack.ChunkGroup) => + Array.from(group.childrenIterable), + ) + const getChunkJsFiles = memoizeGraphRead((chunk: RspackCompilationChunk) => + getFiles(chunk).filter(isManifestJsAsset), + ) - return imports + // Each group supplies its sibling imports and its direct dynamic-import edges. + // Deduping within a group before merging preserves global first-occurrence order. + const getGroupJsFiles = memoizeGraphRead((group: Rspack.ChunkGroup) => + mergeJsFiles(getGroupChunks(group).map(getChunkJsFiles)), + ) + const getChildGroupJsFiles = memoizeGraphRead((group: Rspack.ChunkGroup) => + mergeJsFiles(getChildren(group).map(getGroupJsFiles)), + ) + + return { + getFiles, + getAuxiliaryFiles, + getGroupChunks, + getChunkJsFiles, + computeDynamicImports(chunk: RspackCompilationChunk) { + return mergeJsFiles(getGroups(chunk).map(getChildGroupJsFiles)) + }, + computeAsyncChunkImports( + chunk: RspackCompilationChunk, + currentFile: string, + ) { + return mergeJsFiles(getGroups(chunk).map(getGroupJsFiles), currentFile) + }, + } } /** @@ -215,6 +223,14 @@ export function normalizeRspackClientBuild( compilation: RspackCompilation, inlineCssEnabled = false, ): NormalizedClientBuild { + const { + getFiles, + getAuxiliaryFiles, + getGroupChunks, + getChunkJsFiles, + computeDynamicImports, + computeAsyncChunkImports, + } = createChunkGraphReader() const chunksByFileName = new Map() let cssContentByFileName: Map | undefined let entryChunkFileName: string | undefined @@ -225,9 +241,9 @@ export function normalizeRspackClientBuild( const initialJsFileNames: Array = [] const entryChunkSet = new Set() if (entrypoint) { - for (const chunk of entrypoint.chunks) { + for (const chunk of getGroupChunks(entrypoint)) { entryChunkSet.add(chunk) - for (const file of chunk.files) { + for (const file of getFiles(chunk)) { if (isManifestJsAsset(file)) { initialJsFileNames.push(file) } @@ -243,14 +259,14 @@ export function normalizeRspackClientBuild( const cssFiles: Array = [] const seenCssFiles = new Set() - for (const auxFile of chunk.auxiliaryFiles) { + for (const auxFile of getAuxiliaryFiles(chunk)) { if (auxFile.endsWith('.css') && !seenCssFiles.has(auxFile)) { seenCssFiles.add(auxFile) cssFiles.push(auxFile) } } - for (const mainFile of chunk.files) { + for (const mainFile of getFiles(chunk)) { if (mainFile.endsWith('.css') && !seenCssFiles.has(mainFile)) { seenCssFiles.add(mainFile) cssFiles.push(mainFile) @@ -261,7 +277,9 @@ export function normalizeRspackClientBuild( const isEntryChunk = chunk.name === 'index' && entryChunkSet.has(chunk) const jsFiles = getChunkJsFiles(chunk) - if (jsFiles.length === 0) continue + if (jsFiles.length === 0) { + continue + } // Compute dynamicImports from chunk group children const dynamicImports = computeDynamicImports(chunk) @@ -332,8 +350,8 @@ export function normalizeRspackClientBuild( if (rscEntrypoint && entryChunkFileName) { const mainEntryChunk = chunksByFileName.get(entryChunkFileName) if (mainEntryChunk) { - for (const rscChunk of rscEntrypoint.chunks) { - const allFiles = [...rscChunk.files, ...rscChunk.auxiliaryFiles] + for (const rscChunk of getGroupChunks(rscEntrypoint)) { + const allFiles = [...getFiles(rscChunk), ...getAuxiliaryFiles(rscChunk)] for (const file of allFiles) { if (file.endsWith('.css') && !mainEntryChunk.css.includes(file)) { mainEntryChunk.css.push(file) diff --git a/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts b/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts index 72fd8987411..8c182f95dc2 100644 --- a/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts +++ b/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts @@ -4,6 +4,7 @@ import { buildStartManifest } from '../../src/start-manifest-plugin/manifestBuil import { FIXTURE_ROOT, compileClientFixture, + countPropertyReads, cssAssetOrder, expectedChunkFileOrder, fixtureRouteTreeRoutes, @@ -129,6 +130,50 @@ test('matches the recorded client build with an rsc entry', async () => { expect(build).toMatchSnapshot() }) +describe.each<{ mode: FixtureMode; withRscEntry: boolean }>([ + { mode: 'development', withRscEntry: false }, + { mode: 'production', withRscEntry: false }, + { mode: 'production', withRscEntry: true }, +])('$mode, rsc=$withRscEntry', (options) => { + test('reads each chunk and chunk group graph property at most once', async () => { + await compileClientFixture(options, (compilation) => { + const chunkReads = countPropertyReads( + compilation.chunks.values().next().value!, + ['files', 'auxiliaryFiles', 'groupsIterable'], + ) + const groupReads = countPropertyReads( + compilation.entrypoints.get('index')!, + ['chunks', 'childrenIterable'], + ) + try { + normalizeRspackClientBuild(compilation) + } finally { + groupReads.restore() + chunkReads.restore() + } + + const maxReadsByProperty = new Map() + for (const counter of [chunkReads, groupReads]) { + for (const reads of counter.readsByReceiver.values()) { + for (const [property, count] of reads) { + maxReadsByProperty.set( + property, + Math.max(maxReadsByProperty.get(property) ?? 0, count), + ) + } + } + } + expect(maxReadsByProperty.size).toBe(5) + for (const [property, maxCount] of maxReadsByProperty) { + expect( + maxCount, + `${property}: max reads per receiver = ${maxCount}`, + ).toBeLessThanOrEqual(1) + } + }) + }) +}) + test('matches the recorded client build with inline CSS', async () => { const build = await compileClientFixture( { mode: 'production' }, diff --git a/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts b/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts index 14546022a48..155360b626c 100644 --- a/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts +++ b/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts @@ -83,6 +83,31 @@ function makeWindowsCompilation(readCss: () => string | Uint8Array) { } describe('normalizeRspackClientBuild', () => { + test('keeps hot-update files out of entries and imports', () => { + const entryChunk = { + name: 'index', + files: new Set(['index.js', 'index.abc123.hot-update.js', 'root.css']), + auxiliaryFiles: new Set(), + groupsIterable: new Set(), + } + const compilation = { + entrypoints: new Map([['index', { chunks: [entryChunk] }]]), + chunks: new Set([entryChunk]), + chunkGraph: { getChunkModules: () => [] }, + } as unknown as Rspack.Compilation + + const clientBuild = normalizeRspackClientBuild(compilation) + + expect(clientBuild.entryChunkFileName).toBe('index.js') + expect( + Array.from(clientBuild.chunksByFileName.keys()).some((file) => + file.includes('.hot-update.'), + ), + ).toBe(false) + expect(clientBuild.chunksByFileName.get('index.js')?.isEntry).toBe(true) + expect(clientBuild.chunksByFileName.get('index.js')?.imports).toEqual([]) + }) + test('keeps route stylesheet links with inline CSS disabled by default', () => { const compilation = makeCompilation(() => '.card{color:red}') const clientBuild = normalizeRspackClientBuild(compilation) diff --git a/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts b/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts index 18eea093034..c40f43997cb 100644 --- a/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts +++ b/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts @@ -137,6 +137,54 @@ export async function compileClientFixture( } } +export function countPropertyReads( + target: object, + names: ReadonlyArray, +): { + readsByReceiver: Map> + restore: () => void +} { + const readsByReceiver = new Map>() + const originals = names.map((name) => { + let owner: object | null = Object.getPrototypeOf(target) + while (owner) { + const descriptor = Object.getOwnPropertyDescriptor(owner, name) + if (descriptor) { + if (!descriptor.get || !descriptor.configurable) { + throw new Error(`Expected a configurable getter for ${name}`) + } + return { owner, name, descriptor } + } + owner = Object.getPrototypeOf(owner) + } + throw new Error(`No prototype getter found for ${name}`) + }) + + for (const { owner, name, descriptor } of originals) { + Object.defineProperty(owner, name, { + ...descriptor, + get(this: object) { + let reads = readsByReceiver.get(this) + if (!reads) { + reads = new Map() + readsByReceiver.set(this, reads) + } + reads.set(name, (reads.get(name) ?? 0) + 1) + return descriptor.get!.call(this) + }, + }) + } + + return { + readsByReceiver, + restore() { + for (const { owner, name, descriptor } of originals) { + Object.defineProperty(owner, name, descriptor) + } + }, + } +} + export function expectedChunkFileOrder( compilation: Rspack.Compilation, ): Array { From 10858d22feb57ad243d158ce00527175d1633876 Mon Sep 17 00:00:00 2001 From: Guru Narayanan Date: Fri, 2 Oct 2026 00:09:24 +0530 Subject: [PATCH 04/10] perf(start-plugin-core): scan rspack modules once to find route and hydration chunks --- .changeset/rsbuild-client-build-capture.md | 5 +++ .../src/rsbuild/normalized-client-build.ts | 25 ++++++++++-- .../normalized-client-build-rspack.test.ts | 32 +++++++++++++++ .../rsbuild/normalized-client-build.test.ts | 40 ++++++++++--------- .../rsbuild/rspack-client-build-fixture.ts | 34 ++++++++++++++++ 5 files changed, 115 insertions(+), 21 deletions(-) create mode 100644 .changeset/rsbuild-client-build-capture.md diff --git a/.changeset/rsbuild-client-build-capture.md b/.changeset/rsbuild-client-build-capture.md new file mode 100644 index 00000000000..a587a6d91b5 --- /dev/null +++ b/.changeset/rsbuild-client-build-capture.md @@ -0,0 +1,5 @@ +--- +'@tanstack/start-plugin-core': patch +--- + +Speed up the Rsbuild client build capture on apps with many chunks by reading the Rspack chunk graph once per build. diff --git a/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts b/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts index 4360e87d09d..f74b4b6cf64 100644 --- a/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts +++ b/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts @@ -18,6 +18,21 @@ type RspackModule = Rspack.Module const backslashRegex = /\\/g +function findTaggedChunks( + compilation: RspackCompilation, +): Set { + const taggedChunks = new Set() + for (const mod of compilation.modules) { + const identifier = mod.identifier() + if (identifier.includes(tsrSplit) || identifier.includes(tssHydrate)) { + for (const chunk of compilation.chunkGraph.getModuleChunks(mod)) { + taggedChunks.add(chunk) + } + } + } + return taggedChunks +} + /** * Convert an OS native path to the POSIX form used by the generated route tree. */ @@ -251,11 +266,15 @@ export function normalizeRspackClientBuild( } } + const taggedChunks = findTaggedChunks(compilation) + // Iterate ALL chunks (initial + async) to capture route-split chunks for (const chunk of compilation.chunks) { - const modules = compilation.chunkGraph.getChunkModules(chunk) - const routeFilePaths = getRouteFilePathsFromModules(modules) - const hydrationIds = getHydrationIdsFromModules(modules) + const modules = taggedChunks.has(chunk) + ? compilation.chunkGraph.getChunkModules(chunk) + : undefined + const routeFilePaths = modules ? getRouteFilePathsFromModules(modules) : [] + const hydrationIds = modules ? getHydrationIdsFromModules(modules) : [] const cssFiles: Array = [] const seenCssFiles = new Set() diff --git a/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts b/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts index 8c182f95dc2..1a3c8eb53dd 100644 --- a/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts +++ b/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts @@ -8,6 +8,7 @@ import { cssAssetOrder, expectedChunkFileOrder, fixtureRouteTreeRoutes, + recordChunkModuleCalls, serializeClientBuild, } from './rspack-client-build-fixture' import type { Rspack } from '@rsbuild/core' @@ -47,6 +48,37 @@ describe.each(['development', 'production'])('%s', (mode) => { expect(JSON.parse(serialized)).toMatchSnapshot() }) + test('lists chunk modules only for chunks that hold a route split or hydration module', async () => { + await compileClientFixture({ mode }, (compilation) => { + const taggedChunks = new Set( + Array.from(compilation.chunks).filter((chunk) => + compilation.chunkGraph.getChunkModules(chunk).some((module) => { + const identifier = module.identifier() + return ( + identifier.includes('tsr-split') || + identifier.includes('tss-hydrate') + ) + }), + ), + ) + let calls: ReturnType | undefined + try { + calls = recordChunkModuleCalls(compilation.chunkGraph) + normalizeRspackClientBuild(compilation) + } finally { + calls?.restore() + } + + const listedChunks = new Set(calls.chunks) + expect(listedChunks).toEqual(taggedChunks) + expect(listedChunks.size).toBe(taggedChunks.size) + expect(calls.chunks).toHaveLength(taggedChunks.size) + for (const chunk of calls.chunks) { + expect(taggedChunks.has(chunk)).toBe(true) + } + }) + }) + test('fixture exercises the shapes the normalizer must preserve', async () => { await compileClientFixture({ mode }, (compilation) => { const chunks = Array.from(compilation.chunks) diff --git a/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts b/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts index 155360b626c..72f11b6002d 100644 --- a/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts +++ b/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts @@ -23,19 +23,19 @@ function makeCompilation(readCss: () => string | Uint8Array) { { name: 'root.css', source: { source: readCss } }, { name: 'posts.css', source: { source: readCss } }, ] + const routeModule = { + identifier: () => '/routes/posts.tsx?tsr-split=component', + nameForCondition: () => '/routes/posts.tsx', + } const compilation = { entrypoints: new Map([['index', { chunks: [entryChunk] }]]), chunks: new Set([entryChunk, routeChunk]), + modules: new Set([routeModule]), chunkGraph: { getChunkModules: (chunk: unknown) => - chunk === routeChunk - ? [ - { - identifier: () => '/routes/posts.tsx?tsr-split=component', - nameForCondition: () => '/routes/posts.tsx', - }, - ] - : [], + chunk === routeChunk ? [routeModule] : [], + getModuleChunks: (module: unknown) => + module === routeModule ? [routeChunk] : [], }, getAssets, } as unknown as Rspack.Compilation @@ -62,20 +62,20 @@ function makeWindowsCompilation(readCss: () => string | Uint8Array) { { name: 'root.css', source: { source: readCss } }, { name: 'posts.css', source: { source: readCss } }, ] + const routeModule = { + identifier: () => + 'builtin:swc-loader??ruleSet[0]!C:\\app\\src\\routes\\posts.tsx?tsr-split=component', + nameForCondition: () => 'C:\\app\\src\\routes\\posts.tsx', + } const compilation = { entrypoints: new Map([['index', { chunks: [entryChunk] }]]), chunks: new Set([entryChunk, routeChunk]), + modules: new Set([routeModule]), chunkGraph: { getChunkModules: (chunk: unknown) => - chunk === routeChunk - ? [ - { - identifier: () => - 'builtin:swc-loader??ruleSet[0]!C:\\app\\src\\routes\\posts.tsx?tsr-split=component', - nameForCondition: () => 'C:\\app\\src\\routes\\posts.tsx', - }, - ] - : [], + chunk === routeChunk ? [routeModule] : [], + getModuleChunks: (module: unknown) => + module === routeModule ? [routeChunk] : [], }, getAssets, } as unknown as Rspack.Compilation @@ -93,7 +93,11 @@ describe('normalizeRspackClientBuild', () => { const compilation = { entrypoints: new Map([['index', { chunks: [entryChunk] }]]), chunks: new Set([entryChunk]), - chunkGraph: { getChunkModules: () => [] }, + modules: new Set(), + chunkGraph: { + getChunkModules: () => [], + getModuleChunks: () => [], + }, } as unknown as Rspack.Compilation const clientBuild = normalizeRspackClientBuild(compilation) diff --git a/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts b/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts index c40f43997cb..3edc2f7a5bf 100644 --- a/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts +++ b/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts @@ -185,6 +185,40 @@ export function countPropertyReads( } } +export function recordChunkModuleCalls( + chunkGraph: Rspack.Compilation['chunkGraph'], +): { + chunks: Array + restore: () => void +} { + const chunks: Array = [] + let owner: object | null = Object.getPrototypeOf(chunkGraph) + while (owner) { + const descriptor = Object.getOwnPropertyDescriptor(owner, 'getChunkModules') + if (descriptor) { + if (typeof descriptor.value !== 'function' || !descriptor.configurable) { + throw new Error('Expected a configurable getChunkModules method') + } + const methodOwner = owner + Object.defineProperty(methodOwner, 'getChunkModules', { + ...descriptor, + value(this: Rspack.Compilation['chunkGraph'], chunk: Rspack.Chunk) { + chunks.push(chunk) + return descriptor.value.call(this, chunk) + }, + }) + return { + chunks, + restore() { + Object.defineProperty(methodOwner, 'getChunkModules', descriptor) + }, + } + } + owner = Object.getPrototypeOf(owner) + } + throw new Error('No prototype method found for getChunkModules') +} + export function expectedChunkFileOrder( compilation: Rspack.Compilation, ): Array { From 42cba76d748b535e78a30a92d04b8b59a496ac72 Mon Sep 17 00:00:00 2001 From: Guru Narayanan Date: Fri, 2 Oct 2026 00:45:02 +0530 Subject: [PATCH 05/10] test(start-plugin-core): tighten rspack client build golden tests --- ...ormalized-client-build-rspack.test.ts.snap | 517 ++---------------- .../rspack-client-build/src/islands/chart.js | 4 - .../src/islands/counter.js | 4 - .../src/islands/tooltip.js | 4 - .../src/routes/settings.js | 6 - .../fixtures/rspack-client-build/src/rsc.js | 4 - .../normalized-client-build-rspack.test.ts | 275 ++++++---- .../rsbuild/rspack-client-build-fixture.ts | 12 +- .../rspack-client-build/src/index.js | 8 +- .../rspack-client-build/src/islands/chart.css | 0 .../rspack-client-build/src/islands/chart.js | 3 + .../src/islands/counter.css | 0 .../src/islands/counter.js | 3 + .../src/islands/tooltip.css | 0 .../src/islands/tooltip.js | 3 + .../rspack-client-build/src/lazy/plain.js | 0 .../src/lazy/tsr-split-notes.js | 0 .../rspack-client-build/src/root.css | 0 .../rspack-client-build/src/routes/about.js | 1 - .../rspack-client-build/src/routes/post.js | 2 - .../src/routes/posts-helper.js | 1 - .../rspack-client-build/src/routes/posts.css | 0 .../rspack-client-build/src/routes/posts.js | 3 - .../src/routes/settings.css | 0 .../src/routes/settings.js | 4 + .../rspack-client-build/src/rsc.css | 0 .../test-files/rspack-client-build/src/rsc.js | 3 + .../src/shared/big-shared.js | 0 .../src/shared/runtime-shared.js | 0 .../src/shared/shared-styles.css | 0 .../src/shared/vendor-a.js | 0 .../src/shared/vendor-b.js | 1 - .../rspack-client-build/src/theme.css | 0 33 files changed, 218 insertions(+), 640 deletions(-) delete mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/chart.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/counter.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/tooltip.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/settings.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/rsc.js rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/index.js (64%) rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/islands/chart.css (100%) create mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/chart.js rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/islands/counter.css (100%) create mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/counter.js rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/islands/tooltip.css (100%) create mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/tooltip.js rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/lazy/plain.js (100%) rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/lazy/tsr-split-notes.js (100%) rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/root.css (100%) rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/routes/about.js (64%) rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/routes/post.js (70%) rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/routes/posts-helper.js (65%) rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/routes/posts.css (100%) rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/routes/posts.js (63%) rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/routes/settings.css (100%) create mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/settings.js rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/rsc.css (100%) create mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/rsc.js rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/shared/big-shared.js (100%) rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/shared/runtime-shared.js (100%) rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/shared/shared-styles.css (100%) rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/shared/vendor-a.js (100%) rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/shared/vendor-b.js (66%) rename packages/start-plugin-core/tests/rsbuild/{fixtures => test-files}/rspack-client-build/src/theme.css (100%) diff --git a/packages/start-plugin-core/tests/rsbuild/__snapshots__/normalized-client-build-rspack.test.ts.snap b/packages/start-plugin-core/tests/rsbuild/__snapshots__/normalized-client-build-rspack.test.ts.snap index 3422bc8ed36..e7e04bdbc32 100644 --- a/packages/start-plugin-core/tests/rsbuild/__snapshots__/normalized-client-build-rspack.test.ts.snap +++ b/packages/start-plugin-core/tests/rsbuild/__snapshots__/normalized-client-build-rspack.test.ts.snap @@ -293,523 +293,72 @@ exports[`development > matches the recorded start manifest 1`] = ` } `; -exports[`matches the recorded client build with an rsc entry 1`] = ` -{ - "chunksByFileName": [ - [ - "about.js", - { - "css": [ - "assets/theme.css", - ], - "dynamicImports": [ - "tooltip.js", - ], - "fileName": "about.js", - "hydrationIds": [], - "imports": [ - "vendors.js", - ], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/about.js", - ], - }, - ], - [ - "big-shared.js", - { - "css": [], - "dynamicImports": [ - "counter.js", - "chart.js", - ], - "fileName": "big-shared.js", - "hydrationIds": [], - "imports": [ - "vendors.js", - "post.js", - ], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "chart.js", - { - "css": [ - "chart.css", - ], - "dynamicImports": [], - "fileName": "chart.js", - "hydrationIds": [ - "chart", - ], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "counter.js", - { - "css": [ - "counter.css", - ], - "dynamicImports": [], - "fileName": "counter.js", - "hydrationIds": [ - "counter", - ], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "index.js", - { - "css": [ - "index.css", - "rsc.css", - ], - "dynamicImports": [ - "vendors.js", - "posts.js", - "big-shared.js", - "post.js", - "about.js", - "settings.js", - "plain.js", - "notes.js", - ], - "fileName": "index.js", - "hydrationIds": [], - "imports": [], - "isEntry": true, - "routeFilePaths": [], - }, - ], - [ - "notes.js", - { - "css": [], - "dynamicImports": [], - "fileName": "notes.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "plain.js", - { - "css": [], - "dynamicImports": [], - "fileName": "plain.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "post.js", - { - "css": [], - "dynamicImports": [ - "counter.js", - "chart.js", - ], - "fileName": "post.js", - "hydrationIds": [], - "imports": [ - "vendors.js", - "big-shared.js", - ], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/post.js", - ], - }, - ], - [ - "posts.js", - { - "css": [ - "posts.css", - ], - "dynamicImports": [ - "counter.js", - "tooltip.js", - ], - "fileName": "posts.js", - "hydrationIds": [], - "imports": [ - "vendors.js", - ], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/posts.js", - ], - }, - ], - [ - "rsc.js", - { - "css": [ - "rsc.css", - ], - "dynamicImports": [], - "fileName": "rsc.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "settings.js", - { - "css": [ - "settings.css", - ], - "dynamicImports": [], - "fileName": "settings.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/settings.js", - ], - }, - ], - [ - "tooltip.js", - { - "css": [ - "tooltip.css", - ], - "dynamicImports": [], - "fileName": "tooltip.js", - "hydrationIds": [ - "tooltip", - ], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "vendors.js", - { - "css": [], - "dynamicImports": [ - "counter.js", - "tooltip.js", - "chart.js", - ], - "fileName": "vendors.js", - "hydrationIds": [], - "imports": [ - "posts.js", - "big-shared.js", - "post.js", - "about.js", - ], - "isEntry": false, - "routeFilePaths": [], - }, - ], - ], - "cssContentByFileName": undefined, - "entryChunkFileName": "index.js", -} -`; - exports[`matches the recorded client build with inline CSS 1`] = ` -{ - "chunksByFileName": [ - [ - "about.js", - { - "css": [ - "assets/theme.css", - ], - "dynamicImports": [ - "tooltip.js", - ], - "fileName": "about.js", - "hydrationIds": [], - "imports": [ - "vendors.js", - ], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/about.js", - ], - }, - ], - [ - "big-shared.js", - { - "css": [], - "dynamicImports": [ - "counter.js", - "chart.js", - ], - "fileName": "big-shared.js", - "hydrationIds": [], - "imports": [ - "vendors.js", - "post.js", - ], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "chart.js", - { - "css": [ - "chart.css", - ], - "dynamicImports": [], - "fileName": "chart.js", - "hydrationIds": [ - "chart", - ], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "counter.js", - { - "css": [ - "counter.css", - ], - "dynamicImports": [], - "fileName": "counter.js", - "hydrationIds": [ - "counter", - ], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "index.js", - { - "css": [ - "index.css", - ], - "dynamicImports": [ - "vendors.js", - "posts.js", - "big-shared.js", - "post.js", - "about.js", - "settings.js", - "plain.js", - "notes.js", - ], - "fileName": "index.js", - "hydrationIds": [], - "imports": [], - "isEntry": true, - "routeFilePaths": [], - }, - ], - [ - "notes.js", - { - "css": [], - "dynamicImports": [], - "fileName": "notes.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "plain.js", - { - "css": [], - "dynamicImports": [], - "fileName": "plain.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "post.js", - { - "css": [], - "dynamicImports": [ - "counter.js", - "chart.js", - ], - "fileName": "post.js", - "hydrationIds": [], - "imports": [ - "vendors.js", - "big-shared.js", - ], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/post.js", - ], - }, - ], - [ - "posts.js", - { - "css": [ - "posts.css", - ], - "dynamicImports": [ - "counter.js", - "tooltip.js", - ], - "fileName": "posts.js", - "hydrationIds": [], - "imports": [ - "vendors.js", - ], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/posts.js", - ], - }, - ], - [ - "settings.js", - { - "css": [ - "settings.css", - ], - "dynamicImports": [], - "fileName": "settings.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/settings.js", - ], - }, - ], - [ - "tooltip.js", - { - "css": [ - "tooltip.css", - ], - "dynamicImports": [], - "fileName": "tooltip.js", - "hydrationIds": [ - "tooltip", - ], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "vendors.js", - { - "css": [], - "dynamicImports": [ - "counter.js", - "tooltip.js", - "chart.js", - ], - "fileName": "vendors.js", - "hydrationIds": [], - "imports": [ - "posts.js", - "big-shared.js", - "post.js", - "about.js", - ], - "isEntry": false, - "routeFilePaths": [], - }, - ], - ], - "cssContentByFileName": [ - [ - "assets/theme.css", - ".theme { +[ + [ + "assets/theme.css", + ".theme { background: white; } ", - ], - [ - "chart.css", - ".chart { + ], + [ + "chart.css", + ".chart { color: orange; } ", - ], - [ - "counter.css", - ".counter { + ], + [ + "counter.css", + ".counter { color: blue; } ", - ], - [ - "index.css", - ".root { + ], + [ + "index.css", + ".root { color: black; } ", - ], - [ - "posts.css", - ".posts { + ], + [ + "posts.css", + ".posts { color: red; } ", - ], - [ - "settings.css", - ".settings { + ], + [ + "settings.css", + ".settings { color: green; } ", - ], - [ - "shared-styles.css", - ".shared { + ], + [ + "shared-styles.css", + ".shared { font-weight: bold; } ", - ], - [ - "tooltip.css", - ".tooltip { + ], + [ + "tooltip.css", + ".tooltip { color: gray; } ", - ], ], - "entryChunkFileName": "index.js", -} +] `; exports[`production > matches the recorded normalized client build 1`] = ` diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/chart.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/chart.js deleted file mode 100644 index 9613ae091d3..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/chart.js +++ /dev/null @@ -1,4 +0,0 @@ -// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. -import './chart.css' - -export const chart = 'chart' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/counter.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/counter.js deleted file mode 100644 index 4b5a6770327..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/counter.js +++ /dev/null @@ -1,4 +0,0 @@ -// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. -import './counter.css' - -export const counter = 'counter' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/tooltip.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/tooltip.js deleted file mode 100644 index 17caeefd75e..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/tooltip.js +++ /dev/null @@ -1,4 +0,0 @@ -// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. -import './tooltip.css' - -export const tooltip = 'tooltip' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/settings.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/settings.js deleted file mode 100644 index e4b2ba60325..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/settings.js +++ /dev/null @@ -1,6 +0,0 @@ -// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. -import './settings.css' -// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. -import '../shared/shared-styles.css' - -export const settings = 'settings' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/rsc.js b/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/rsc.js deleted file mode 100644 index 334f1dd6cb0..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/rsc.js +++ /dev/null @@ -1,4 +0,0 @@ -// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. -import './rsc.css' - -Object.assign(globalThis, { fixtureRsc: 'rsc' }) diff --git a/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts b/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts index 1a3c8eb53dd..6a5ce07a0a8 100644 --- a/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts +++ b/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts @@ -22,14 +22,19 @@ function hasJsFile(chunk: Rspack.Chunk): boolean { describe.each(['development', 'production'])('%s', (mode) => { test('matches the recorded normalized client build', async () => { - const build = await compileClientFixture({ mode }, (compilation) => { - const expectedOrder = expectedChunkFileOrder(compilation) - const clientBuild = normalizeRspackClientBuild(compilation) - expect(Array.from(clientBuild.chunksByFileName.keys())).toEqual( - expectedOrder, - ) - return serializeClientBuild(clientBuild) - }) + const { build, actualOrder, expectedOrder } = await compileClientFixture( + { mode }, + (compilation) => { + const expectedOrder = expectedChunkFileOrder(compilation) + const clientBuild = normalizeRspackClientBuild(compilation) + return { + build: serializeClientBuild(clientBuild), + actualOrder: Array.from(clientBuild.chunksByFileName.keys()), + expectedOrder, + } + }, + ) + expect(actualOrder).toEqual(expectedOrder) expect(build).toMatchSnapshot() }) @@ -49,7 +54,7 @@ describe.each(['development', 'production'])('%s', (mode) => { }) test('lists chunk modules only for chunks that hold a route split or hydration module', async () => { - await compileClientFixture({ mode }, (compilation) => { + const facts = await compileClientFixture({ mode }, (compilation) => { const taggedChunks = new Set( Array.from(compilation.chunks).filter((chunk) => compilation.chunkGraph.getChunkModules(chunk).some((module) => { @@ -69,97 +74,126 @@ describe.each(['development', 'production'])('%s', (mode) => { calls?.restore() } - const listedChunks = new Set(calls.chunks) - expect(listedChunks).toEqual(taggedChunks) - expect(listedChunks.size).toBe(taggedChunks.size) - expect(calls.chunks).toHaveLength(taggedChunks.size) - for (const chunk of calls.chunks) { - expect(taggedChunks.has(chunk)).toBe(true) + return { + taggedChunkCount: taggedChunks.size, + totalChunkCount: compilation.chunks.size, + listedChunkCount: new Set(calls.chunks).size, + recordedCalls: calls.chunks.map((chunk) => ({ + name: chunk.name, + isTagged: taggedChunks.has(chunk), + })), } }) + expect(facts.taggedChunkCount).toBeGreaterThan(0) + expect(facts.taggedChunkCount).toBeLessThan(facts.totalChunkCount) + expect(facts.listedChunkCount).toBe(facts.taggedChunkCount) + expect(facts.recordedCalls).toHaveLength(facts.taggedChunkCount) + for (const call of facts.recordedCalls) { + expect(call.isTagged, `getChunkModules(${call.name})`).toBe(true) + } }) test('fixture exercises the shapes the normalizer must preserve', async () => { - await compileClientFixture({ mode }, (compilation) => { + const facts = await compileClientFixture({ mode }, (compilation) => { const chunks = Array.from(compilation.chunks) const groups = Array.from(compilation.chunkGroups) + const tooltipChunks = new Set( + chunks.filter((chunk) => + compilation.chunkGraph + .getChunkModules(chunk) + .some((module) => + module + .identifier() + .replace(/\\/g, '/') + .includes('/islands/tooltip.js?tss-hydrate=tooltip'), + ), + ), + ) - expect( - groups.some((group) => group.chunks.filter(hasJsFile).length >= 2), - 'a group has sibling JS chunks', - ).toBe(true) - expect( - chunks.some((chunk) => Array.from(chunk.groupsIterable).length >= 2), - 'a shared chunk belongs to multiple groups', - ).toBe(true) - expect( - chunks.some((chunk) => !hasJsFile(chunk)), - 'a CSS-only chunk is present', - ).toBe(true) - expect( - chunks.some((chunk) => + return { + hasSiblingJsChunks: groups.some( + (group) => group.chunks.filter(hasJsFile).length >= 2, + ), + hasSharedChunk: chunks.some( + (chunk) => Array.from(chunk.groupsIterable).length >= 2, + ), + hasCssOnlyChunk: chunks.some((chunk) => !hasJsFile(chunk)), + hasAuxiliaryCss: chunks.some((chunk) => Array.from(chunk.auxiliaryFiles).some((file) => file.endsWith('.css'), ), ), - 'CSS assets appear in auxiliaryFiles', - ).toBe(true) - expect( - chunks.some( + hasTwoPostsSplits: chunks.some( (chunk) => compilation.chunkGraph.getChunkModules(chunk).filter((module) => { const identifier = module.identifier().replace(/\\/g, '/') return identifier.includes('/routes/posts.js?tsr-split=') }).length === 2, ), - 'two posts split modules share a chunk', - ).toBe(true) - - const hydrationChunks = new Set( - chunks.filter((chunk) => - compilation.chunkGraph - .getChunkModules(chunk) - .some((module) => module.identifier().includes('?tss-hydrate=')), - ), - ) - expect( - groups.some( + hasTooltipThroughVendors: groups.some( (group) => group.chunks.some((chunk) => chunk.name === 'vendors') && Array.from(group.childrenIterable).some((child) => - child.chunks.some((chunk) => hydrationChunks.has(chunk)), + child.chunks.some((chunk) => tooltipChunks.has(chunk)), ), ), - 'hydration is reachable through a vendors group', - ).toBe(true) - - if (mode === 'production') { - expect( + hasPostsConcatenationRoot: + mode === 'production' && Array.from(compilation.modules).some( (module) => module.constructor.name === 'ConcatenatedModule' && module.identifier().includes('posts.js?tsr-split='), ), - 'a posts split is a concatenation root', - ).toBe(true) } }) + expect(facts.hasSiblingJsChunks, 'a group has sibling JS chunks').toBe(true) + expect( + facts.hasSharedChunk, + 'a shared chunk belongs to multiple groups', + ).toBe(true) + expect(facts.hasCssOnlyChunk, 'a CSS-only chunk is present').toBe(true) + expect(facts.hasAuxiliaryCss, 'CSS assets appear in auxiliaryFiles').toBe( + true, + ) + expect( + facts.hasTwoPostsSplits, + 'two posts split modules share a chunk', + ).toBe(true) + expect( + facts.hasTooltipThroughVendors, + 'tooltip is reachable through a vendors group', + ).toBe(true) + if (mode === 'production') { + expect( + facts.hasPostsConcatenationRoot, + 'a posts split is a concatenation root', + ).toBe(true) + } }) }) -test('matches the recorded client build with an rsc entry', async () => { - const build = await compileClientFixture( - { mode: 'production', withRscEntry: true }, - (compilation) => { - const expectedOrder = expectedChunkFileOrder(compilation) - const clientBuild = normalizeRspackClientBuild(compilation) - expect(Array.from(clientBuild.chunksByFileName.keys())).toEqual( - expectedOrder, - ) - return serializeClientBuild(clientBuild) - }, - ) - expect(build).toMatchSnapshot() +test('merges rsc CSS into the main entry without making rsc the entry', async () => { + const { indexChunk, rscChunk, actualOrder, expectedOrder } = + await compileClientFixture( + { mode: 'production', withRscEntry: true }, + (compilation) => { + const expectedOrder = expectedChunkFileOrder(compilation) + const clientBuild = normalizeRspackClientBuild(compilation) + return { + indexChunk: clientBuild.chunksByFileName.get('index.js'), + rscChunk: clientBuild.chunksByFileName.get('rsc.js'), + actualOrder: Array.from(clientBuild.chunksByFileName.keys()), + expectedOrder, + } + }, + ) + expect(actualOrder).toEqual(expectedOrder) + expect(indexChunk?.css.at(-1)).toBe('rsc.css') + expect( + indexChunk?.css.filter((fileName) => fileName === 'rsc.css'), + ).toHaveLength(1) + expect(rscChunk).toBeDefined() + expect(rscChunk?.isEntry).toBe(false) }) describe.each<{ mode: FixtureMode; withRscEntry: boolean }>([ @@ -168,59 +202,70 @@ describe.each<{ mode: FixtureMode; withRscEntry: boolean }>([ { mode: 'production', withRscEntry: true }, ])('$mode, rsc=$withRscEntry', (options) => { test('reads each chunk and chunk group graph property at most once', async () => { - await compileClientFixture(options, (compilation) => { - const chunkReads = countPropertyReads( - compilation.chunks.values().next().value!, - ['files', 'auxiliaryFiles', 'groupsIterable'], - ) - const groupReads = countPropertyReads( - compilation.entrypoints.get('index')!, - ['chunks', 'childrenIterable'], - ) - try { - normalizeRspackClientBuild(compilation) - } finally { - groupReads.restore() - chunkReads.restore() - } + const maxReadsByProperty = await compileClientFixture( + options, + (compilation) => { + let chunkReads: ReturnType | undefined + let groupReads: ReturnType | undefined + try { + chunkReads = countPropertyReads( + compilation.chunks.values().next().value!, + ['files', 'auxiliaryFiles', 'groupsIterable'], + ) + groupReads = countPropertyReads( + compilation.entrypoints.get('index')!, + ['chunks', 'childrenIterable'], + ) + normalizeRspackClientBuild(compilation) + } finally { + groupReads?.restore() + chunkReads?.restore() + } - const maxReadsByProperty = new Map() - for (const counter of [chunkReads, groupReads]) { - for (const reads of counter.readsByReceiver.values()) { - for (const [property, count] of reads) { - maxReadsByProperty.set( - property, - Math.max(maxReadsByProperty.get(property) ?? 0, count), - ) + const maxReadsByProperty = new Map() + for (const counter of [chunkReads, groupReads]) { + for (const reads of counter.readsByReceiver.values()) { + for (const [property, count] of reads) { + maxReadsByProperty.set( + property, + Math.max(maxReadsByProperty.get(property) ?? 0, count), + ) + } } } - } - expect(maxReadsByProperty.size).toBe(5) - for (const [property, maxCount] of maxReadsByProperty) { - expect( - maxCount, - `${property}: max reads per receiver = ${maxCount}`, - ).toBeLessThanOrEqual(1) - } - }) + return Array.from(maxReadsByProperty) + }, + ) + expect(maxReadsByProperty).toHaveLength(5) + for (const [property, maxCount] of maxReadsByProperty) { + expect( + maxCount, + `${property}: max reads per receiver = ${maxCount}`, + ).toBeLessThanOrEqual(1) + } }) }) test('matches the recorded client build with inline CSS', async () => { - const build = await compileClientFixture( - { mode: 'production' }, - (compilation) => { - const expectedOrder = expectedChunkFileOrder(compilation) - const expectedCssOrder = cssAssetOrder(compilation) - const clientBuild = normalizeRspackClientBuild(compilation, true) - expect(Array.from(clientBuild.chunksByFileName.keys())).toEqual( - expectedOrder, - ) - expect(Array.from(clientBuild.cssContentByFileName!.keys())).toEqual( - expectedCssOrder, - ) - return serializeClientBuild(clientBuild) - }, - ) - expect(build).toMatchSnapshot() + const { + cssContentByFileName, + actualOrder, + expectedOrder, + actualCssOrder, + expectedCssOrder, + } = await compileClientFixture({ mode: 'production' }, (compilation) => { + const expectedOrder = expectedChunkFileOrder(compilation) + const expectedCssOrder = cssAssetOrder(compilation) + const build = normalizeRspackClientBuild(compilation, true) + return { + cssContentByFileName: serializeClientBuild(build).cssContentByFileName, + actualOrder: Array.from(build.chunksByFileName.keys()), + expectedOrder, + actualCssOrder: Array.from(build.cssContentByFileName!.keys()), + expectedCssOrder, + } + }) + expect(actualOrder).toEqual(expectedOrder) + expect(actualCssOrder).toEqual(expectedCssOrder) + expect(cssContentByFileName).toMatchSnapshot() }) diff --git a/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts b/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts index 3edc2f7a5bf..db345a8ed67 100644 --- a/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts +++ b/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts @@ -7,7 +7,7 @@ import type { Rspack } from '@rsbuild/core' import type { NormalizedClientBuild } from '../../src/types' export const FIXTURE_ROOT = fileURLToPath( - new URL('./fixtures/rspack-client-build/', import.meta.url), + new URL('./test-files/rspack-client-build/', import.meta.url), ).replace(/[\\/]$/, '') export type FixtureMode = 'development' | 'production' @@ -19,6 +19,7 @@ export async function compileClientFixture( const outputPath = await mkdtemp(join(tmpdir(), 'rspack-client-build-')) let compiler: Rspack.Compiler | undefined let result: T + let called = false try { compiler = rspack({ @@ -89,6 +90,7 @@ export async function compileClientFixture( stage: rspack.Compilation.PROCESS_ASSETS_STAGE_REPORT, }, () => { + called = true result = inspect(compilation) }, ) @@ -113,6 +115,8 @@ export async function compileClientFixture( }) ?? 'Rspack did not return compilation stats', ), ) + } else if (!called) { + reject(new Error('Client fixture inspection did not run')) } else { resolve(result) } @@ -243,13 +247,13 @@ export function cssAssetOrder(compilation: Rspack.Compilation): Array { .map((asset) => asset.name) } -export function serializeClientBuild(build: NormalizedClientBuild): unknown { +export function serializeClientBuild(build: NormalizedClientBuild) { const fixtureRoot = FIXTURE_ROOT.replace(/\\/g, '/') return { entryChunkFileName: build.entryChunkFileName, // Rspack's chunk and asset iteration order varies between compilations in one process. chunksByFileName: Array.from(build.chunksByFileName) - .sort(([left], [right]) => left.localeCompare(right)) + .sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0)) .map(([fileName, chunk]) => [ fileName, { @@ -261,7 +265,7 @@ export function serializeClientBuild(build: NormalizedClientBuild): unknown { ]), cssContentByFileName: build.cssContentByFileName ? Array.from(build.cssContentByFileName).sort(([left], [right]) => - left.localeCompare(right), + left < right ? -1 : left > right ? 1 : 0, ) : undefined, } diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/index.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/index.js similarity index 64% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/index.js rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/index.js index bdbb9d0e7e0..da9b10cad60 100644 --- a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/index.js +++ b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/index.js @@ -1,4 +1,3 @@ -// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. import './root.css' import { runtime } from './shared/runtime-shared.js' @@ -6,32 +5,27 @@ export const app = { runtime, posts: () => import( - // @ts-expect-error Rspack resolves resource queries in this fixture input. /* webpackChunkName: "posts" */ './routes/posts.js?tsr-split=component' ), // Only the component split uses posts-helper, allowing production concatenation. postsError: () => import( - // @ts-expect-error Rspack resolves resource queries in this fixture input. /* webpackChunkName: "posts", webpackExports: ["errorComponent"] */ './routes/posts.js?tsr-split=errorComponent' ), post: () => import( - // @ts-expect-error Rspack resolves resource queries in this fixture input. /* webpackChunkName: "post" */ './routes/post.js?tsr-split=component' ), about: () => import( - // @ts-expect-error Rspack resolves resource queries in this fixture input. /* webpackChunkName: "about" */ './routes/about.js?tsr-split=component' ), settings: () => import( - // @ts-expect-error Rspack resolves resource queries in this fixture input. /* webpackChunkName: "settings" */ './routes/settings.js?tsr-split=component' ), plain: () => import(/* webpackChunkName: "plain" */ './lazy/plain.js'), notes: () => import(/* webpackChunkName: "notes" */ './lazy/tsr-split-notes.js'), } -Object.assign(globalThis, { fixtureApp: app }) +globalThis.fixtureApp = app diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/chart.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/chart.css similarity index 100% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/chart.css rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/chart.css diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/chart.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/chart.js new file mode 100644 index 00000000000..639c548b812 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/chart.js @@ -0,0 +1,3 @@ +import './chart.css' + +export const chart = 'chart' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/counter.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/counter.css similarity index 100% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/counter.css rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/counter.css diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/counter.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/counter.js new file mode 100644 index 00000000000..cd1c1633762 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/counter.js @@ -0,0 +1,3 @@ +import './counter.css' + +export const counter = 'counter' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/tooltip.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/tooltip.css similarity index 100% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/islands/tooltip.css rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/tooltip.css diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/tooltip.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/tooltip.js new file mode 100644 index 00000000000..0585f5855e7 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/tooltip.js @@ -0,0 +1,3 @@ +import './tooltip.css' + +export const tooltip = 'tooltip' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/lazy/plain.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/lazy/plain.js similarity index 100% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/lazy/plain.js rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/lazy/plain.js diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/lazy/tsr-split-notes.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/lazy/tsr-split-notes.js similarity index 100% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/lazy/tsr-split-notes.js rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/lazy/tsr-split-notes.js diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/root.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/root.css similarity index 100% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/root.css rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/root.css diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/about.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/about.js similarity index 64% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/about.js rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/about.js index 8267380a9d0..0491486af04 100644 --- a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/about.js +++ b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/about.js @@ -1,4 +1,3 @@ -// @ts-expect-error Rspack resolves resource queries in this fixture input. import themeUrl from '../theme.css?url' import { vendorB } from '../shared/vendor-b.js' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/post.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/post.js similarity index 70% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/post.js rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/post.js index 39824be86b7..62371d9ab23 100644 --- a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/post.js +++ b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/post.js @@ -4,11 +4,9 @@ import { bigShared } from '../shared/big-shared.js' export const post = () => vendorA + bigShared export const counter = () => import( - // @ts-expect-error Rspack resolves resource queries in this fixture input. /* webpackChunkName: "counter" */ '../islands/counter.js?tss-hydrate=counter' ) export const chart = () => import( - // @ts-expect-error Rspack resolves resource queries in this fixture input. /* webpackChunkName: "chart" */ '../islands/chart.js?tss-hydrate=chart' ) diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts-helper.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts-helper.js similarity index 65% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts-helper.js rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts-helper.js index 23bae658a36..2f2303f4cf6 100644 --- a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts-helper.js +++ b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts-helper.js @@ -1,2 +1 @@ -/** @param {string} value */ export const postsHelper = (value) => `posts:${value}` diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts.css similarity index 100% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts.css rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts.css diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts.js similarity index 63% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts.js rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts.js index 5192083c966..0f644c137e8 100644 --- a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/posts.js +++ b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts.js @@ -1,6 +1,4 @@ -// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. import './posts.css' -// @ts-ignore Rspack handles CSS imports; only newer TypeScript versions check them. import '../shared/shared-styles.css' import { vendorA } from '../shared/vendor-a.js' import { vendorB } from '../shared/vendor-b.js' @@ -10,6 +8,5 @@ export const errorComponent = 'posts-error' export const posts = () => postsHelper(vendorA + vendorB) export const counter = () => import( - // @ts-expect-error Rspack resolves resource queries in this fixture input. /* webpackChunkName: "counter" */ '../islands/counter.js?tss-hydrate=counter' ) diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/settings.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/settings.css similarity index 100% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/routes/settings.css rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/settings.css diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/settings.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/settings.js new file mode 100644 index 00000000000..3d354a2cda1 --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/settings.js @@ -0,0 +1,4 @@ +import './settings.css' +import '../shared/shared-styles.css' + +export const settings = 'settings' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/rsc.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/rsc.css similarity index 100% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/rsc.css rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/rsc.css diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/rsc.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/rsc.js new file mode 100644 index 00000000000..6186afa338e --- /dev/null +++ b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/rsc.js @@ -0,0 +1,3 @@ +import './rsc.css' + +globalThis.fixtureRsc = 'rsc' diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/big-shared.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/big-shared.js similarity index 100% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/big-shared.js rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/big-shared.js diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/runtime-shared.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/runtime-shared.js similarity index 100% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/runtime-shared.js rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/runtime-shared.js diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/shared-styles.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/shared-styles.css similarity index 100% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/shared-styles.css rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/shared-styles.css diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/vendor-a.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/vendor-a.js similarity index 100% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/vendor-a.js rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/vendor-a.js diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/vendor-b.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/vendor-b.js similarity index 66% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/vendor-b.js rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/vendor-b.js index fbba157c724..8638d33cd45 100644 --- a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/shared/vendor-b.js +++ b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/vendor-b.js @@ -1,6 +1,5 @@ export const vendorB = 'vendor-b' export const tooltip = () => import( - // @ts-expect-error Rspack resolves resource queries in this fixture input. /* webpackChunkName: "tooltip" */ '../islands/tooltip.js?tss-hydrate=tooltip' ) diff --git a/packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/theme.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/theme.css similarity index 100% rename from packages/start-plugin-core/tests/rsbuild/fixtures/rspack-client-build/src/theme.css rename to packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/theme.css From 78ca689b6a782fbd31672f1eba74c05d02beb03e Mon Sep 17 00:00:00 2001 From: Guru Narayanan Date: Fri, 2 Oct 2026 00:45:17 +0530 Subject: [PATCH 06/10] perf(start-plugin-core): document route and hydration chunk scan --- .changeset/rsbuild-client-build-capture.md | 2 +- .../src/rsbuild/normalized-client-build.ts | 50 +++++++++++-------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/.changeset/rsbuild-client-build-capture.md b/.changeset/rsbuild-client-build-capture.md index a587a6d91b5..dbc74cdfec4 100644 --- a/.changeset/rsbuild-client-build-capture.md +++ b/.changeset/rsbuild-client-build-capture.md @@ -2,4 +2,4 @@ '@tanstack/start-plugin-core': patch --- -Speed up the Rsbuild client build capture on apps with many chunks by reading the Rspack chunk graph once per build. +Speed up Start manifest generation for Rsbuild client builds with many chunks by caching Rspack chunk graph reads and only scanning chunks that contain route-split or hydration modules. diff --git a/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts b/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts index f74b4b6cf64..920bdb2b9c1 100644 --- a/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts +++ b/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts @@ -18,21 +18,6 @@ type RspackModule = Rspack.Module const backslashRegex = /\\/g -function findTaggedChunks( - compilation: RspackCompilation, -): Set { - const taggedChunks = new Set() - for (const mod of compilation.modules) { - const identifier = mod.identifier() - if (identifier.includes(tsrSplit) || identifier.includes(tssHydrate)) { - for (const chunk of compilation.chunkGraph.getModuleChunks(mod)) { - taggedChunks.add(chunk) - } - } - } - return taggedChunks -} - /** * Convert an OS native path to the POSIX form used by the generated route tree. */ @@ -126,6 +111,28 @@ function getHydrationIdsFromModules( return hydrationIds ?? [] } +/** + * Substring checks admit a superset of chunks; the extractors still decide. + * Relies on every getChunkModules module (including a production ConcatenatedModule + * root) being in compilation.modules and reporting its chunk via getModuleChunks. + * Rspack must return the same chunk objects as compilation.chunks, as entryChunkSet + * already requires. + */ +function findRouteAndHydrationChunks( + compilation: RspackCompilation, +): Set { + const routeAndHydrationChunks = new Set() + for (const mod of compilation.modules) { + const identifier = mod.identifier() + if (identifier.includes(tsrSplit) || identifier.includes(tssHydrate)) { + for (const chunk of compilation.chunkGraph.getModuleChunks(mod)) { + routeAndHydrationChunks.add(chunk) + } + } + } + return routeAndHydrationChunks +} + /** * Returns true for Rspack/webpack HMR runtime chunks that should never be * surfaced to the Start manifest. These files are emitted on every rebuild @@ -216,9 +223,12 @@ function createChunkGraphReader() { getAuxiliaryFiles, getGroupChunks, getChunkJsFiles, + // Child groups are import() points; their chunks' JS is Rollup's dynamicImports. computeDynamicImports(chunk: RspackCompilationChunk) { return mergeJsFiles(getGroups(chunk).map(getChildGroupJsFiles)) }, + // A group holds every chunk needed for an import, so its other chunks are + // static imports, analogous to Rollup's imports. computeAsyncChunkImports( chunk: RspackCompilationChunk, currentFile: string, @@ -258,19 +268,15 @@ export function normalizeRspackClientBuild( if (entrypoint) { for (const chunk of getGroupChunks(entrypoint)) { entryChunkSet.add(chunk) - for (const file of getFiles(chunk)) { - if (isManifestJsAsset(file)) { - initialJsFileNames.push(file) - } - } + initialJsFileNames.push(...getChunkJsFiles(chunk)) } } - const taggedChunks = findTaggedChunks(compilation) + const routeAndHydrationChunks = findRouteAndHydrationChunks(compilation) // Iterate ALL chunks (initial + async) to capture route-split chunks for (const chunk of compilation.chunks) { - const modules = taggedChunks.has(chunk) + const modules = routeAndHydrationChunks.has(chunk) ? compilation.chunkGraph.getChunkModules(chunk) : undefined const routeFilePaths = modules ? getRouteFilePathsFromModules(modules) : [] From 921e809bc6bd7165c7dffae4db46875747e3dc03 Mon Sep 17 00:00:00 2001 From: Guru Narayanan Date: Fri, 2 Oct 2026 12:52:11 +0530 Subject: [PATCH 07/10] test(start-plugin-core): replace rspack build fixture with unit tests Remove the real Rspack compile fixture, its helper and snapshots. Cover the same behavior in the existing normalized-client-build unit tests with a small fake chunk graph: output order of imports and dynamic imports, module reads only for route-split and hydration chunks, and one read per chunk graph property. --- ...ormalized-client-build-rspack.test.ts.snap | 655 ------------------ .../normalized-client-build-rspack.test.ts | 271 -------- .../rsbuild/normalized-client-build.test.ts | 197 +++++- .../rsbuild/rspack-client-build-fixture.ts | 288 -------- .../rspack-client-build/src/index.js | 31 - .../rspack-client-build/src/islands/chart.css | 3 - .../rspack-client-build/src/islands/chart.js | 3 - .../src/islands/counter.css | 3 - .../src/islands/counter.js | 3 - .../src/islands/tooltip.css | 3 - .../src/islands/tooltip.js | 3 - .../rspack-client-build/src/lazy/plain.js | 3 - .../src/lazy/tsr-split-notes.js | 1 - .../rspack-client-build/src/root.css | 3 - .../rspack-client-build/src/routes/about.js | 4 - .../rspack-client-build/src/routes/post.js | 12 - .../src/routes/posts-helper.js | 1 - .../rspack-client-build/src/routes/posts.css | 3 - .../rspack-client-build/src/routes/posts.js | 12 - .../src/routes/settings.css | 3 - .../src/routes/settings.js | 4 - .../rspack-client-build/src/rsc.css | 3 - .../test-files/rspack-client-build/src/rsc.js | 3 - .../src/shared/big-shared.js | 1 - .../src/shared/runtime-shared.js | 1 - .../src/shared/shared-styles.css | 3 - .../src/shared/vendor-a.js | 1 - .../src/shared/vendor-b.js | 5 - .../rspack-client-build/src/theme.css | 3 - 29 files changed, 174 insertions(+), 1352 deletions(-) delete mode 100644 packages/start-plugin-core/tests/rsbuild/__snapshots__/normalized-client-build-rspack.test.ts.snap delete mode 100644 packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts delete mode 100644 packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/index.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/chart.css delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/chart.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/counter.css delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/counter.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/tooltip.css delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/tooltip.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/lazy/plain.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/lazy/tsr-split-notes.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/root.css delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/about.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/post.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts-helper.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts.css delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/settings.css delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/settings.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/rsc.css delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/rsc.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/big-shared.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/runtime-shared.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/shared-styles.css delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/vendor-a.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/vendor-b.js delete mode 100644 packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/theme.css diff --git a/packages/start-plugin-core/tests/rsbuild/__snapshots__/normalized-client-build-rspack.test.ts.snap b/packages/start-plugin-core/tests/rsbuild/__snapshots__/normalized-client-build-rspack.test.ts.snap deleted file mode 100644 index e7e04bdbc32..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/__snapshots__/normalized-client-build-rspack.test.ts.snap +++ /dev/null @@ -1,655 +0,0 @@ -// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html - -exports[`development > matches the recorded normalized client build 1`] = ` -{ - "chunksByFileName": [ - [ - "about.js", - { - "css": [ - "assets/theme.css", - ], - "dynamicImports": [ - "tooltip.js", - ], - "fileName": "about.js", - "hydrationIds": [], - "imports": [ - "vendors.js", - ], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/about.js", - ], - }, - ], - [ - "big-shared.js", - { - "css": [], - "dynamicImports": [ - "counter.js", - "chart.js", - ], - "fileName": "big-shared.js", - "hydrationIds": [], - "imports": [ - "vendors.js", - "post.js", - ], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "chart.js", - { - "css": [ - "chart.css", - ], - "dynamicImports": [], - "fileName": "chart.js", - "hydrationIds": [ - "chart", - ], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "counter.js", - { - "css": [ - "counter.css", - ], - "dynamicImports": [], - "fileName": "counter.js", - "hydrationIds": [ - "counter", - ], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "index.js", - { - "css": [ - "index.css", - ], - "dynamicImports": [ - "vendors.js", - "posts.js", - "big-shared.js", - "post.js", - "about.js", - "settings.js", - "plain.js", - "notes.js", - ], - "fileName": "index.js", - "hydrationIds": [], - "imports": [], - "isEntry": true, - "routeFilePaths": [], - }, - ], - [ - "notes.js", - { - "css": [], - "dynamicImports": [], - "fileName": "notes.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "plain.js", - { - "css": [], - "dynamicImports": [], - "fileName": "plain.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "post.js", - { - "css": [], - "dynamicImports": [ - "counter.js", - "chart.js", - ], - "fileName": "post.js", - "hydrationIds": [], - "imports": [ - "vendors.js", - "big-shared.js", - ], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/post.js", - ], - }, - ], - [ - "posts.js", - { - "css": [ - "posts.css", - ], - "dynamicImports": [ - "counter.js", - "tooltip.js", - ], - "fileName": "posts.js", - "hydrationIds": [], - "imports": [ - "vendors.js", - ], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/posts.js", - ], - }, - ], - [ - "settings.js", - { - "css": [ - "settings.css", - ], - "dynamicImports": [], - "fileName": "settings.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/settings.js", - ], - }, - ], - [ - "tooltip.js", - { - "css": [ - "tooltip.css", - ], - "dynamicImports": [], - "fileName": "tooltip.js", - "hydrationIds": [ - "tooltip", - ], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "vendors.js", - { - "css": [], - "dynamicImports": [ - "counter.js", - "tooltip.js", - "chart.js", - ], - "fileName": "vendors.js", - "hydrationIds": [], - "imports": [ - "posts.js", - "big-shared.js", - "post.js", - "about.js", - ], - "isEntry": false, - "routeFilePaths": [], - }, - ], - ], - "cssContentByFileName": undefined, - "entryChunkFileName": "index.js", -} -`; - -exports[`development > matches the recorded start manifest 1`] = ` -{ - "routes": { - "/about": { - "css": [ - "/assets/assets/theme.css", - "/assets/counter.css", - "/assets/tooltip.css", - "/assets/chart.css", - ], - "filePath": "/src/routes/about.js", - "preloads": [ - "/assets/about.js", - "/assets/vendors.js", - ], - }, - "/post": { - "css": [ - "/assets/counter.css", - "/assets/tooltip.css", - "/assets/chart.css", - ], - "filePath": "/src/routes/post.js", - "preloads": [ - "/assets/post.js", - "/assets/vendors.js", - "/assets/big-shared.js", - ], - }, - "/posts": { - "css": [ - "/assets/assets/theme.css", - "/assets/posts.css", - "/assets/counter.css", - "/assets/chart.css", - "/assets/tooltip.css", - ], - "filePath": "/src/routes/posts.js", - "preloads": [ - "/assets/posts.js", - "/assets/vendors.js", - ], - }, - "/settings": { - "css": [ - "/assets/settings.css", - ], - "filePath": "/src/routes/settings.js", - "preloads": [ - "/assets/settings.js", - ], - }, - "__root__": { - "css": [ - "/assets/index.css", - ], - "preloads": [ - "/assets/index.js", - ], - "scripts": [ - { - "attrs": { - "async": true, - "src": "/assets/index.js", - "type": "module", - }, - }, - ], - }, - }, -} -`; - -exports[`matches the recorded client build with inline CSS 1`] = ` -[ - [ - "assets/theme.css", - ".theme { - background: white; -} -", - ], - [ - "chart.css", - ".chart { - color: orange; -} - -", - ], - [ - "counter.css", - ".counter { - color: blue; -} - -", - ], - [ - "index.css", - ".root { - color: black; -} - -", - ], - [ - "posts.css", - ".posts { - color: red; -} - -", - ], - [ - "settings.css", - ".settings { - color: green; -} - -", - ], - [ - "shared-styles.css", - ".shared { - font-weight: bold; -} - -", - ], - [ - "tooltip.css", - ".tooltip { - color: gray; -} - -", - ], -] -`; - -exports[`production > matches the recorded normalized client build 1`] = ` -{ - "chunksByFileName": [ - [ - "about.js", - { - "css": [ - "assets/theme.css", - ], - "dynamicImports": [ - "tooltip.js", - ], - "fileName": "about.js", - "hydrationIds": [], - "imports": [ - "vendors.js", - ], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/about.js", - ], - }, - ], - [ - "big-shared.js", - { - "css": [], - "dynamicImports": [ - "counter.js", - "chart.js", - ], - "fileName": "big-shared.js", - "hydrationIds": [], - "imports": [ - "vendors.js", - "post.js", - ], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "chart.js", - { - "css": [ - "chart.css", - ], - "dynamicImports": [], - "fileName": "chart.js", - "hydrationIds": [ - "chart", - ], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "counter.js", - { - "css": [ - "counter.css", - ], - "dynamicImports": [], - "fileName": "counter.js", - "hydrationIds": [ - "counter", - ], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "index.js", - { - "css": [ - "index.css", - ], - "dynamicImports": [ - "vendors.js", - "posts.js", - "big-shared.js", - "post.js", - "about.js", - "settings.js", - "plain.js", - "notes.js", - ], - "fileName": "index.js", - "hydrationIds": [], - "imports": [], - "isEntry": true, - "routeFilePaths": [], - }, - ], - [ - "notes.js", - { - "css": [], - "dynamicImports": [], - "fileName": "notes.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "plain.js", - { - "css": [], - "dynamicImports": [], - "fileName": "plain.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "post.js", - { - "css": [], - "dynamicImports": [ - "counter.js", - "chart.js", - ], - "fileName": "post.js", - "hydrationIds": [], - "imports": [ - "vendors.js", - "big-shared.js", - ], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/post.js", - ], - }, - ], - [ - "posts.js", - { - "css": [ - "posts.css", - ], - "dynamicImports": [ - "counter.js", - "tooltip.js", - ], - "fileName": "posts.js", - "hydrationIds": [], - "imports": [ - "vendors.js", - ], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/posts.js", - ], - }, - ], - [ - "settings.js", - { - "css": [ - "settings.css", - ], - "dynamicImports": [], - "fileName": "settings.js", - "hydrationIds": [], - "imports": [], - "isEntry": false, - "routeFilePaths": [ - "/src/routes/settings.js", - ], - }, - ], - [ - "tooltip.js", - { - "css": [ - "tooltip.css", - ], - "dynamicImports": [], - "fileName": "tooltip.js", - "hydrationIds": [ - "tooltip", - ], - "imports": [], - "isEntry": false, - "routeFilePaths": [], - }, - ], - [ - "vendors.js", - { - "css": [], - "dynamicImports": [ - "counter.js", - "tooltip.js", - "chart.js", - ], - "fileName": "vendors.js", - "hydrationIds": [], - "imports": [ - "posts.js", - "big-shared.js", - "post.js", - "about.js", - ], - "isEntry": false, - "routeFilePaths": [], - }, - ], - ], - "cssContentByFileName": undefined, - "entryChunkFileName": "index.js", -} -`; - -exports[`production > matches the recorded start manifest 1`] = ` -{ - "routes": { - "/about": { - "css": [ - "/assets/assets/theme.css", - "/assets/counter.css", - "/assets/tooltip.css", - "/assets/chart.css", - ], - "filePath": "/src/routes/about.js", - "preloads": [ - "/assets/about.js", - "/assets/vendors.js", - ], - }, - "/post": { - "css": [ - "/assets/counter.css", - "/assets/tooltip.css", - "/assets/chart.css", - ], - "filePath": "/src/routes/post.js", - "preloads": [ - "/assets/post.js", - "/assets/vendors.js", - "/assets/big-shared.js", - ], - }, - "/posts": { - "css": [ - "/assets/assets/theme.css", - "/assets/posts.css", - "/assets/counter.css", - "/assets/chart.css", - "/assets/tooltip.css", - ], - "filePath": "/src/routes/posts.js", - "preloads": [ - "/assets/posts.js", - "/assets/vendors.js", - ], - }, - "/settings": { - "css": [ - "/assets/settings.css", - ], - "filePath": "/src/routes/settings.js", - "preloads": [ - "/assets/settings.js", - ], - }, - "__root__": { - "css": [ - "/assets/index.css", - ], - "preloads": [ - "/assets/index.js", - ], - "scripts": [ - { - "attrs": { - "async": true, - "src": "/assets/index.js", - "type": "module", - }, - }, - ], - }, - }, -} -`; diff --git a/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts b/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts deleted file mode 100644 index 6a5ce07a0a8..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/normalized-client-build-rspack.test.ts +++ /dev/null @@ -1,271 +0,0 @@ -import { describe, expect, test } from 'vitest' -import { normalizeRspackClientBuild } from '../../src/rsbuild/normalized-client-build' -import { buildStartManifest } from '../../src/start-manifest-plugin/manifestBuilder' -import { - FIXTURE_ROOT, - compileClientFixture, - countPropertyReads, - cssAssetOrder, - expectedChunkFileOrder, - fixtureRouteTreeRoutes, - recordChunkModuleCalls, - serializeClientBuild, -} from './rspack-client-build-fixture' -import type { Rspack } from '@rsbuild/core' -import type { FixtureMode } from './rspack-client-build-fixture' - -function hasJsFile(chunk: Rspack.Chunk): boolean { - return Array.from(chunk.files).some( - (file) => file.endsWith('.js') || file.endsWith('.mjs'), - ) -} - -describe.each(['development', 'production'])('%s', (mode) => { - test('matches the recorded normalized client build', async () => { - const { build, actualOrder, expectedOrder } = await compileClientFixture( - { mode }, - (compilation) => { - const expectedOrder = expectedChunkFileOrder(compilation) - const clientBuild = normalizeRspackClientBuild(compilation) - return { - build: serializeClientBuild(clientBuild), - actualOrder: Array.from(clientBuild.chunksByFileName.keys()), - expectedOrder, - } - }, - ) - expect(actualOrder).toEqual(expectedOrder) - expect(build).toMatchSnapshot() - }) - - test('matches the recorded start manifest', async () => { - const manifest = await compileClientFixture({ mode }, (compilation) => - buildStartManifest({ - clientBuild: normalizeRspackClientBuild(compilation), - routeTreeRoutes: fixtureRouteTreeRoutes(), - basePath: '/assets', - }), - ) - const serialized = JSON.stringify(manifest).replaceAll( - FIXTURE_ROOT.replace(/\\/g, '/'), - '', - ) - expect(JSON.parse(serialized)).toMatchSnapshot() - }) - - test('lists chunk modules only for chunks that hold a route split or hydration module', async () => { - const facts = await compileClientFixture({ mode }, (compilation) => { - const taggedChunks = new Set( - Array.from(compilation.chunks).filter((chunk) => - compilation.chunkGraph.getChunkModules(chunk).some((module) => { - const identifier = module.identifier() - return ( - identifier.includes('tsr-split') || - identifier.includes('tss-hydrate') - ) - }), - ), - ) - let calls: ReturnType | undefined - try { - calls = recordChunkModuleCalls(compilation.chunkGraph) - normalizeRspackClientBuild(compilation) - } finally { - calls?.restore() - } - - return { - taggedChunkCount: taggedChunks.size, - totalChunkCount: compilation.chunks.size, - listedChunkCount: new Set(calls.chunks).size, - recordedCalls: calls.chunks.map((chunk) => ({ - name: chunk.name, - isTagged: taggedChunks.has(chunk), - })), - } - }) - expect(facts.taggedChunkCount).toBeGreaterThan(0) - expect(facts.taggedChunkCount).toBeLessThan(facts.totalChunkCount) - expect(facts.listedChunkCount).toBe(facts.taggedChunkCount) - expect(facts.recordedCalls).toHaveLength(facts.taggedChunkCount) - for (const call of facts.recordedCalls) { - expect(call.isTagged, `getChunkModules(${call.name})`).toBe(true) - } - }) - - test('fixture exercises the shapes the normalizer must preserve', async () => { - const facts = await compileClientFixture({ mode }, (compilation) => { - const chunks = Array.from(compilation.chunks) - const groups = Array.from(compilation.chunkGroups) - const tooltipChunks = new Set( - chunks.filter((chunk) => - compilation.chunkGraph - .getChunkModules(chunk) - .some((module) => - module - .identifier() - .replace(/\\/g, '/') - .includes('/islands/tooltip.js?tss-hydrate=tooltip'), - ), - ), - ) - - return { - hasSiblingJsChunks: groups.some( - (group) => group.chunks.filter(hasJsFile).length >= 2, - ), - hasSharedChunk: chunks.some( - (chunk) => Array.from(chunk.groupsIterable).length >= 2, - ), - hasCssOnlyChunk: chunks.some((chunk) => !hasJsFile(chunk)), - hasAuxiliaryCss: chunks.some((chunk) => - Array.from(chunk.auxiliaryFiles).some((file) => - file.endsWith('.css'), - ), - ), - hasTwoPostsSplits: chunks.some( - (chunk) => - compilation.chunkGraph.getChunkModules(chunk).filter((module) => { - const identifier = module.identifier().replace(/\\/g, '/') - return identifier.includes('/routes/posts.js?tsr-split=') - }).length === 2, - ), - hasTooltipThroughVendors: groups.some( - (group) => - group.chunks.some((chunk) => chunk.name === 'vendors') && - Array.from(group.childrenIterable).some((child) => - child.chunks.some((chunk) => tooltipChunks.has(chunk)), - ), - ), - hasPostsConcatenationRoot: - mode === 'production' && - Array.from(compilation.modules).some( - (module) => - module.constructor.name === 'ConcatenatedModule' && - module.identifier().includes('posts.js?tsr-split='), - ), - } - }) - expect(facts.hasSiblingJsChunks, 'a group has sibling JS chunks').toBe(true) - expect( - facts.hasSharedChunk, - 'a shared chunk belongs to multiple groups', - ).toBe(true) - expect(facts.hasCssOnlyChunk, 'a CSS-only chunk is present').toBe(true) - expect(facts.hasAuxiliaryCss, 'CSS assets appear in auxiliaryFiles').toBe( - true, - ) - expect( - facts.hasTwoPostsSplits, - 'two posts split modules share a chunk', - ).toBe(true) - expect( - facts.hasTooltipThroughVendors, - 'tooltip is reachable through a vendors group', - ).toBe(true) - if (mode === 'production') { - expect( - facts.hasPostsConcatenationRoot, - 'a posts split is a concatenation root', - ).toBe(true) - } - }) -}) - -test('merges rsc CSS into the main entry without making rsc the entry', async () => { - const { indexChunk, rscChunk, actualOrder, expectedOrder } = - await compileClientFixture( - { mode: 'production', withRscEntry: true }, - (compilation) => { - const expectedOrder = expectedChunkFileOrder(compilation) - const clientBuild = normalizeRspackClientBuild(compilation) - return { - indexChunk: clientBuild.chunksByFileName.get('index.js'), - rscChunk: clientBuild.chunksByFileName.get('rsc.js'), - actualOrder: Array.from(clientBuild.chunksByFileName.keys()), - expectedOrder, - } - }, - ) - expect(actualOrder).toEqual(expectedOrder) - expect(indexChunk?.css.at(-1)).toBe('rsc.css') - expect( - indexChunk?.css.filter((fileName) => fileName === 'rsc.css'), - ).toHaveLength(1) - expect(rscChunk).toBeDefined() - expect(rscChunk?.isEntry).toBe(false) -}) - -describe.each<{ mode: FixtureMode; withRscEntry: boolean }>([ - { mode: 'development', withRscEntry: false }, - { mode: 'production', withRscEntry: false }, - { mode: 'production', withRscEntry: true }, -])('$mode, rsc=$withRscEntry', (options) => { - test('reads each chunk and chunk group graph property at most once', async () => { - const maxReadsByProperty = await compileClientFixture( - options, - (compilation) => { - let chunkReads: ReturnType | undefined - let groupReads: ReturnType | undefined - try { - chunkReads = countPropertyReads( - compilation.chunks.values().next().value!, - ['files', 'auxiliaryFiles', 'groupsIterable'], - ) - groupReads = countPropertyReads( - compilation.entrypoints.get('index')!, - ['chunks', 'childrenIterable'], - ) - normalizeRspackClientBuild(compilation) - } finally { - groupReads?.restore() - chunkReads?.restore() - } - - const maxReadsByProperty = new Map() - for (const counter of [chunkReads, groupReads]) { - for (const reads of counter.readsByReceiver.values()) { - for (const [property, count] of reads) { - maxReadsByProperty.set( - property, - Math.max(maxReadsByProperty.get(property) ?? 0, count), - ) - } - } - } - return Array.from(maxReadsByProperty) - }, - ) - expect(maxReadsByProperty).toHaveLength(5) - for (const [property, maxCount] of maxReadsByProperty) { - expect( - maxCount, - `${property}: max reads per receiver = ${maxCount}`, - ).toBeLessThanOrEqual(1) - } - }) -}) - -test('matches the recorded client build with inline CSS', async () => { - const { - cssContentByFileName, - actualOrder, - expectedOrder, - actualCssOrder, - expectedCssOrder, - } = await compileClientFixture({ mode: 'production' }, (compilation) => { - const expectedOrder = expectedChunkFileOrder(compilation) - const expectedCssOrder = cssAssetOrder(compilation) - const build = normalizeRspackClientBuild(compilation, true) - return { - cssContentByFileName: serializeClientBuild(build).cssContentByFileName, - actualOrder: Array.from(build.chunksByFileName.keys()), - expectedOrder, - actualCssOrder: Array.from(build.cssContentByFileName!.keys()), - expectedCssOrder, - } - }) - expect(actualOrder).toEqual(expectedOrder) - expect(actualCssOrder).toEqual(expectedCssOrder) - expect(cssContentByFileName).toMatchSnapshot() -}) diff --git a/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts b/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts index 72f11b6002d..56857a72acd 100644 --- a/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts +++ b/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts @@ -5,7 +5,7 @@ import { } from '../../src/rsbuild/normalized-client-build' import { buildStartManifest } from '../../src/start-manifest-plugin/manifestBuilder' import type { RsbuildPluginAPI, Rspack } from '@rsbuild/core' -import type { GetConfigFn } from '../../src/types' +import type { GetConfigFn, NormalizedClientChunk } from '../../src/types' function makeCompilation(readCss: () => string | Uint8Array) { const entryChunk = { @@ -82,34 +82,185 @@ function makeWindowsCompilation(readCss: () => string | Uint8Array) { return compilation } -describe('normalizeRspackClientBuild', () => { - test('keeps hot-update files out of entries and imports', () => { - const entryChunk = { - name: 'index', - files: new Set(['index.js', 'index.abc123.hot-update.js', 'root.css']), - auxiliaryFiles: new Set(), - groupsIterable: new Set(), +type MockChunk = { + name: string + files: Set + auxiliaryFiles: Set + groupsIterable: Set +} +type MockChunkGroup = { + chunks: Array + childrenIterable: Set +} + +// The entry group lazy-loads the posts and about routes, which share a chunk. +// The posts route lazy-loads the post route, which contains a hydration island. +function makeChunkGraph() { + const chunk = ( + name: string, + files: Array, + auxiliaryFiles: Array = [], + ): MockChunk => ({ + name, + files: new Set(files), + auxiliaryFiles: new Set(auxiliaryFiles), + groupsIterable: new Set(), + }) + const group = ( + chunks: Array, + children: Array = [], + ): MockChunkGroup => { + const chunkGroup = { chunks, childrenIterable: new Set(children) } + for (const member of chunks) { + member.groupsIterable.add(chunkGroup) } - const compilation = { - entrypoints: new Map([['index', { chunks: [entryChunk] }]]), - chunks: new Set([entryChunk]), - modules: new Set(), - chunkGraph: { - getChunkModules: () => [], - getModuleChunks: () => [], + return chunkGroup + } + + const index = chunk('index', [ + 'index.js', + 'index.abc123.hot-update.js', + 'root.css', + ]) + const vendor = chunk('vendor', ['vendor.js']) + const shared = chunk('shared', ['shared.js', 'shared.abc123.hot-update.js']) + const posts = chunk('posts', ['posts.js'], ['posts.css']) + const about = chunk('about', ['about.js']) + const post = chunk('post', ['post.js']) + const postGroup = group([post]) + const postsGroup = group([shared, posts], [postGroup]) + const aboutGroup = group([shared, about]) + const entryGroup = group([vendor, index], [postsGroup, aboutGroup]) + + const moduleChunks = new Map>([ + [ + { + identifier: () => '/routes/posts.tsx?tsr-split=component', + nameForCondition: () => '/routes/posts.tsx', }, - } as unknown as Rspack.Compilation + [posts], + ], + [{ identifier: () => '/islands/counter.tsx?tss-hydrate=counter' }, [post]], + [{ identifier: () => '/shared.ts' }, [shared]], + ]) + const getChunkModules = vi.fn((target: MockChunk) => + Array.from(moduleChunks.keys()).filter((mod) => + moduleChunks.get(mod)!.includes(target), + ), + ) + const chunks = [index, vendor, shared, posts, about, post] + const compilation = { + entrypoints: new Map([['index', entryGroup]]), + chunks: new Set(chunks), + modules: new Set(moduleChunks.keys()), + chunkGraph: { + getChunkModules, + getModuleChunks: (mod: object) => moduleChunks.get(mod) ?? [], + }, + } as unknown as Rspack.Compilation + return { + compilation, + getChunkModules, + graphObjects: [...chunks, postGroup, postsGroup, aboutGroup, entryGroup], + } +} + +// Counts reads of the chunk and chunk group properties that build a new +// collection on every read in Rspack. Returns the highest count for any one +// property of any one object. +function trackGraphPropertyReads(graphObjects: Array) { + let maxReads = 0 + for (const graphObject of graphObjects) { + for (const key of [ + 'files', + 'auxiliaryFiles', + 'groupsIterable', + 'chunks', + 'childrenIterable', + ]) { + if (!(key in graphObject)) { + continue + } + const value: unknown = Reflect.get(graphObject, key) + let reads = 0 + Object.defineProperty(graphObject, key, { + get() { + reads += 1 + maxReads = Math.max(maxReads, reads) + return value + }, + }) + } + } + return () => maxReads +} + +function expectedChunk( + fileName: string, + fields: Partial = {}, +): NormalizedClientChunk { + return { + fileName, + isEntry: false, + imports: [], + dynamicImports: [], + css: [], + routeFilePaths: [], + hydrationIds: [], + ...fields, + } +} + +describe('normalizeRspackClientBuild', () => { + test('lists imports and dynamic imports from chunk groups in first-seen order', () => { + const { compilation } = makeChunkGraph() const clientBuild = normalizeRspackClientBuild(compilation) + const lazyRoutes = ['shared.js', 'posts.js', 'about.js'] expect(clientBuild.entryChunkFileName).toBe('index.js') - expect( - Array.from(clientBuild.chunksByFileName.keys()).some((file) => - file.includes('.hot-update.'), - ), - ).toBe(false) - expect(clientBuild.chunksByFileName.get('index.js')?.isEntry).toBe(true) - expect(clientBuild.chunksByFileName.get('index.js')?.imports).toEqual([]) + expect(Array.from(clientBuild.chunksByFileName.values())).toEqual([ + expectedChunk('index.js', { + isEntry: true, + imports: ['vendor.js'], + dynamicImports: lazyRoutes, + css: ['root.css'], + }), + expectedChunk('vendor.js', { + imports: ['index.js'], + dynamicImports: lazyRoutes, + }), + expectedChunk('shared.js', { + imports: ['posts.js', 'about.js'], + dynamicImports: ['post.js'], + }), + expectedChunk('posts.js', { + imports: ['shared.js'], + dynamicImports: ['post.js'], + css: ['posts.css'], + routeFilePaths: ['/routes/posts.tsx'], + }), + expectedChunk('about.js', { imports: ['shared.js'] }), + expectedChunk('post.js', { hydrationIds: ['counter'] }), + ]) + }) + + test('reads modules only from chunks with a route-split or hydration module', () => { + const { compilation, getChunkModules } = makeChunkGraph() + normalizeRspackClientBuild(compilation) + + expect(getChunkModules.mock.calls.map(([target]) => target.name)).toEqual([ + 'posts', + 'post', + ]) + }) + + test('reads each chunk and chunk group property at most once', () => { + const { compilation, graphObjects } = makeChunkGraph() + const maxReads = trackGraphPropertyReads(graphObjects) + normalizeRspackClientBuild(compilation) + + expect(maxReads()).toBe(1) }) test('keeps route stylesheet links with inline CSS disabled by default', () => { diff --git a/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts b/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts deleted file mode 100644 index db345a8ed67..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/rspack-client-build-fixture.ts +++ /dev/null @@ -1,288 +0,0 @@ -import { mkdtemp, rm } from 'node:fs/promises' -import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { fileURLToPath } from 'node:url' -import { rspack } from '@rsbuild/core' -import type { Rspack } from '@rsbuild/core' -import type { NormalizedClientBuild } from '../../src/types' - -export const FIXTURE_ROOT = fileURLToPath( - new URL('./test-files/rspack-client-build/', import.meta.url), -).replace(/[\\/]$/, '') - -export type FixtureMode = 'development' | 'production' - -export async function compileClientFixture( - options: { mode: FixtureMode; withRscEntry?: boolean }, - inspect: (compilation: Rspack.Compilation) => T, -): Promise { - const outputPath = await mkdtemp(join(tmpdir(), 'rspack-client-build-')) - let compiler: Rspack.Compiler | undefined - let result: T - let called = false - - try { - compiler = rspack({ - mode: options.mode, - context: FIXTURE_ROOT, - devtool: false, - entry: { - index: './src/index.js', - ...(options.withRscEntry ? { rsc: './src/rsc.js' } : {}), - }, - output: { - path: outputPath, - filename: '[name].js', - chunkFilename: '[name].js', - cssFilename: '[name].css', - cssChunkFilename: '[name].css', - assetModuleFilename: 'assets/[name][ext]', - }, - module: { - rules: [ - { - test: /\.css$/, - // The enclosing package is sideEffects: false, unlike this fixture app. - sideEffects: true, - oneOf: [ - { resourceQuery: /url/, type: 'asset/resource' }, - { type: 'css/auto' }, - ], - }, - ], - }, - optimization: { - minimize: false, - chunkIds: 'named', - moduleIds: 'named', - splitChunks: { - chunks: 'all', - minSize: 0, - cacheGroups: { - vendors: { - test: /[\\/]shared[\\/]vendor-/, - name: 'vendors', - enforce: true, - }, - bigShared: { - test: /big-shared/, - name: 'big-shared', - enforce: true, - }, - sharedStyles: { - test: /shared-styles\.css$/, - type: 'css/auto', - name: 'shared-styles', - enforce: true, - }, - }, - }, - }, - plugins: [ - { - apply(fixtureCompiler: Rspack.Compiler) { - fixtureCompiler.hooks.thisCompilation.tap( - 'InspectClientFixture', - (compilation: Rspack.Compilation) => { - compilation.hooks.processAssets.tap( - { - name: 'InspectClientFixture', - stage: rspack.Compilation.PROCESS_ASSETS_STAGE_REPORT, - }, - () => { - called = true - result = inspect(compilation) - }, - ) - }, - ) - }, - }, - ], - }) - - return await new Promise((resolve, reject) => { - compiler!.run((error, stats) => { - if (error) { - reject(error) - } else if (!stats || stats.hasErrors()) { - reject( - new Error( - stats?.toString({ - all: false, - errors: true, - errorDetails: true, - }) ?? 'Rspack did not return compilation stats', - ), - ) - } else if (!called) { - reject(new Error('Client fixture inspection did not run')) - } else { - resolve(result) - } - }) - }) - } finally { - try { - if (compiler) { - await new Promise((resolve, reject) => { - compiler!.close((error) => { - if (error) { - reject(error) - } else { - resolve() - } - }) - }) - } - } finally { - await rm(outputPath, { recursive: true, force: true }) - } - } -} - -export function countPropertyReads( - target: object, - names: ReadonlyArray, -): { - readsByReceiver: Map> - restore: () => void -} { - const readsByReceiver = new Map>() - const originals = names.map((name) => { - let owner: object | null = Object.getPrototypeOf(target) - while (owner) { - const descriptor = Object.getOwnPropertyDescriptor(owner, name) - if (descriptor) { - if (!descriptor.get || !descriptor.configurable) { - throw new Error(`Expected a configurable getter for ${name}`) - } - return { owner, name, descriptor } - } - owner = Object.getPrototypeOf(owner) - } - throw new Error(`No prototype getter found for ${name}`) - }) - - for (const { owner, name, descriptor } of originals) { - Object.defineProperty(owner, name, { - ...descriptor, - get(this: object) { - let reads = readsByReceiver.get(this) - if (!reads) { - reads = new Map() - readsByReceiver.set(this, reads) - } - reads.set(name, (reads.get(name) ?? 0) + 1) - return descriptor.get!.call(this) - }, - }) - } - - return { - readsByReceiver, - restore() { - for (const { owner, name, descriptor } of originals) { - Object.defineProperty(owner, name, descriptor) - } - }, - } -} - -export function recordChunkModuleCalls( - chunkGraph: Rspack.Compilation['chunkGraph'], -): { - chunks: Array - restore: () => void -} { - const chunks: Array = [] - let owner: object | null = Object.getPrototypeOf(chunkGraph) - while (owner) { - const descriptor = Object.getOwnPropertyDescriptor(owner, 'getChunkModules') - if (descriptor) { - if (typeof descriptor.value !== 'function' || !descriptor.configurable) { - throw new Error('Expected a configurable getChunkModules method') - } - const methodOwner = owner - Object.defineProperty(methodOwner, 'getChunkModules', { - ...descriptor, - value(this: Rspack.Compilation['chunkGraph'], chunk: Rspack.Chunk) { - chunks.push(chunk) - return descriptor.value.call(this, chunk) - }, - }) - return { - chunks, - restore() { - Object.defineProperty(methodOwner, 'getChunkModules', descriptor) - }, - } - } - owner = Object.getPrototypeOf(owner) - } - throw new Error('No prototype method found for getChunkModules') -} - -export function expectedChunkFileOrder( - compilation: Rspack.Compilation, -): Array { - const files: Array = [] - for (const chunk of compilation.chunks) { - for (const file of chunk.files) { - if ( - (file.endsWith('.js') || file.endsWith('.mjs')) && - !file.includes('.hot-update.') - ) { - files.push(file) - } - } - } - return files -} - -export function cssAssetOrder(compilation: Rspack.Compilation): Array { - return compilation - .getAssets() - .filter((asset) => asset.name.endsWith('.css')) - .map((asset) => asset.name) -} - -export function serializeClientBuild(build: NormalizedClientBuild) { - const fixtureRoot = FIXTURE_ROOT.replace(/\\/g, '/') - return { - entryChunkFileName: build.entryChunkFileName, - // Rspack's chunk and asset iteration order varies between compilations in one process. - chunksByFileName: Array.from(build.chunksByFileName) - .sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0)) - .map(([fileName, chunk]) => [ - fileName, - { - ...chunk, - routeFilePaths: chunk.routeFilePaths.map((filePath) => - filePath.replaceAll(fixtureRoot, ''), - ), - }, - ]), - cssContentByFileName: build.cssContentByFileName - ? Array.from(build.cssContentByFileName).sort(([left], [right]) => - left < right ? -1 : left > right ? 1 : 0, - ) - : undefined, - } -} - -export function fixtureRouteTreeRoutes(): Record< - string, - { filePath?: string } -> { - const routes: Record = { __root__: {} } - for (const name of ['posts', 'post', 'about', 'settings']) { - routes[`/${name}`] = { - filePath: join(FIXTURE_ROOT, 'src/routes', `${name}.js`).replace( - /\\/g, - '/', - ), - } - } - return routes -} diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/index.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/index.js deleted file mode 100644 index da9b10cad60..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/index.js +++ /dev/null @@ -1,31 +0,0 @@ -import './root.css' -import { runtime } from './shared/runtime-shared.js' - -export const app = { - runtime, - posts: () => - import( - /* webpackChunkName: "posts" */ './routes/posts.js?tsr-split=component' - ), - // Only the component split uses posts-helper, allowing production concatenation. - postsError: () => - import( - /* webpackChunkName: "posts", webpackExports: ["errorComponent"] */ './routes/posts.js?tsr-split=errorComponent' - ), - post: () => - import( - /* webpackChunkName: "post" */ './routes/post.js?tsr-split=component' - ), - about: () => - import( - /* webpackChunkName: "about" */ './routes/about.js?tsr-split=component' - ), - settings: () => - import( - /* webpackChunkName: "settings" */ './routes/settings.js?tsr-split=component' - ), - plain: () => import(/* webpackChunkName: "plain" */ './lazy/plain.js'), - notes: () => - import(/* webpackChunkName: "notes" */ './lazy/tsr-split-notes.js'), -} -globalThis.fixtureApp = app diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/chart.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/chart.css deleted file mode 100644 index ae060598221..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/chart.css +++ /dev/null @@ -1,3 +0,0 @@ -.chart { - color: orange; -} diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/chart.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/chart.js deleted file mode 100644 index 639c548b812..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/chart.js +++ /dev/null @@ -1,3 +0,0 @@ -import './chart.css' - -export const chart = 'chart' diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/counter.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/counter.css deleted file mode 100644 index 54e2be6982b..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/counter.css +++ /dev/null @@ -1,3 +0,0 @@ -.counter { - color: blue; -} diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/counter.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/counter.js deleted file mode 100644 index cd1c1633762..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/counter.js +++ /dev/null @@ -1,3 +0,0 @@ -import './counter.css' - -export const counter = 'counter' diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/tooltip.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/tooltip.css deleted file mode 100644 index 3f6134b01ba..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/tooltip.css +++ /dev/null @@ -1,3 +0,0 @@ -.tooltip { - color: gray; -} diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/tooltip.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/tooltip.js deleted file mode 100644 index 0585f5855e7..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/islands/tooltip.js +++ /dev/null @@ -1,3 +0,0 @@ -import './tooltip.css' - -export const tooltip = 'tooltip' diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/lazy/plain.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/lazy/plain.js deleted file mode 100644 index a718f6a1e79..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/lazy/plain.js +++ /dev/null @@ -1,3 +0,0 @@ -import { runtime } from '../shared/runtime-shared.js' - -export const plain = `plain:${runtime}` diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/lazy/tsr-split-notes.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/lazy/tsr-split-notes.js deleted file mode 100644 index 39c93cc7ff9..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/lazy/tsr-split-notes.js +++ /dev/null @@ -1 +0,0 @@ -export const notes = 'not a route split' diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/root.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/root.css deleted file mode 100644 index 25f0ffad60b..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/root.css +++ /dev/null @@ -1,3 +0,0 @@ -.root { - color: black; -} diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/about.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/about.js deleted file mode 100644 index 0491486af04..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/about.js +++ /dev/null @@ -1,4 +0,0 @@ -import themeUrl from '../theme.css?url' -import { vendorB } from '../shared/vendor-b.js' - -export const about = () => ({ themeUrl, vendorB }) diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/post.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/post.js deleted file mode 100644 index 62371d9ab23..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/post.js +++ /dev/null @@ -1,12 +0,0 @@ -import { vendorA } from '../shared/vendor-a.js' -import { bigShared } from '../shared/big-shared.js' - -export const post = () => vendorA + bigShared -export const counter = () => - import( - /* webpackChunkName: "counter" */ '../islands/counter.js?tss-hydrate=counter' - ) -export const chart = () => - import( - /* webpackChunkName: "chart" */ '../islands/chart.js?tss-hydrate=chart' - ) diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts-helper.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts-helper.js deleted file mode 100644 index 2f2303f4cf6..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts-helper.js +++ /dev/null @@ -1 +0,0 @@ -export const postsHelper = (value) => `posts:${value}` diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts.css deleted file mode 100644 index e9d9ef1e7fc..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts.css +++ /dev/null @@ -1,3 +0,0 @@ -.posts { - color: red; -} diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts.js deleted file mode 100644 index 0f644c137e8..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/posts.js +++ /dev/null @@ -1,12 +0,0 @@ -import './posts.css' -import '../shared/shared-styles.css' -import { vendorA } from '../shared/vendor-a.js' -import { vendorB } from '../shared/vendor-b.js' -import { postsHelper } from './posts-helper.js' - -export const errorComponent = 'posts-error' -export const posts = () => postsHelper(vendorA + vendorB) -export const counter = () => - import( - /* webpackChunkName: "counter" */ '../islands/counter.js?tss-hydrate=counter' - ) diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/settings.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/settings.css deleted file mode 100644 index 9cad3b2d1b8..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/settings.css +++ /dev/null @@ -1,3 +0,0 @@ -.settings { - color: green; -} diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/settings.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/settings.js deleted file mode 100644 index 3d354a2cda1..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/routes/settings.js +++ /dev/null @@ -1,4 +0,0 @@ -import './settings.css' -import '../shared/shared-styles.css' - -export const settings = 'settings' diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/rsc.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/rsc.css deleted file mode 100644 index 8c438ed6dc5..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/rsc.css +++ /dev/null @@ -1,3 +0,0 @@ -.rsc { - color: navy; -} diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/rsc.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/rsc.js deleted file mode 100644 index 6186afa338e..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/rsc.js +++ /dev/null @@ -1,3 +0,0 @@ -import './rsc.css' - -globalThis.fixtureRsc = 'rsc' diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/big-shared.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/big-shared.js deleted file mode 100644 index 292a7efa27e..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/big-shared.js +++ /dev/null @@ -1 +0,0 @@ -export const bigShared = 'big-shared' diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/runtime-shared.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/runtime-shared.js deleted file mode 100644 index 749f652eef1..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/runtime-shared.js +++ /dev/null @@ -1 +0,0 @@ -export const runtime = 'runtime' diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/shared-styles.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/shared-styles.css deleted file mode 100644 index a9f7e595db6..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/shared-styles.css +++ /dev/null @@ -1,3 +0,0 @@ -.shared { - font-weight: bold; -} diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/vendor-a.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/vendor-a.js deleted file mode 100644 index 043a9c38c01..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/vendor-a.js +++ /dev/null @@ -1 +0,0 @@ -export const vendorA = 'vendor-a' diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/vendor-b.js b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/vendor-b.js deleted file mode 100644 index 8638d33cd45..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/shared/vendor-b.js +++ /dev/null @@ -1,5 +0,0 @@ -export const vendorB = 'vendor-b' -export const tooltip = () => - import( - /* webpackChunkName: "tooltip" */ '../islands/tooltip.js?tss-hydrate=tooltip' - ) diff --git a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/theme.css b/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/theme.css deleted file mode 100644 index 09d808ff40f..00000000000 --- a/packages/start-plugin-core/tests/rsbuild/test-files/rspack-client-build/src/theme.css +++ /dev/null @@ -1,3 +0,0 @@ -.theme { - background: white; -} From c4c304023e683e3bccc203d8bd3569fc6a57171d Mon Sep 17 00:00:00 2001 From: Guru Narayanan Date: Fri, 2 Oct 2026 12:52:11 +0530 Subject: [PATCH 08/10] docs(start-plugin-core): simplify rsbuild client build comments --- .changeset/rsbuild-client-build-capture.md | 2 +- .../src/rsbuild/normalized-client-build.ts | 21 ++++++++----------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/.changeset/rsbuild-client-build-capture.md b/.changeset/rsbuild-client-build-capture.md index dbc74cdfec4..70fbc4c839b 100644 --- a/.changeset/rsbuild-client-build-capture.md +++ b/.changeset/rsbuild-client-build-capture.md @@ -2,4 +2,4 @@ '@tanstack/start-plugin-core': patch --- -Speed up Start manifest generation for Rsbuild client builds with many chunks by caching Rspack chunk graph reads and only scanning chunks that contain route-split or hydration modules. +Make Start manifest generation faster for Rsbuild client builds with many chunks. diff --git a/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts b/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts index 920bdb2b9c1..d6ab6c38b6e 100644 --- a/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts +++ b/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts @@ -112,11 +112,7 @@ function getHydrationIdsFromModules( } /** - * Substring checks admit a superset of chunks; the extractors still decide. - * Relies on every getChunkModules module (including a production ConcatenatedModule - * root) being in compilation.modules and reporting its chunk via getModuleChunks. - * Rspack must return the same chunk objects as compilation.chunks, as entryChunkSet - * already requires. + * Returns the chunks that contain a route-split or hydration module. */ function findRouteAndHydrationChunks( compilation: RspackCompilation, @@ -186,8 +182,8 @@ function mergeJsFiles( } /** - * Rspack's graph getters allocate collections on each read. Keep this cache local - * to one capture so rebuilds see fresh graph data. + * Reads each chunk and chunk group property once per normalizeRspackClientBuild + * call. Rspack builds a new collection every time one of these properties is read. */ function createChunkGraphReader() { const getFiles = memoizeGraphRead((chunk: RspackCompilationChunk) => @@ -209,11 +205,11 @@ function createChunkGraphReader() { getFiles(chunk).filter(isManifestJsAsset), ) - // Each group supplies its sibling imports and its direct dynamic-import edges. - // Deduping within a group before merging preserves global first-occurrence order. + // JS files of all chunks in a group, without duplicates. const getGroupJsFiles = memoizeGraphRead((group: Rspack.ChunkGroup) => mergeJsFiles(getGroupChunks(group).map(getChunkJsFiles)), ) + // JS files of all chunks in a group's child groups, without duplicates. const getChildGroupJsFiles = memoizeGraphRead((group: Rspack.ChunkGroup) => mergeJsFiles(getChildren(group).map(getGroupJsFiles)), ) @@ -223,12 +219,13 @@ function createChunkGraphReader() { getAuxiliaryFiles, getGroupChunks, getChunkJsFiles, - // Child groups are import() points; their chunks' JS is Rollup's dynamicImports. + // Child groups are dynamic import() points, so their JS files are the + // chunk's dynamic imports. computeDynamicImports(chunk: RspackCompilationChunk) { return mergeJsFiles(getGroups(chunk).map(getChildGroupJsFiles)) }, - // A group holds every chunk needed for an import, so its other chunks are - // static imports, analogous to Rollup's imports. + // A group holds every chunk an import needs, so the other chunks in the + // chunk's groups are its static imports. computeAsyncChunkImports( chunk: RspackCompilationChunk, currentFile: string, From 38fb677a0a9031a1263958e4b18fec0362d31036 Mon Sep 17 00:00:00 2001 From: Guru Narayanan Date: Fri, 2 Oct 2026 13:01:09 +0530 Subject: [PATCH 09/10] docs(start-plugin-core): explain rsbuild chunk graph read helpers --- .../src/rsbuild/normalized-client-build.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts b/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts index d6ab6c38b6e..75e4c504994 100644 --- a/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts +++ b/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts @@ -150,6 +150,11 @@ function isManifestJsAsset(file: string): boolean { return !isHotUpdateAsset(file) } +/** + * Wraps a read of a chunk or chunk group property so it runs once per object. + * Later calls with the same object return the same array, so callers must not + * modify it. + */ function memoizeGraphRead( read: (target: TTarget) => Array, ): (target: TTarget) => Array { @@ -164,6 +169,11 @@ function memoizeGraphRead( } } +/** + * Joins the file lists in order and drops duplicates, keeping the first + * occurrence. `currentFile` is left out, so a chunk does not list its own file + * as an import. + */ function mergeJsFiles( fileLists: Array>, currentFile?: string, From aa5c89c783adb51c14cd622e0861e3ed639fa34b Mon Sep 17 00:00:00 2001 From: Guru Narayanan Date: Fri, 2 Oct 2026 13:07:56 +0530 Subject: [PATCH 10/10] refactor(start-plugin-core): address review of rsbuild client build reader Clarify comments on the chunk scan and read helpers, read child groups directly since their merged JS files are already cached per group, keep the unrelated isManifestJsAsset line unchanged, and assert the set of chunks whose modules are read. --- .../src/rsbuild/normalized-client-build.ts | 22 ++++++++----------- .../rsbuild/normalized-client-build.test.ts | 8 +++---- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts b/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts index 75e4c504994..f7b44b53422 100644 --- a/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts +++ b/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts @@ -112,7 +112,8 @@ function getHydrationIdsFromModules( } /** - * Returns the chunks that contain a route-split or hydration module. + * Returns the chunks with a module whose identifier contains `tsr-split` or + * `tss-hydrate`. The extractors check each module's query exactly. */ function findRouteAndHydrationChunks( compilation: RspackCompilation, @@ -144,16 +145,13 @@ function isHotUpdateAsset(file: string): boolean { * Excludes HMR runtime patches. */ function isManifestJsAsset(file: string): boolean { - if (!file.endsWith('.js') && !file.endsWith('.mjs')) { - return false - } + if (!file.endsWith('.js') && !file.endsWith('.mjs')) return false return !isHotUpdateAsset(file) } /** - * Wraps a read of a chunk or chunk group property so it runs once per object. - * Later calls with the same object return the same array, so callers must not - * modify it. + * Caches the result of `read` for each chunk or chunk group. Later calls with + * the same object return the same array, so callers must not modify it. */ function memoizeGraphRead( read: (target: TTarget) => Array, @@ -192,8 +190,9 @@ function mergeJsFiles( } /** - * Reads each chunk and chunk group property once per normalizeRspackClientBuild - * call. Rspack builds a new collection every time one of these properties is read. + * Reads each chunk and chunk group property at most once per + * normalizeRspackClientBuild call, because Rspack builds a new collection on + * every read. Also caches the merged JS files of each group. */ function createChunkGraphReader() { const getFiles = memoizeGraphRead((chunk: RspackCompilationChunk) => @@ -208,9 +207,6 @@ function createChunkGraphReader() { const getGroupChunks = memoizeGraphRead((group: Rspack.ChunkGroup) => Array.from(group.chunks), ) - const getChildren = memoizeGraphRead((group: Rspack.ChunkGroup) => - Array.from(group.childrenIterable), - ) const getChunkJsFiles = memoizeGraphRead((chunk: RspackCompilationChunk) => getFiles(chunk).filter(isManifestJsAsset), ) @@ -221,7 +217,7 @@ function createChunkGraphReader() { ) // JS files of all chunks in a group's child groups, without duplicates. const getChildGroupJsFiles = memoizeGraphRead((group: Rspack.ChunkGroup) => - mergeJsFiles(getChildren(group).map(getGroupJsFiles)), + mergeJsFiles(Array.from(group.childrenIterable).map(getGroupJsFiles)), ) return { diff --git a/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts b/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts index 56857a72acd..55058308ba7 100644 --- a/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts +++ b/packages/start-plugin-core/tests/rsbuild/normalized-client-build.test.ts @@ -249,10 +249,9 @@ describe('normalizeRspackClientBuild', () => { const { compilation, getChunkModules } = makeChunkGraph() normalizeRspackClientBuild(compilation) - expect(getChunkModules.mock.calls.map(([target]) => target.name)).toEqual([ - 'posts', - 'post', - ]) + expect( + new Set(getChunkModules.mock.calls.map(([target]) => target.name)), + ).toEqual(new Set(['posts', 'post'])) }) test('reads each chunk and chunk group property at most once', () => { @@ -260,6 +259,7 @@ describe('normalizeRspackClientBuild', () => { const maxReads = trackGraphPropertyReads(graphObjects) normalizeRspackClientBuild(compilation) + // 1, not 0, also confirms the read counters were installed. expect(maxReads()).toBe(1) })