From 6236846416923ec8a30b908a247052564bcd5e43 Mon Sep 17 00:00:00 2001 From: bluwy Date: Thu, 30 May 2024 00:43:09 +0800 Subject: [PATCH 1/2] fix(plugin-legacy): group discovered polyfills by output --- packages/plugin-legacy/src/index.ts | 43 +++++++++++++++++++---------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index 599b9749cbae3a..99f01f705a4134 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -159,10 +159,11 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { const modernPolyfills = new Set() const legacyPolyfills = new Set() // 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; legacy: Set }> - | undefined + // modern and legacy polyfills in a sorted chunks map for each rendered outputs before merging them. + const outputToChunkFileNameToPolyfills = new Map< + NormalizedOutputOptions, + Map; legacy: Set }> | null + >() if (Array.isArray(options.modernPolyfills) && genModern) { options.modernPolyfills.forEach((i) => { @@ -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 @@ -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 @@ -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) { @@ -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) { @@ -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 ( From 2570a33bed7e26cd4f5964084a5cf8cc0a579338 Mon Sep 17 00:00:00 2001 From: bluwy Date: Thu, 30 May 2024 14:30:29 +0800 Subject: [PATCH 2/2] refactor: use WeakMap --- packages/plugin-legacy/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index 99f01f705a4134..2fabb042a3cefc 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -160,7 +160,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { const legacyPolyfills = new Set() // When discovering polyfills in `renderChunk`, the hook may be non-deterministic, so we group the // modern and legacy polyfills in a sorted chunks map for each rendered outputs before merging them. - const outputToChunkFileNameToPolyfills = new Map< + const outputToChunkFileNameToPolyfills = new WeakMap< NormalizedOutputOptions, Map; legacy: Set }> | null >()