@@ -13,7 +13,9 @@ import type {
13
13
} from 'vite'
14
14
import type {
15
15
NormalizedOutputOptions ,
16
+ OutputAsset ,
16
17
OutputBundle ,
18
+ OutputChunk ,
17
19
OutputOptions ,
18
20
PreRenderedChunk ,
19
21
RenderedChunk ,
@@ -302,7 +304,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
302
304
modernPolyfills ,
303
305
)
304
306
}
305
- const polyfillChunk = await buildPolyfillChunk (
307
+ await buildPolyfillChunk (
306
308
config . mode ,
307
309
modernPolyfills ,
308
310
bundle ,
@@ -311,10 +313,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
311
313
'es' ,
312
314
opts ,
313
315
true ,
316
+ genLegacy ,
314
317
)
315
- if ( genLegacy && polyfillChunk ) {
316
- polyfillChunk . code = modernChunkLegacyGuard + polyfillChunk . code
317
- }
318
318
return
319
319
}
320
320
@@ -789,20 +789,25 @@ async function buildPolyfillChunk(
789
789
format : 'iife' | 'es' ,
790
790
rollupOutputOptions : NormalizedOutputOptions ,
791
791
excludeSystemJS ?: boolean ,
792
+ prependModenChunkLegacyGuard ?: boolean ,
792
793
) {
793
- let { minify, assetsDir } = buildOptions
794
+ let { minify, assetsDir, sourcemap } = buildOptions
794
795
minify = minify ? 'terser' : false
795
796
const res = await build ( {
796
797
mode,
797
798
// so that everything is resolved from here
798
799
root : path . dirname ( fileURLToPath ( import . meta. url ) ) ,
799
800
configFile : false ,
800
801
logLevel : 'error' ,
801
- plugins : [ polyfillsPlugin ( imports , excludeSystemJS ) ] ,
802
+ plugins : [
803
+ polyfillsPlugin ( imports , excludeSystemJS ) ,
804
+ prependModenChunkLegacyGuard && prependModenChunkLegacyGuardPlugin ( ) ,
805
+ ] ,
802
806
build : {
803
807
write : false ,
804
808
minify,
805
809
assetsDir,
810
+ sourcemap,
806
811
rollupOptions : {
807
812
input : {
808
813
polyfills : polyfillId ,
@@ -828,7 +833,9 @@ async function buildPolyfillChunk(
828
833
} )
829
834
const _polyfillChunk = Array . isArray ( res ) ? res [ 0 ] : res
830
835
if ( ! ( 'output' in _polyfillChunk ) ) return
831
- const polyfillChunk = _polyfillChunk . output [ 0 ]
836
+ const polyfillChunk = _polyfillChunk . output . find (
837
+ ( chunk ) => chunk . type === 'chunk' && chunk . isEntry ,
838
+ ) as OutputChunk
832
839
833
840
// associate the polyfill chunk to every entry chunk so that we can retrieve
834
841
// the polyfill filename in index html transform
@@ -841,8 +848,16 @@ async function buildPolyfillChunk(
841
848
842
849
// add the chunk to the bundle
843
850
bundle [ polyfillChunk . fileName ] = polyfillChunk
844
-
845
- return polyfillChunk
851
+ if ( polyfillChunk . sourcemapFileName ) {
852
+ const polyfillChunkMapAsset = _polyfillChunk . output . find (
853
+ ( chunk ) =>
854
+ chunk . type === 'asset' &&
855
+ chunk . fileName === polyfillChunk . sourcemapFileName ,
856
+ ) as OutputAsset | undefined
857
+ if ( polyfillChunkMapAsset ) {
858
+ bundle [ polyfillChunk . sourcemapFileName ] = polyfillChunkMapAsset
859
+ }
860
+ }
846
861
}
847
862
848
863
const polyfillId = '\0vite/legacy-polyfills'
@@ -869,6 +884,28 @@ function polyfillsPlugin(
869
884
}
870
885
}
871
886
887
+ function prependModenChunkLegacyGuardPlugin ( ) : Plugin {
888
+ let sourceMapEnabled ! : boolean
889
+ return {
890
+ name : 'vite:legacy-prepend-moden-chunk-legacy-guard' ,
891
+ configResolved ( config ) {
892
+ sourceMapEnabled = ! ! config . build . sourcemap
893
+ } ,
894
+ renderChunk ( code ) {
895
+ if ( ! sourceMapEnabled ) {
896
+ return modernChunkLegacyGuard + code
897
+ }
898
+
899
+ const ms = new MagicString ( code )
900
+ ms . prepend ( modernChunkLegacyGuard )
901
+ return {
902
+ code : ms . toString ( ) ,
903
+ map : ms . generateMap ( { hires : 'boundary' } ) ,
904
+ }
905
+ } ,
906
+ }
907
+ }
908
+
872
909
function isLegacyChunk ( chunk : RenderedChunk , options : NormalizedOutputOptions ) {
873
910
return options . format === 'system' && chunk . fileName . includes ( '-legacy' )
874
911
}
0 commit comments