Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin-legacy): add modernTargets option #15506

Merged
merged 8 commits into from
Jan 17, 2024
Merged
1 change: 1 addition & 0 deletions packages/plugin-legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
},
"devDependencies": {
"acorn": "^8.11.2",
"esbuild-plugin-browserslist": "^0.10.0",
bluwy marked this conversation as resolved.
Show resolved Hide resolved
"picocolors": "^1.0.0",
"vite": "workspace:*"
}
Expand Down
12 changes: 10 additions & 2 deletions packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
} from '@babel/core'
import colors from 'picocolors'
import browserslist from 'browserslist'
import { resolveToEsbuildTarget } from 'esbuild-plugin-browserslist'
import type { Options } from './types'
import {
detectModernBrowserCode,
Expand Down Expand Up @@ -125,6 +126,7 @@ const prefixedHashInFileNameRE = /\W?\[hash(:\d+)?\]/
function viteLegacyPlugin(options: Options = {}): Plugin[] {
let config: ResolvedConfig
let targets: Options['targets']
let modernTargets: Options['modernTargets']

// browsers supporting ESM + dynamic import + import.meta + async generator
const modernTargetsEsbuild = [
Expand Down Expand Up @@ -205,7 +207,9 @@ 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
config.build.target = modernTargetsEsbuild
config.build.target = options.modernTargets
? resolveToEsbuildTarget(browserslist(options.modernTargets))
: modernTargetsEsbuild
}
}

Expand Down Expand Up @@ -322,6 +326,10 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
'last 2 versions and not dead, > 0.3%, Firefox ESR'
isDebug && console.log(`[@vitejs/plugin-legacy] targets:`, targets)

modernTargets = options.modernTargets || modernTargetsBabel
isDebug &&
console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets)

const getLegacyOutputFileName = (
fileNames:
| string
Expand Down Expand Up @@ -394,7 +402,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
genModern
) {
// analyze and record modern polyfills
await detectPolyfills(raw, modernTargetsBabel, modernPolyfills)
await detectPolyfills(raw, modernTargets, modernPolyfills)
}

const ms = new MagicString(raw)
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin-legacy/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export interface Options {
* default: 'defaults'
*/
targets?: string | string[] | { [key: string]: string }
/**
* default: 'edge>=80, firefox>=72, chrome>=80, safari>=13.1, chromeAndroid>=80, iOS>=13.1'
*/
modernTargets?: string | string[]
/**
* default: true
*/
Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.