Skip to content

Commit 9869e2c

Browse files
authored
fix(rsc): normalize group chunk virtual id properly (#770)
1 parent d9da80f commit 9869e2c

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

packages/plugin-rsc/src/plugin.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,8 @@ function vitePluginUseClient(
11591159
}) ??
11601160
// use original module id as name by default
11611161
manager.toRelativeId(meta.importId)
1162-
name = name.replaceAll('..', '__')
1162+
// ensure clean virtual id to avoid interfering with other plugins
1163+
name = cleanUrl(name.replaceAll('..', '__'))
11631164
const group = (manager.clientReferenceGroups[name] ??= [])
11641165
group.push(meta)
11651166
meta.groupChunkId = `\0virtual:vite-rsc/client-references/group/${name}`
@@ -1261,19 +1262,36 @@ function vitePluginUseClient(
12611262
generateBundle(_options, bundle) {
12621263
if (this.environment.name !== serverEnvironmentName) return
12631264

1264-
// track used exports of client references in rsc build
1265-
// to tree shake unused exports in browser and ssr build
1265+
// analyze rsc build to inform later client reference building.
1266+
// - track used client reference exports to tree-shake unused ones
1267+
// - generate associated server chunk name by grouping client references
1268+
12661269
for (const chunk of Object.values(bundle)) {
12671270
if (chunk.type === 'chunk') {
1268-
for (const [id, mod] of Object.entries(chunk.modules)) {
1271+
const metas: [string, ClientReferenceMeta][] = []
1272+
for (const id of chunk.moduleIds) {
12691273
const meta = manager.clientReferenceMetaMap[id]
12701274
if (meta) {
1275+
metas.push([id, meta])
1276+
}
1277+
}
1278+
if (metas.length > 0) {
1279+
// this name is used for client reference group virtual chunk name,
1280+
// which should have a stable and understandle name.
1281+
let serverChunk: string
1282+
if (chunk.facadeModuleId) {
1283+
serverChunk =
1284+
'facade:' + manager.toRelativeId(chunk.facadeModuleId)
1285+
} else {
1286+
serverChunk =
1287+
'shared:' +
1288+
manager.toRelativeId(metas.map(([id]) => id).sort()[0]!)
1289+
}
1290+
for (const [id, meta] of metas) {
1291+
const mod = chunk.modules[id]
1292+
assert(mod)
12711293
meta.renderedExports = mod.renderedExports
1272-
meta.serverChunk =
1273-
(chunk.facadeModuleId ? 'facade:' : 'non-facade:') +
1274-
manager.toRelativeId(
1275-
chunk.facadeModuleId ?? [...chunk.moduleIds].sort()[0]!,
1276-
)
1294+
meta.serverChunk = serverChunk
12771295
}
12781296
}
12791297
}

0 commit comments

Comments
 (0)