Skip to content

Commit 1c2e941

Browse files
authored
fix(legacy): modern polyfill autodetection was injecting more polyfills than needed (#14428)
1 parent aae3a83 commit 1c2e941

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

packages/plugin-legacy/src/index.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,20 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
123123
let config: ResolvedConfig
124124
let targets: Options['targets']
125125

126+
// browsers supporting ESM + dynamic import + import.meta + async generator
127+
const modernTargetsEsbuild = [
128+
'es2020',
129+
'edge79',
130+
'firefox67',
131+
'chrome64',
132+
'safari12',
133+
]
134+
// same with above but by browserslist syntax
135+
// es2020 = chrome 80+, safari 13.1+, firefox 72+, edge 80+
136+
// https://github.com/evanw/esbuild/issues/121#issuecomment-646956379
137+
const modernTargetsBabel =
138+
'edge>=80, firefox>=72, chrome>=80, safari>=13.1, chromeAndroid>=80, iOS>=13.1'
139+
126140
const genLegacy = options.renderLegacyChunks !== false
127141
const genModern = options.renderModernChunks !== false
128142
if (!genLegacy && !genModern) {
@@ -188,14 +202,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
188202
// Vite's default target browsers are **not** the same.
189203
// See https://github.com/vitejs/vite/pull/10052#issuecomment-1242076461
190204
overriddenBuildTarget = config.build.target !== undefined
191-
// browsers supporting ESM + dynamic import + import.meta + async generator
192-
config.build.target = [
193-
'es2020',
194-
'edge79',
195-
'firefox67',
196-
'chrome64',
197-
'safari12',
198-
]
205+
config.build.target = modernTargetsEsbuild
199206
}
200207
}
201208

@@ -375,7 +382,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
375382
genModern
376383
) {
377384
// analyze and record modern polyfills
378-
await detectPolyfills(raw, { esmodules: true }, modernPolyfills)
385+
await detectPolyfills(raw, modernTargetsBabel, modernPolyfills)
379386
}
380387

381388
const ms = new MagicString(raw)

0 commit comments

Comments
 (0)