Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-legacy): group discovered polyfills by output #17347

Merged
merged 2 commits into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,11 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
const modernPolyfills = new Set<string>()
const legacyPolyfills = new Set<string>()
// When discovering polyfills in `renderChunk`, the hook may be non-deterministic, so we group the
// modern and legacy polyfills in a sorted map before merging them.
let chunkFileNameToPolyfills:
| Map<string, { modern: Set<string>; legacy: Set<string> }>
| undefined
// modern and legacy polyfills in a sorted chunks map for each rendered outputs before merging them.
const outputToChunkFileNameToPolyfills = new WeakMap<
NormalizedOutputOptions,
Map<string, { modern: Set<string>; legacy: Set<string> }> | null
>()

if (Array.isArray(options.modernPolyfills) && genModern) {
options.modernPolyfills.forEach((i) => {
Expand Down Expand Up @@ -267,12 +268,18 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
return
}

const chunkFileNameToPolyfills =
outputToChunkFileNameToPolyfills.get(opts)
if (chunkFileNameToPolyfills == null) {
throw new Error(
'Internal @vitejs/plugin-legacy error: discovered polyfills should exist',
)
}

if (!isLegacyBundle(bundle, opts)) {
// Merge discovered modern polyfills to `modernPolyfills`
if (chunkFileNameToPolyfills) {
for (const { modern } of chunkFileNameToPolyfills.values()) {
modern.forEach((p) => modernPolyfills.add(p))
}
for (const { modern } of chunkFileNameToPolyfills.values()) {
modern.forEach((p) => modernPolyfills.add(p))
}
if (!modernPolyfills.size) {
return
Expand Down Expand Up @@ -303,10 +310,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
}

// Merge discovered legacy polyfills to `legacyPolyfills`
if (chunkFileNameToPolyfills) {
for (const { legacy } of chunkFileNameToPolyfills.values()) {
legacy.forEach((p) => legacyPolyfills.add(p))
}
for (const { legacy } of chunkFileNameToPolyfills.values()) {
legacy.forEach((p) => legacyPolyfills.add(p))
}

// legacy bundle
Expand Down Expand Up @@ -348,8 +353,9 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
enforce: 'post',
apply: 'build',

renderStart() {
chunkFileNameToPolyfills = undefined
renderStart(opts) {
// Empty the nested map for this output
outputToChunkFileNameToPolyfills.set(opts, null)
},

configResolved(_config) {
Expand Down Expand Up @@ -438,6 +444,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
}

// On first run, intialize the map with sorted chunk file names
let chunkFileNameToPolyfills = outputToChunkFileNameToPolyfills.get(opts)
if (chunkFileNameToPolyfills == null) {
chunkFileNameToPolyfills = new Map()
for (const fileName in chunks) {
Expand All @@ -446,8 +453,14 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
legacy: new Set(),
})
}
outputToChunkFileNameToPolyfills.set(opts, chunkFileNameToPolyfills)
}
const polyfillsDiscovered = chunkFileNameToPolyfills.get(chunk.fileName)
if (polyfillsDiscovered == null) {
throw new Error(
`Internal @vitejs/plugin-legacy error: discovered polyfills for ${chunk.fileName} should exist`,
)
}
const polyfillsDiscovered = chunkFileNameToPolyfills.get(chunk.fileName)!

if (!isLegacyChunk(chunk, opts)) {
if (
Expand Down