From 945dc4de52e64a1a8f6e2451fadf6aba7e460234 Mon Sep 17 00:00:00 2001 From: lsdsjy Date: Wed, 20 Sep 2023 15:57:16 +0800 Subject: [PATCH] fix(legacy): add guard to modern polyfill chunk (#13719) --- packages/plugin-legacy/src/index.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index ccf0b3ea367535..eb5dbb0752756f 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -237,7 +237,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { `[@vitejs/plugin-legacy] modern polyfills:`, modernPolyfills, ) - await buildPolyfillChunk( + const polyfillChunk = await buildPolyfillChunk( config.mode, modernPolyfills, bundle, @@ -247,6 +247,9 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { opts, true, ) + if (genLegacy && polyfillChunk) { + polyfillChunk.code = modernChunkLegacyGuard + polyfillChunk.code + } return } @@ -734,6 +737,8 @@ async function buildPolyfillChunk( // add the chunk to the bundle bundle[polyfillChunk.fileName] = polyfillChunk + + return polyfillChunk } const polyfillId = '\0vite/legacy-polyfills'