@@ -1159,7 +1159,8 @@ function vitePluginUseClient(
1159
1159
} ) ??
1160
1160
// use original module id as name by default
1161
1161
manager . toRelativeId ( meta . importId )
1162
- name = name . replaceAll ( '..' , '__' )
1162
+ // ensure clean virtual id to avoid interfering with other plugins
1163
+ name = cleanUrl ( name . replaceAll ( '..' , '__' ) )
1163
1164
const group = ( manager . clientReferenceGroups [ name ] ??= [ ] )
1164
1165
group . push ( meta )
1165
1166
meta . groupChunkId = `\0virtual:vite-rsc/client-references/group/${ name } `
@@ -1261,19 +1262,36 @@ function vitePluginUseClient(
1261
1262
generateBundle ( _options , bundle ) {
1262
1263
if ( this . environment . name !== serverEnvironmentName ) return
1263
1264
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
+
1266
1269
for ( const chunk of Object . values ( bundle ) ) {
1267
1270
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 ) {
1269
1273
const meta = manager . clientReferenceMetaMap [ id ]
1270
1274
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 )
1271
1293
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
1277
1295
}
1278
1296
}
1279
1297
}
0 commit comments