@@ -602,7 +602,7 @@ async function buildEnvironment(
602
602
const outDir = resolve ( options . outDir )
603
603
604
604
// inject environment and ssr arg to plugin load/transform hooks
605
- const chunkMetadataMap = new Map < string , ChunkMetadata > ( )
605
+ const chunkMetadataMap = new ChunkMetadataMap ( )
606
606
const plugins = environment . plugins . map ( ( p ) =>
607
607
injectEnvironmentToHooks ( environment , chunkMetadataMap , p ) ,
608
608
)
@@ -859,6 +859,7 @@ async function buildEnvironment(
859
859
watcher . on ( 'event' , ( event ) => {
860
860
if ( event . code === 'BUNDLE_START' ) {
861
861
logger . info ( colors . cyan ( `\nbuild started...` ) )
862
+ chunkMetadataMap . clearResetChunks ( )
862
863
if ( options . write ) {
863
864
prepareOutDir ( resolvedOutDirs , emptyOutDir , environment )
864
865
}
@@ -1200,9 +1201,51 @@ function isExternal(id: string, test: string | RegExp) {
1200
1201
}
1201
1202
}
1202
1203
1204
+ export class ChunkMetadataMap {
1205
+ private _inner = new Map < string , ChunkMetadata > ( )
1206
+ private _resetChunks = new Set < string > ( )
1207
+
1208
+ private _getKey ( chunk : RenderedChunk | OutputChunk ) : string {
1209
+ return 'preliminaryFileName' in chunk
1210
+ ? chunk . preliminaryFileName
1211
+ : chunk . fileName
1212
+ }
1213
+
1214
+ private _getDefaultValue ( chunk : RenderedChunk | OutputChunk ) : ChunkMetadata {
1215
+ return {
1216
+ importedAssets : new Set ( ) ,
1217
+ importedCss : new Set ( ) ,
1218
+ // NOTE: adding this as a workaround for now ideally we'd want to remove this workaround
1219
+ // use shared `chunk.modules` object to allow mutation on js side plugins
1220
+ __modules : chunk . modules ,
1221
+ }
1222
+ }
1223
+
1224
+ get ( chunk : RenderedChunk | OutputChunk ) : ChunkMetadata {
1225
+ const key = this . _getKey ( chunk )
1226
+ if ( ! this . _inner . has ( key ) ) {
1227
+ this . _inner . set ( key , this . _getDefaultValue ( chunk ) )
1228
+ }
1229
+ return this . _inner . get ( key ) !
1230
+ }
1231
+
1232
+ // reset chunk metadata on the first RenderChunk call for watch mode
1233
+ reset ( chunk : RenderedChunk | OutputChunk ) : void {
1234
+ const key = this . _getKey ( chunk )
1235
+ if ( this . _resetChunks . has ( key ) ) return
1236
+
1237
+ this . _resetChunks . add ( key )
1238
+ this . _inner . set ( key , this . _getDefaultValue ( chunk ) )
1239
+ }
1240
+
1241
+ clearResetChunks ( ) : void {
1242
+ this . _resetChunks . clear ( )
1243
+ }
1244
+ }
1245
+
1203
1246
export function injectEnvironmentToHooks (
1204
1247
environment : BuildEnvironment ,
1205
- chunkMetadataMap : Map < string , ChunkMetadata > ,
1248
+ chunkMetadataMap : ChunkMetadataMap ,
1206
1249
plugin : Plugin ,
1207
1250
) : Plugin {
1208
1251
const { resolveId, load, transform } = plugin
@@ -1319,7 +1362,7 @@ function wrapEnvironmentTransform(
1319
1362
1320
1363
function wrapEnvironmentHook < HookName extends keyof Plugin > (
1321
1364
environment : BuildEnvironment ,
1322
- chunkMetadataMap : Map < string , ChunkMetadata > ,
1365
+ chunkMetadataMap : ChunkMetadataMap ,
1323
1366
plugin : Plugin ,
1324
1367
hookName : HookName ,
1325
1368
) : Plugin [ HookName ] {
@@ -1334,7 +1377,7 @@ function wrapEnvironmentHook<HookName extends keyof Plugin>(
1334
1377
...args : any [ ]
1335
1378
) {
1336
1379
if ( hookName === 'renderChunk' ) {
1337
- injectChunkMetadata ( chunkMetadataMap , args [ 1 ] )
1380
+ injectChunkMetadata ( chunkMetadataMap , args [ 1 ] , true )
1338
1381
}
1339
1382
if ( hookName === 'augmentChunkHash' ) {
1340
1383
injectChunkMetadata ( chunkMetadataMap , args [ 0 ] )
@@ -1361,25 +1404,17 @@ function wrapEnvironmentHook<HookName extends keyof Plugin>(
1361
1404
}
1362
1405
1363
1406
function injectChunkMetadata (
1364
- chunkMetadataMap : Map < string , ChunkMetadata > ,
1407
+ chunkMetadataMap : ChunkMetadataMap ,
1365
1408
chunk : RenderedChunk | OutputChunk ,
1409
+ resetChunkMetadata = false ,
1366
1410
) {
1367
- const key =
1368
- 'preliminaryFileName' in chunk ? chunk . preliminaryFileName : chunk . fileName
1369
- if ( ! chunkMetadataMap . has ( key ) ) {
1370
- chunkMetadataMap . set ( key , {
1371
- importedAssets : new Set ( ) ,
1372
- importedCss : new Set ( ) ,
1373
- // NOTE: adding this as a workaround for now ideally we'd want to remove this workaround
1374
- // use shared `chunk.modules` object
1375
- // to allow mutation on js side plugins
1376
- __modules : chunk . modules ,
1377
- } )
1411
+ if ( resetChunkMetadata ) {
1412
+ chunkMetadataMap . reset ( chunk )
1378
1413
}
1379
1414
// define instead of assign to avoid detected as a change
1380
1415
// https://github.com/rolldown/rolldown/blob/f4c5ff27799f2b0152c689c398e61bc7d30429ff/packages/rolldown/src/utils/transform-to-rollup-output.ts#L87
1381
1416
Object . defineProperty ( chunk , 'viteMetadata' , {
1382
- value : chunkMetadataMap . get ( key ) ,
1417
+ value : chunkMetadataMap . get ( chunk ) ,
1383
1418
enumerable : true ,
1384
1419
} )
1385
1420
Object . defineProperty ( chunk , 'modules' , {
0 commit comments