Skip to content

Commit cf56507

Browse files
undermoonnbluwy
andauthored
feat(plugin-legacy): add modernTargets option (#15506)
Co-authored-by: bluwy <[email protected]>
1 parent bf1e9c2 commit cf56507

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed

packages/plugin-legacy/README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,23 @@ npm add -D terser
4040
- **Type:** `string | string[] | { [key: string]: string }`
4141
- **Default:** [`'last 2 versions and not dead, > 0.3%, Firefox ESR'`](https://browsersl.ist/#q=last+2+versions+and+not+dead%2C+%3E+0.3%25%2C+Firefox+ESR)
4242

43-
If explicitly set, it's passed on to [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env#targets).
43+
If explicitly set, it's passed on to [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env#targets) when rendering **legacy chunks**.
4444

4545
The query is also [Browserslist compatible](https://github.com/browserslist/browserslist). See [Browserslist Best Practices](https://github.com/browserslist/browserslist#best-practices) for more details.
4646

4747
If it's not set, plugin-legacy will load [the browserslist config sources](https://github.com/browserslist/browserslist#queries) and then fallback to the default value.
4848

49+
### `modernTargets`
50+
51+
- **Type:** `string | string[]`
52+
- **Default:** [`'edge>=80, firefox>=72, chrome>=80, safari>=13.1, chromeAndroid>=80, iOS>=13.1'`](https://browsersl.ist/#q=edge%3E%3D80%2C+firefox%3E%3D72%2C+chrome%3E%3D80%2C+safari%3E%3D13.1%2C+chromeAndroid%3E%3D80%2C+iOS%3E%3D13.1)
53+
54+
If explicitly set, it's passed on to [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env#targets) when rendering **modern chunks**.
55+
56+
The query is also [Browserslist compatible](https://github.com/browserslist/browserslist). See [Browserslist Best Practices](https://github.com/browserslist/browserslist#best-practices) for more details.
57+
58+
If it's not set, plugin-legacy will fallback to the default value.
59+
4960
### `polyfills`
5061

5162
- **Type:** `boolean | string[]`

packages/plugin-legacy/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@babel/preset-env": "^7.23.8",
4646
"browserslist": "^4.22.2",
4747
"core-js": "^3.35.0",
48+
"esbuild-plugin-browserslist": "^0.10.0",
4849
"magic-string": "^0.30.5",
4950
"regenerator-runtime": "^0.14.1",
5051
"systemjs": "^6.14.3"

packages/plugin-legacy/src/index.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {
2424
} from '@babel/core'
2525
import colors from 'picocolors'
2626
import browserslist from 'browserslist'
27+
import { resolveToEsbuildTarget } from 'esbuild-plugin-browserslist'
2728
import type { Options } from './types'
2829
import {
2930
detectModernBrowserCode,
@@ -125,6 +126,7 @@ const prefixedHashInFileNameRE = /\W?\[hash(:\d+)?\]/
125126
function viteLegacyPlugin(options: Options = {}): Plugin[] {
126127
let config: ResolvedConfig
127128
let targets: Options['targets']
129+
let modernTargets: Options['modernTargets']
128130

129131
// browsers supporting ESM + dynamic import + import.meta + async generator
130132
const modernTargetsEsbuild = [
@@ -183,6 +185,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
183185
}
184186

185187
let overriddenBuildTarget = false
188+
let overriddenDefaultModernTargets = false
186189
const legacyConfigPlugin: Plugin = {
187190
name: 'vite:legacy-config',
188191

@@ -205,7 +208,10 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
205208
// Vite's default target browsers are **not** the same.
206209
// See https://github.com/vitejs/vite/pull/10052#issuecomment-1242076461
207210
overriddenBuildTarget = config.build.target !== undefined
208-
config.build.target = modernTargetsEsbuild
211+
overriddenDefaultModernTargets = options.modernTargets !== undefined
212+
config.build.target = options.modernTargets
213+
? resolveToEsbuildTarget(browserslist(options.modernTargets))
214+
: modernTargetsEsbuild
209215
}
210216
}
211217

@@ -226,6 +232,13 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
226232
),
227233
)
228234
}
235+
if (overriddenDefaultModernTargets) {
236+
config.logger.warn(
237+
colors.yellow(
238+
`plugin-legacy 'modernTargets' option overrode the builtin targets of modern chunks. Some versions of browsers between legacy and modern may not be supported.`,
239+
),
240+
)
241+
}
229242
},
230243
}
231244

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

338+
modernTargets = options.modernTargets || modernTargetsBabel
339+
isDebug &&
340+
console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets)
341+
325342
const getLegacyOutputFileName = (
326343
fileNames:
327344
| string
@@ -394,7 +411,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
394411
genModern
395412
) {
396413
// analyze and record modern polyfills
397-
await detectPolyfills(raw, modernTargetsBabel, modernPolyfills)
414+
await detectPolyfills(raw, modernTargets, modernPolyfills)
398415
}
399416

400417
const ms = new MagicString(raw)

packages/plugin-legacy/src/types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ export interface Options {
33
* default: 'defaults'
44
*/
55
targets?: string | string[] | { [key: string]: string }
6+
/**
7+
* default: 'edge>=80, firefox>=72, chrome>=80, safari>=13.1, chromeAndroid>=80, iOS>=13.1'
8+
*/
9+
modernTargets?: string | string[]
610
/**
711
* default: true
812
*/

pnpm-lock.yaml

+17-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)