diff --git a/.changeset/rsbuild-manifest-own-chunks.md b/.changeset/rsbuild-manifest-own-chunks.md new file mode 100644 index 00000000000..04e8e299283 --- /dev/null +++ b/.changeset/rsbuild-manifest-own-chunks.md @@ -0,0 +1,5 @@ +--- +'@tanstack/start-plugin-core': patch +--- + +Rsbuild: a route's client manifest entry lists only its own chunks and CSS. When two routes shared a module in a chunk of its own, each route's entry also listed the other route's chunk as a preload and its stylesheet. 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 591d6b0113a..d19ad87ed69 100644 --- a/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts +++ b/packages/start-plugin-core/src/rsbuild/normalized-client-build.ts @@ -230,16 +230,33 @@ function computeStaticDependencyChunks( // A shared chunk can belong to several route groups. Group membership // limits where dependencies can be loaded from; it does not establish // an import from the shared chunk back to every route in those groups. - const availableChunks = new Set() - const groups = new Set(chunk.groupsIterable) - for (const group of groups) { - for (const availableChunk of group.chunks) { - availableChunks.add(availableChunk) + // A chunk counts as available only when every group of this chunk (or + // one of its ancestors) has it: a module duplicated into each route's + // chunk is satisfied per group, by that group's own chunk, so neither + // route's chunk is a dependency of the shared chunk (#8780). + let availableChunks: Set | undefined + for (const group of chunk.groupsIterable) { + const groupChunks = new Set() + const ancestors = new Set([group]) + for (const ancestor of ancestors) { + for (const availableChunk of ancestor.chunks) { + groupChunks.add(availableChunk) + } + for (const parent of ancestor.getParents()) { + ancestors.add(parent) + } } - for (const parent of group.getParents()) { - groups.add(parent) + if (availableChunks === undefined) { + availableChunks = groupChunks + } else { + for (const availableChunk of availableChunks) { + if (!groupChunks.has(availableChunk)) { + availableChunks.delete(availableChunk) + } + } } } + availableChunks ??= new Set() for (const module of modules) { for (const dependency of module.dependencies) { 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 f563c57bf7d..ba9a033c80c 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 @@ -496,6 +496,77 @@ describe('normalizeRspackClientBuild', () => { ]) }) + // #8780: a chunk of a module two routes share must not import either + // route's chunk for a module duplicated into each of them (a chunk is a + // dependency only when every group of the shared chunk has it). + test('does not import a sibling route chunk through a module duplicated into each route', async () => { + const { clientBuild, directory } = await buildClientFixture( + { + 'index.js': ` + globalThis.routes = [ + () => import(/* webpackChunkName: "alpha" */ './alpha.js?tsr-split=component'), + () => import(/* webpackChunkName: "beta" */ './beta.js?tsr-split=component'), + ] + `, + 'alpha.js': ` + import { shared } from './shared.js' + import './alpha.css' + export const render = () => shared() + 'alpha' + `, + 'beta.js': ` + import { shared } from './shared.js' + import './beta.css' + export const render = () => shared() + 'beta' + `, + 'shared.js': `import { widget } from './widget.js'; export const shared = () => widget()`, + 'widget.js': `export const widget = () => 'widget'`, + 'alpha.css': '.alpha { color: red; }', + 'beta.css': '.beta { color: blue; }', + }, + { + optimization: { + splitChunks: { + minSize: 0, + cacheGroups: { + default: false, + defaultVendors: false, + shared: { + name: 'shared', + test: /shared\.js$/, + chunks: 'async', + enforce: true, + }, + }, + }, + }, + }, + ) + + expect(clientBuild.chunksByFileName.get('shared.js')?.imports).toEqual([]) + const manifest = buildStartManifest({ + clientBuild, + basePath: '/', + routeTreeRoutes: { + __root__: { + filePath: join(directory, 'root.js'), + children: ['/alpha', '/beta'], + }, + '/alpha': { filePath: join(directory, 'alpha.js') }, + '/beta': { filePath: join(directory, 'beta.js') }, + }, + }) + expect(manifest.routes['/alpha']?.css).toEqual(['/alpha.css']) + expect(manifest.routes['/alpha']?.preloads).toEqual([ + '/alpha.js', + '/shared.js', + ]) + expect(manifest.routes['/beta']?.css).toEqual(['/beta.css']) + expect(manifest.routes['/beta']?.preloads).toEqual([ + '/beta.js', + '/shared.js', + ]) + }) + test('collects nested CSS imports before the importing route stylesheet', async () => { const { clientBuild, directory } = await buildClientFixture( {