@@ -17,6 +17,7 @@ import type {
17
17
OutputBundle ,
18
18
OutputChunk ,
19
19
OutputOptions ,
20
+ PluginContext ,
20
21
PreRenderedChunk ,
21
22
RenderedChunk ,
22
23
} from 'rollup'
@@ -174,6 +175,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
174
175
const legacyPolyfills = new Set < string > ( )
175
176
// When discovering polyfills in `renderChunk`, the hook may be non-deterministic, so we group the
176
177
// modern and legacy polyfills in a sorted chunks map for each rendered outputs before merging them.
178
+ // TODO: options object is not identical, so Map won't work
177
179
const outputToChunkFileNameToPolyfills = new WeakMap <
178
180
NormalizedOutputOptions ,
179
181
Map < string , { modern : Set < string > ; legacy : Set < string > } > | null
@@ -282,13 +284,14 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
282
284
return
283
285
}
284
286
285
- const chunkFileNameToPolyfills =
286
- outputToChunkFileNameToPolyfills . get ( opts )
287
- if ( chunkFileNameToPolyfills == null ) {
288
- throw new Error (
289
- 'Internal @vitejs/plugin-legacy error: discovered polyfills should exist' ,
290
- )
291
- }
287
+ // const chunkFileNameToPolyfills =
288
+ // outputToChunkFileNameToPolyfills.get(opts)
289
+ // if (chunkFileNameToPolyfills == null) {
290
+ // throw new Error(
291
+ // 'Internal @vitejs/plugin-legacy error: discovered polyfills should exist',
292
+ // )
293
+ // }
294
+ const chunkFileNameToPolyfills = new Map ( )
292
295
293
296
if ( ! isLegacyBundle ( bundle , opts ) ) {
294
297
// Merge discovered modern polyfills to `modernPolyfills`
@@ -305,6 +308,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
305
308
)
306
309
}
307
310
await buildPolyfillChunk (
311
+ this ,
308
312
config . mode ,
309
313
modernPolyfills ,
310
314
bundle ,
@@ -347,6 +351,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
347
351
}
348
352
349
353
await buildPolyfillChunk (
354
+ this ,
350
355
config . mode ,
351
356
legacyPolyfills ,
352
357
bundle ,
@@ -434,7 +439,9 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
434
439
) : OutputOptions => {
435
440
return {
436
441
...options ,
437
- format : 'system' ,
442
+ // TODO
443
+ format : 'esm' ,
444
+ // format: 'system',
438
445
entryFileNames : getLegacyOutputFileName ( options . entryFileNames ) ,
439
446
chunkFileNames : getLegacyOutputFileName ( options . chunkFileNames ) ,
440
447
}
@@ -455,7 +462,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
455
462
}
456
463
} ,
457
464
458
- async renderChunk ( raw , chunk , opts , { chunks } ) {
465
+ // TODO: meta.chunks not supported
466
+ async renderChunk ( raw , chunk , opts , { chunks } = { chunks : { } } ) {
459
467
if ( config . build . ssr ) {
460
468
return null
461
469
}
@@ -472,11 +480,15 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
472
480
}
473
481
outputToChunkFileNameToPolyfills . set ( opts , chunkFileNameToPolyfills )
474
482
}
475
- const polyfillsDiscovered = chunkFileNameToPolyfills . get ( chunk . fileName )
476
- if ( polyfillsDiscovered == null ) {
477
- throw new Error (
478
- `Internal @vitejs/plugin-legacy error: discovered polyfills for ${ chunk . fileName } should exist` ,
479
- )
483
+ // const polyfillsDiscovered = chunkFileNameToPolyfills.get(chunk.fileName)
484
+ // if (polyfillsDiscovered == null) {
485
+ // throw new Error(
486
+ // `Internal @vitejs/plugin-legacy error: discovered polyfills for ${chunk.fileName} should exist`,
487
+ // )
488
+ // }
489
+ const polyfillsDiscovered = {
490
+ modern : new Set ( ) ,
491
+ legacy : new Set ( ) ,
480
492
}
481
493
482
494
if ( ! isLegacyChunk ( chunk , opts ) ) {
@@ -545,6 +557,12 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
545
557
// transform the legacy chunk with @babel /preset-env
546
558
const sourceMaps = ! ! config . build . sourcemap
547
559
const babel = await loadBabel ( )
560
+ const systemJsPlugins = [
561
+ // @ts -ignore
562
+ ( await import ( '@babel/plugin-transform-dynamic-import' ) ) . default ,
563
+ // @ts -ignore
564
+ ( await import ( '@babel/plugin-transform-modules-systemjs' ) ) . default ,
565
+ ]
548
566
const result = babel . transform ( raw , {
549
567
babelrc : false ,
550
568
configFile : false ,
@@ -557,6 +575,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
557
575
[
558
576
( ) => ( {
559
577
plugins : [
578
+ ...systemJsPlugins ,
560
579
recordAndRemovePolyfillBabelPlugin ( polyfillsDiscovered . legacy ) ,
561
580
replaceLegacyEnvBabelPlugin ( ) ,
562
581
wrapIIFEBabelPlugin ( ) ,
@@ -718,7 +737,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
718
737
// avoid emitting duplicate assets
719
738
for ( const name in bundle ) {
720
739
if ( bundle [ name ] . type === 'asset' && ! / .+ \. m a p $ / . test ( name ) ) {
721
- delete bundle [ name ]
740
+ // TODO: don't delete polyfil chunk emitted as asset
741
+ // delete bundle[name]
722
742
}
723
743
}
724
744
}
@@ -781,6 +801,7 @@ function createBabelPresetEnvOptions(
781
801
}
782
802
783
803
async function buildPolyfillChunk (
804
+ ctx : PluginContext ,
784
805
mode : string ,
785
806
imports : Set < string > ,
786
807
bundle : OutputBundle ,
@@ -846,16 +867,23 @@ async function buildPolyfillChunk(
846
867
}
847
868
}
848
869
870
+ // TODO: adding a whole new chunk to `bundle` is not supported by rolldown.
871
+ // can we use `emitFile(asset)` instead?
872
+ ctx . emitFile ( {
873
+ type : 'asset' ,
874
+ fileName : polyfillChunk . fileName ,
875
+ source : polyfillChunk . code ,
876
+ } )
849
877
// add the chunk to the bundle
850
- bundle [ polyfillChunk . fileName ] = polyfillChunk
878
+ // bundle[polyfillChunk.fileName] = polyfillChunk
851
879
if ( polyfillChunk . sourcemapFileName ) {
852
880
const polyfillChunkMapAsset = _polyfillChunk . output . find (
853
881
( chunk ) =>
854
882
chunk . type === 'asset' &&
855
883
chunk . fileName === polyfillChunk . sourcemapFileName ,
856
884
) as OutputAsset | undefined
857
885
if ( polyfillChunkMapAsset ) {
858
- bundle [ polyfillChunk . sourcemapFileName ] = polyfillChunkMapAsset
886
+ // bundle[polyfillChunk.sourcemapFileName] = polyfillChunkMapAsset
859
887
}
860
888
}
861
889
}
@@ -907,14 +935,15 @@ function prependModenChunkLegacyGuardPlugin(): Plugin {
907
935
}
908
936
909
937
function isLegacyChunk ( chunk : RenderedChunk , options : NormalizedOutputOptions ) {
910
- return options . format === 'system' && chunk . fileName . includes ( '-legacy' )
938
+ return chunk . fileName . includes ( '-legacy' )
939
+ // return options.format === 'system' && chunk.fileName.includes('-legacy')
911
940
}
912
941
913
942
function isLegacyBundle (
914
943
bundle : OutputBundle ,
915
944
options : NormalizedOutputOptions ,
916
945
) {
917
- if ( options . format === 'system' ) {
946
+ if ( true || options . format === 'system' ) {
918
947
const entryChunk = Object . values ( bundle ) . find (
919
948
( output ) => output . type === 'chunk' && output . isEntry ,
920
949
)
0 commit comments