From 36ca4f2eaea5d7e9ac24e728e1a86304a4cc83a5 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 21 Sep 2023 18:06:01 +0900 Subject: [PATCH] fix(legacy): modern polyfill autodetection was injecting more polyfills than needed --- 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 eb5dbb0752756f..5fa8c10a65a7a2 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)