Skip to content

Commit

Permalink
fix(legacy): modern polyfill autodetection was injecting more polyfil…
Browse files Browse the repository at this point in the history
…ls than needed (#14428)
  • Loading branch information
sapphi-red committed Sep 22, 2023
1 parent aae3a83 commit 1c2e941
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1c2e941

Please sign in to comment.