Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rsbuild-manifest-own-chunks.md
Original file line number Diff line number Diff line change
@@ -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.
31 changes: 24 additions & 7 deletions packages/start-plugin-core/src/rsbuild/normalized-client-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RspackCompilationChunk>()
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<RspackCompilationChunk> | undefined
for (const group of chunk.groupsIterable) {
const groupChunks = new Set<RspackCompilationChunk>()
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
Loading