@@ -159,10 +159,11 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
159
159
const modernPolyfills = new Set < string > ( )
160
160
const legacyPolyfills = new Set < string > ( )
161
161
// When discovering polyfills in `renderChunk`, the hook may be non-deterministic, so we group the
162
- // modern and legacy polyfills in a sorted map before merging them.
163
- let chunkFileNameToPolyfills :
164
- | Map < string , { modern : Set < string > ; legacy : Set < string > } >
165
- | undefined
162
+ // modern and legacy polyfills in a sorted chunks map for each rendered outputs before merging them.
163
+ const outputToChunkFileNameToPolyfills = new WeakMap <
164
+ NormalizedOutputOptions ,
165
+ Map < string , { modern : Set < string > ; legacy : Set < string > } > | null
166
+ > ( )
166
167
167
168
if ( Array . isArray ( options . modernPolyfills ) && genModern ) {
168
169
options . modernPolyfills . forEach ( ( i ) => {
@@ -267,12 +268,18 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
267
268
return
268
269
}
269
270
271
+ const chunkFileNameToPolyfills =
272
+ outputToChunkFileNameToPolyfills . get ( opts )
273
+ if ( chunkFileNameToPolyfills == null ) {
274
+ throw new Error (
275
+ 'Internal @vitejs/plugin-legacy error: discovered polyfills should exist' ,
276
+ )
277
+ }
278
+
270
279
if ( ! isLegacyBundle ( bundle , opts ) ) {
271
280
// Merge discovered modern polyfills to `modernPolyfills`
272
- if ( chunkFileNameToPolyfills ) {
273
- for ( const { modern } of chunkFileNameToPolyfills . values ( ) ) {
274
- modern . forEach ( ( p ) => modernPolyfills . add ( p ) )
275
- }
281
+ for ( const { modern } of chunkFileNameToPolyfills . values ( ) ) {
282
+ modern . forEach ( ( p ) => modernPolyfills . add ( p ) )
276
283
}
277
284
if ( ! modernPolyfills . size ) {
278
285
return
@@ -303,10 +310,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
303
310
}
304
311
305
312
// Merge discovered legacy polyfills to `legacyPolyfills`
306
- if ( chunkFileNameToPolyfills ) {
307
- for ( const { legacy } of chunkFileNameToPolyfills . values ( ) ) {
308
- legacy . forEach ( ( p ) => legacyPolyfills . add ( p ) )
309
- }
313
+ for ( const { legacy } of chunkFileNameToPolyfills . values ( ) ) {
314
+ legacy . forEach ( ( p ) => legacyPolyfills . add ( p ) )
310
315
}
311
316
312
317
// legacy bundle
@@ -348,8 +353,9 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
348
353
enforce : 'post' ,
349
354
apply : 'build' ,
350
355
351
- renderStart ( ) {
352
- chunkFileNameToPolyfills = undefined
356
+ renderStart ( opts ) {
357
+ // Empty the nested map for this output
358
+ outputToChunkFileNameToPolyfills . set ( opts , null )
353
359
} ,
354
360
355
361
configResolved ( _config ) {
@@ -438,6 +444,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
438
444
}
439
445
440
446
// On first run, intialize the map with sorted chunk file names
447
+ let chunkFileNameToPolyfills = outputToChunkFileNameToPolyfills . get ( opts )
441
448
if ( chunkFileNameToPolyfills == null ) {
442
449
chunkFileNameToPolyfills = new Map ( )
443
450
for ( const fileName in chunks ) {
@@ -446,8 +453,14 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
446
453
legacy : new Set ( ) ,
447
454
} )
448
455
}
456
+ outputToChunkFileNameToPolyfills . set ( opts , chunkFileNameToPolyfills )
457
+ }
458
+ const polyfillsDiscovered = chunkFileNameToPolyfills . get ( chunk . fileName )
459
+ if ( polyfillsDiscovered == null ) {
460
+ throw new Error (
461
+ `Internal @vitejs/plugin-legacy error: discovered polyfills for ${ chunk . fileName } should exist` ,
462
+ )
449
463
}
450
- const polyfillsDiscovered = chunkFileNameToPolyfills . get ( chunk . fileName ) !
451
464
452
465
if ( ! isLegacyChunk ( chunk , opts ) ) {
453
466
if (
0 commit comments