@@ -125,6 +125,12 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
125
125
let targets : Options [ 'targets' ]
126
126
127
127
const genLegacy = options . renderLegacyChunks !== false
128
+ const genModern = options . renderModernChunks !== false
129
+ if ( ! genLegacy && ! genModern ) {
130
+ throw new Error (
131
+ '`renderLegacyChunks` and `renderModernChunks` cannot be both false' ,
132
+ )
133
+ }
128
134
129
135
const debugFlags = ( process . env . DEBUG || '' ) . split ( ',' )
130
136
const isDebug =
@@ -136,7 +142,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
136
142
const modernPolyfills = new Set < string > ( )
137
143
const legacyPolyfills = new Set < string > ( )
138
144
139
- if ( Array . isArray ( options . modernPolyfills ) ) {
145
+ if ( Array . isArray ( options . modernPolyfills ) && genModern ) {
140
146
options . modernPolyfills . forEach ( ( i ) => {
141
147
modernPolyfills . add (
142
148
i . includes ( '/' ) ? `core-js/${ i } ` : `core-js/modules/${ i } .js` ,
@@ -343,9 +349,15 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
343
349
const { rollupOptions } = config . build
344
350
const { output } = rollupOptions
345
351
if ( Array . isArray ( output ) ) {
346
- rollupOptions . output = [ ...output . map ( createLegacyOutput ) , ...output ]
352
+ rollupOptions . output = [
353
+ ...output . map ( createLegacyOutput ) ,
354
+ ...( genModern ? output : [ ] ) ,
355
+ ]
347
356
} else {
348
- rollupOptions . output = [ createLegacyOutput ( output ) , output || { } ]
357
+ rollupOptions . output = [
358
+ createLegacyOutput ( output ) ,
359
+ ...( genModern ? [ output || { } ] : [ ] ) ,
360
+ ]
349
361
}
350
362
} ,
351
363
@@ -357,7 +369,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
357
369
if ( ! isLegacyChunk ( chunk , opts ) ) {
358
370
if (
359
371
options . modernPolyfills &&
360
- ! Array . isArray ( options . modernPolyfills )
372
+ ! Array . isArray ( options . modernPolyfills ) &&
373
+ genModern
361
374
) {
362
375
// analyze and record modern polyfills
363
376
await detectPolyfills ( raw , { esmodules : true } , modernPolyfills )
@@ -455,50 +468,59 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
455
468
if ( config . build . ssr ) return
456
469
if ( ! chunk ) return
457
470
if ( chunk . fileName . includes ( '-legacy' ) ) {
458
- // The legacy bundle is built first, and its index.html isn't actually
459
- // emitted . Here we simply record its corresponding legacy chunk.
471
+ // The legacy bundle is built first, and its index.html isn't actually emitted if
472
+ // modern bundle will be generated . Here we simply record its corresponding legacy chunk.
460
473
facadeToLegacyChunkMap . set ( chunk . facadeModuleId , chunk . fileName )
461
- return
474
+ if ( genModern ) {
475
+ return
476
+ }
477
+ }
478
+ if ( ! genModern ) {
479
+ html = html . replace ( / < s c r i p t t y p e = " m o d u l e " .* ?< \/ s c r i p t > / g, '' )
462
480
}
463
481
464
482
const tags : HtmlTagDescriptor [ ] = [ ]
465
483
const htmlFilename = chunk . facadeModuleId ?. replace ( / \? .* $ / , '' )
466
484
467
485
// 1. inject modern polyfills
468
- const modernPolyfillFilename = facadeToModernPolyfillMap . get (
469
- chunk . facadeModuleId ,
470
- )
471
-
472
- if ( modernPolyfillFilename ) {
473
- tags . push ( {
474
- tag : 'script' ,
475
- attrs : {
476
- type : 'module' ,
477
- crossorigin : true ,
478
- src : toAssetPathFromHtml (
479
- modernPolyfillFilename ,
480
- chunk . facadeModuleId ! ,
481
- config ,
482
- ) ,
483
- } ,
484
- } )
485
- } else if ( modernPolyfills . size ) {
486
- throw new Error (
487
- `No corresponding modern polyfill chunk found for ${ htmlFilename } ` ,
486
+ if ( genModern ) {
487
+ const modernPolyfillFilename = facadeToModernPolyfillMap . get (
488
+ chunk . facadeModuleId ,
488
489
)
490
+
491
+ if ( modernPolyfillFilename ) {
492
+ tags . push ( {
493
+ tag : 'script' ,
494
+ attrs : {
495
+ type : 'module' ,
496
+ crossorigin : true ,
497
+ src : toAssetPathFromHtml (
498
+ modernPolyfillFilename ,
499
+ chunk . facadeModuleId ! ,
500
+ config ,
501
+ ) ,
502
+ } ,
503
+ } )
504
+ } else if ( modernPolyfills . size ) {
505
+ throw new Error (
506
+ `No corresponding modern polyfill chunk found for ${ htmlFilename } ` ,
507
+ )
508
+ }
489
509
}
490
510
491
511
if ( ! genLegacy ) {
492
512
return { html, tags }
493
513
}
494
514
495
515
// 2. inject Safari 10 nomodule fix
496
- tags . push ( {
497
- tag : 'script' ,
498
- attrs : { nomodule : true } ,
499
- children : safari10NoModuleFix ,
500
- injectTo : 'body' ,
501
- } )
516
+ if ( genModern ) {
517
+ tags . push ( {
518
+ tag : 'script' ,
519
+ attrs : { nomodule : genModern } ,
520
+ children : safari10NoModuleFix ,
521
+ injectTo : 'body' ,
522
+ } )
523
+ }
502
524
503
525
// 3. inject legacy polyfills
504
526
const legacyPolyfillFilename = facadeToLegacyPolyfillMap . get (
@@ -508,7 +530,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
508
530
tags . push ( {
509
531
tag : 'script' ,
510
532
attrs : {
511
- nomodule : true ,
533
+ nomodule : genModern ,
512
534
crossorigin : true ,
513
535
id : legacyPolyfillId ,
514
536
src : toAssetPathFromHtml (
@@ -534,7 +556,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
534
556
tags . push ( {
535
557
tag : 'script' ,
536
558
attrs : {
537
- nomodule : true ,
559
+ nomodule : genModern ,
538
560
crossorigin : true ,
539
561
// we set the entry path on the element as an attribute so that the
540
562
// script content will stay consistent - which allows using a constant
@@ -556,7 +578,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
556
578
}
557
579
558
580
// 5. inject dynamic import fallback entry
559
- if ( genLegacy && legacyPolyfillFilename && legacyEntryFilename ) {
581
+ if ( legacyPolyfillFilename && legacyEntryFilename && genModern ) {
560
582
tags . push ( {
561
583
tag : 'script' ,
562
584
attrs : { type : 'module' } ,
@@ -582,7 +604,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
582
604
return
583
605
}
584
606
585
- if ( isLegacyBundle ( bundle , opts ) ) {
607
+ if ( isLegacyBundle ( bundle , opts ) && genModern ) {
586
608
// avoid emitting duplicate assets
587
609
for ( const name in bundle ) {
588
610
if ( bundle [ name ] . type === 'asset' && ! / .+ \. m a p $ / . test ( name ) ) {
0 commit comments