From 1c2e941d03621a4b77d9dfca8841e336b716691c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Sat, 23 Sep 2023 00:03:12 +0900 Subject: [PATCH] fix(legacy): modern polyfill autodetection was injecting more polyfills than needed (#14428) --- packages/plugin-legacy/src/index.ts | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index 4fcdb2c11ddc16..c07df1da56740a 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -123,6 +123,20 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { let config: ResolvedConfig let targets: Options['targets'] + // browsers supporting ESM + dynamic import + import.meta + async generator + const modernTargetsEsbuild = [ + 'es2020', + 'edge79', + 'firefox67', + 'chrome64', + 'safari12', + ] + // same with above but by browserslist syntax + // es2020 = chrome 80+, safari 13.1+, firefox 72+, edge 80+ + // https://github.com/evanw/esbuild/issues/121#issuecomment-646956379 + const modernTargetsBabel = + 'edge>=80, firefox>=72, chrome>=80, safari>=13.1, chromeAndroid>=80, iOS>=13.1' + const genLegacy = options.renderLegacyChunks !== false const genModern = options.renderModernChunks !== false if (!genLegacy && !genModern) { @@ -188,14 +202,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { // Vite's default target browsers are **not** the same. // See https://github.com/vitejs/vite/pull/10052#issuecomment-1242076461 overriddenBuildTarget = config.build.target !== undefined - // browsers supporting ESM + dynamic import + import.meta + async generator - config.build.target = [ - 'es2020', - 'edge79', - 'firefox67', - 'chrome64', - 'safari12', - ] + config.build.target = modernTargetsEsbuild } } @@ -375,7 +382,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { genModern ) { // analyze and record modern polyfills - await detectPolyfills(raw, { esmodules: true }, modernPolyfills) + await detectPolyfills(raw, modernTargetsBabel, modernPolyfills) } const ms = new MagicString(raw)