@@ -158,6 +158,11 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
158
158
const facadeToModernPolyfillMap = new Map ( )
159
159
const modernPolyfills = new Set < string > ( )
160
160
const legacyPolyfills = new Set < string > ( )
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
161
166
162
167
if ( Array . isArray ( options . modernPolyfills ) && genModern ) {
163
168
options . modernPolyfills . forEach ( ( i ) => {
@@ -263,6 +268,12 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
263
268
}
264
269
265
270
if ( ! isLegacyBundle ( bundle , opts ) ) {
271
+ // Merge discovered modern polyfills to `modernPolyfills`
272
+ if ( chunkFileNameToPolyfills ) {
273
+ for ( const { modern } of chunkFileNameToPolyfills . values ( ) ) {
274
+ modern . forEach ( ( p ) => modernPolyfills . add ( p ) )
275
+ }
276
+ }
266
277
if ( ! modernPolyfills . size ) {
267
278
return
268
279
}
@@ -291,6 +302,13 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
291
302
return
292
303
}
293
304
305
+ // Merge discovered legacy polyfills to `legacyPolyfills`
306
+ if ( chunkFileNameToPolyfills ) {
307
+ for ( const { legacy } of chunkFileNameToPolyfills . values ( ) ) {
308
+ legacy . forEach ( ( p ) => legacyPolyfills . add ( p ) )
309
+ }
310
+ }
311
+
294
312
// legacy bundle
295
313
if ( options . polyfills !== false ) {
296
314
// check if the target needs Promise polyfill because SystemJS relies on it
@@ -330,6 +348,10 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
330
348
enforce : 'post' ,
331
349
apply : 'build' ,
332
350
351
+ renderStart ( ) {
352
+ chunkFileNameToPolyfills = undefined
353
+ } ,
354
+
333
355
configResolved ( _config ) {
334
356
if ( _config . build . lib ) {
335
357
throw new Error ( '@vitejs/plugin-legacy does not support library mode.' )
@@ -410,19 +432,31 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
410
432
}
411
433
} ,
412
434
413
- async renderChunk ( raw , chunk , opts ) {
435
+ async renderChunk ( raw , chunk , opts , { chunks } ) {
414
436
if ( config . build . ssr ) {
415
437
return null
416
438
}
417
439
440
+ // On first run, intialize the map with sorted chunk file names
441
+ if ( chunkFileNameToPolyfills == null ) {
442
+ chunkFileNameToPolyfills = new Map ( )
443
+ for ( const fileName in chunks ) {
444
+ chunkFileNameToPolyfills . set ( fileName , {
445
+ modern : new Set ( ) ,
446
+ legacy : new Set ( ) ,
447
+ } )
448
+ }
449
+ }
450
+ const polyfillsDiscovered = chunkFileNameToPolyfills . get ( chunk . fileName ) !
451
+
418
452
if ( ! isLegacyChunk ( chunk , opts ) ) {
419
453
if (
420
454
options . modernPolyfills &&
421
455
! Array . isArray ( options . modernPolyfills ) &&
422
456
genModern
423
457
) {
424
458
// analyze and record modern polyfills
425
- await detectPolyfills ( raw , modernTargets , modernPolyfills )
459
+ await detectPolyfills ( raw , modernTargets , polyfillsDiscovered . modern )
426
460
}
427
461
428
462
const ms = new MagicString ( raw )
@@ -493,7 +527,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
493
527
[
494
528
( ) => ( {
495
529
plugins : [
496
- recordAndRemovePolyfillBabelPlugin ( legacyPolyfills ) ,
530
+ recordAndRemovePolyfillBabelPlugin ( polyfillsDiscovered . legacy ) ,
497
531
replaceLegacyEnvBabelPlugin ( ) ,
498
532
wrapIIFEBabelPlugin ( ) ,
499
533
] ,
0 commit comments