Skip to content

Commit c735cc7

Browse files
authored
fix(plugin-legacy): group discovered polyfills by output (#17347)
1 parent 4161843 commit c735cc7

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

packages/plugin-legacy/src/index.ts

+28-15
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,11 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
159159
const modernPolyfills = new Set<string>()
160160
const legacyPolyfills = new Set<string>()
161161
// When discovering polyfills in `renderChunk`, the hook may be non-deterministic, so we group the
162-
// modern and legacy polyfills in a sorted map before merging them.
163-
let chunkFileNameToPolyfills:
164-
| Map<string, { modern: Set<string>; legacy: Set<string> }>
165-
| undefined
162+
// modern and legacy polyfills in a sorted chunks map for each rendered outputs before merging them.
163+
const outputToChunkFileNameToPolyfills = new WeakMap<
164+
NormalizedOutputOptions,
165+
Map<string, { modern: Set<string>; legacy: Set<string> }> | null
166+
>()
166167

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

271+
const chunkFileNameToPolyfills =
272+
outputToChunkFileNameToPolyfills.get(opts)
273+
if (chunkFileNameToPolyfills == null) {
274+
throw new Error(
275+
'Internal @vitejs/plugin-legacy error: discovered polyfills should exist',
276+
)
277+
}
278+
270279
if (!isLegacyBundle(bundle, opts)) {
271280
// Merge discovered modern polyfills to `modernPolyfills`
272-
if (chunkFileNameToPolyfills) {
273-
for (const { modern } of chunkFileNameToPolyfills.values()) {
274-
modern.forEach((p) => modernPolyfills.add(p))
275-
}
281+
for (const { modern } of chunkFileNameToPolyfills.values()) {
282+
modern.forEach((p) => modernPolyfills.add(p))
276283
}
277284
if (!modernPolyfills.size) {
278285
return
@@ -303,10 +310,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
303310
}
304311

305312
// Merge discovered legacy polyfills to `legacyPolyfills`
306-
if (chunkFileNameToPolyfills) {
307-
for (const { legacy } of chunkFileNameToPolyfills.values()) {
308-
legacy.forEach((p) => legacyPolyfills.add(p))
309-
}
313+
for (const { legacy } of chunkFileNameToPolyfills.values()) {
314+
legacy.forEach((p) => legacyPolyfills.add(p))
310315
}
311316

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

351-
renderStart() {
352-
chunkFileNameToPolyfills = undefined
356+
renderStart(opts) {
357+
// Empty the nested map for this output
358+
outputToChunkFileNameToPolyfills.set(opts, null)
353359
},
354360

355361
configResolved(_config) {
@@ -438,6 +444,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
438444
}
439445

440446
// On first run, intialize the map with sorted chunk file names
447+
let chunkFileNameToPolyfills = outputToChunkFileNameToPolyfills.get(opts)
441448
if (chunkFileNameToPolyfills == null) {
442449
chunkFileNameToPolyfills = new Map()
443450
for (const fileName in chunks) {
@@ -446,8 +453,14 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
446453
legacy: new Set(),
447454
})
448455
}
456+
outputToChunkFileNameToPolyfills.set(opts, chunkFileNameToPolyfills)
457+
}
458+
const polyfillsDiscovered = chunkFileNameToPolyfills.get(chunk.fileName)
459+
if (polyfillsDiscovered == null) {
460+
throw new Error(
461+
`Internal @vitejs/plugin-legacy error: discovered polyfills for ${chunk.fileName} should exist`,
462+
)
449463
}
450-
const polyfillsDiscovered = chunkFileNameToPolyfills.get(chunk.fileName)!
451464

452465
if (!isLegacyChunk(chunk, opts)) {
453466
if (

0 commit comments

Comments
 (0)