Skip to content

Commit f311ff3

Browse files
authored
fix(legacy): generate sourcemap for polyfill chunks (#18250)
1 parent a03bb0e commit f311ff3

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

packages/plugin-legacy/src/index.ts

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import type {
1313
} from 'vite'
1414
import type {
1515
NormalizedOutputOptions,
16+
OutputAsset,
1617
OutputBundle,
18+
OutputChunk,
1719
OutputOptions,
1820
PreRenderedChunk,
1921
RenderedChunk,
@@ -302,7 +304,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
302304
modernPolyfills,
303305
)
304306
}
305-
const polyfillChunk = await buildPolyfillChunk(
307+
await buildPolyfillChunk(
306308
config.mode,
307309
modernPolyfills,
308310
bundle,
@@ -311,10 +313,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
311313
'es',
312314
opts,
313315
true,
316+
genLegacy,
314317
)
315-
if (genLegacy && polyfillChunk) {
316-
polyfillChunk.code = modernChunkLegacyGuard + polyfillChunk.code
317-
}
318318
return
319319
}
320320

@@ -789,20 +789,25 @@ async function buildPolyfillChunk(
789789
format: 'iife' | 'es',
790790
rollupOutputOptions: NormalizedOutputOptions,
791791
excludeSystemJS?: boolean,
792+
prependModenChunkLegacyGuard?: boolean,
792793
) {
793-
let { minify, assetsDir } = buildOptions
794+
let { minify, assetsDir, sourcemap } = buildOptions
794795
minify = minify ? 'terser' : false
795796
const res = await build({
796797
mode,
797798
// so that everything is resolved from here
798799
root: path.dirname(fileURLToPath(import.meta.url)),
799800
configFile: false,
800801
logLevel: 'error',
801-
plugins: [polyfillsPlugin(imports, excludeSystemJS)],
802+
plugins: [
803+
polyfillsPlugin(imports, excludeSystemJS),
804+
prependModenChunkLegacyGuard && prependModenChunkLegacyGuardPlugin(),
805+
],
802806
build: {
803807
write: false,
804808
minify,
805809
assetsDir,
810+
sourcemap,
806811
rollupOptions: {
807812
input: {
808813
polyfills: polyfillId,
@@ -828,7 +833,9 @@ async function buildPolyfillChunk(
828833
})
829834
const _polyfillChunk = Array.isArray(res) ? res[0] : res
830835
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
832839

833840
// associate the polyfill chunk to every entry chunk so that we can retrieve
834841
// the polyfill filename in index html transform
@@ -841,8 +848,16 @@ async function buildPolyfillChunk(
841848

842849
// add the chunk to the bundle
843850
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+
}
846861
}
847862

848863
const polyfillId = '\0vite/legacy-polyfills'
@@ -869,6 +884,28 @@ function polyfillsPlugin(
869884
}
870885
}
871886

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+
872909
function isLegacyChunk(chunk: RenderedChunk, options: NormalizedOutputOptions) {
873910
return options.format === 'system' && chunk.fileName.includes('-legacy')
874911
}

playground/legacy/__tests__/legacy.spec.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,21 @@ describe.runIf(isBuild)('build', () => {
143143

144144
test('should generate legacy sourcemap file', async () => {
145145
expect(
146-
listAssets().some((filename) => /index-legacy.+\.map$/.test(filename)),
146+
listAssets().some((filename) =>
147+
/index-legacy-[-\w]{8}\.js\.map$/.test(filename),
148+
),
147149
).toBeTruthy()
148150
expect(
149151
listAssets().some((filename) =>
150-
/polyfills-legacy.+\.map$/.test(filename),
152+
/polyfills-legacy-[-\w]{8}\.js\.map$/.test(filename),
151153
),
152-
).toBeFalsy()
154+
).toBeTruthy()
155+
// also for modern polyfills
156+
expect(
157+
listAssets().some((filename) =>
158+
/polyfills-[-\w]{8}\.js\.map$/.test(filename),
159+
),
160+
).toBeTruthy()
153161
})
154162

155163
test('should have only modern entry files guarded', async () => {

0 commit comments

Comments
 (0)