Skip to content

Commit

Permalink
fix(legacy): generate sourcemap for polyfill chunks (#18250)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Oct 4, 2024
1 parent a03bb0e commit f311ff3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
55 changes: 46 additions & 9 deletions packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import type {
} from 'vite'
import type {
NormalizedOutputOptions,
OutputAsset,
OutputBundle,
OutputChunk,
OutputOptions,
PreRenderedChunk,
RenderedChunk,
Expand Down Expand Up @@ -302,7 +304,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
modernPolyfills,
)
}
const polyfillChunk = await buildPolyfillChunk(
await buildPolyfillChunk(
config.mode,
modernPolyfills,
bundle,
Expand All @@ -311,10 +313,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
'es',
opts,
true,
genLegacy,
)
if (genLegacy && polyfillChunk) {
polyfillChunk.code = modernChunkLegacyGuard + polyfillChunk.code
}
return
}

Expand Down Expand Up @@ -789,20 +789,25 @@ async function buildPolyfillChunk(
format: 'iife' | 'es',
rollupOutputOptions: NormalizedOutputOptions,
excludeSystemJS?: boolean,
prependModenChunkLegacyGuard?: boolean,
) {
let { minify, assetsDir } = buildOptions
let { minify, assetsDir, sourcemap } = buildOptions
minify = minify ? 'terser' : false
const res = await build({
mode,
// so that everything is resolved from here
root: path.dirname(fileURLToPath(import.meta.url)),
configFile: false,
logLevel: 'error',
plugins: [polyfillsPlugin(imports, excludeSystemJS)],
plugins: [
polyfillsPlugin(imports, excludeSystemJS),
prependModenChunkLegacyGuard && prependModenChunkLegacyGuardPlugin(),
],
build: {
write: false,
minify,
assetsDir,
sourcemap,
rollupOptions: {
input: {
polyfills: polyfillId,
Expand All @@ -828,7 +833,9 @@ async function buildPolyfillChunk(
})
const _polyfillChunk = Array.isArray(res) ? res[0] : res
if (!('output' in _polyfillChunk)) return
const polyfillChunk = _polyfillChunk.output[0]
const polyfillChunk = _polyfillChunk.output.find(
(chunk) => chunk.type === 'chunk' && chunk.isEntry,
) as OutputChunk

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

// add the chunk to the bundle
bundle[polyfillChunk.fileName] = polyfillChunk

return polyfillChunk
if (polyfillChunk.sourcemapFileName) {
const polyfillChunkMapAsset = _polyfillChunk.output.find(
(chunk) =>
chunk.type === 'asset' &&
chunk.fileName === polyfillChunk.sourcemapFileName,
) as OutputAsset | undefined
if (polyfillChunkMapAsset) {
bundle[polyfillChunk.sourcemapFileName] = polyfillChunkMapAsset
}
}
}

const polyfillId = '\0vite/legacy-polyfills'
Expand All @@ -869,6 +884,28 @@ function polyfillsPlugin(
}
}

function prependModenChunkLegacyGuardPlugin(): Plugin {
let sourceMapEnabled!: boolean
return {
name: 'vite:legacy-prepend-moden-chunk-legacy-guard',
configResolved(config) {
sourceMapEnabled = !!config.build.sourcemap
},
renderChunk(code) {
if (!sourceMapEnabled) {
return modernChunkLegacyGuard + code
}

const ms = new MagicString(code)
ms.prepend(modernChunkLegacyGuard)
return {
code: ms.toString(),
map: ms.generateMap({ hires: 'boundary' }),
}
},
}
}

function isLegacyChunk(chunk: RenderedChunk, options: NormalizedOutputOptions) {
return options.format === 'system' && chunk.fileName.includes('-legacy')
}
Expand Down
14 changes: 11 additions & 3 deletions playground/legacy/__tests__/legacy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,21 @@ describe.runIf(isBuild)('build', () => {

test('should generate legacy sourcemap file', async () => {
expect(
listAssets().some((filename) => /index-legacy.+\.map$/.test(filename)),
listAssets().some((filename) =>
/index-legacy-[-\w]{8}\.js\.map$/.test(filename),
),
).toBeTruthy()
expect(
listAssets().some((filename) =>
/polyfills-legacy.+\.map$/.test(filename),
/polyfills-legacy-[-\w]{8}\.js\.map$/.test(filename),
),
).toBeFalsy()
).toBeTruthy()
// also for modern polyfills
expect(
listAssets().some((filename) =>
/polyfills-[-\w]{8}\.js\.map$/.test(filename),
),
).toBeTruthy()
})

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

0 comments on commit f311ff3

Please sign in to comment.