Skip to content

Commit d5b8f86

Browse files
authored
fix(plugin-legacy)!: support browserslist and update default target (#11318)
Co-authored-by: 翠 / green <[email protected]> fixes #2476
1 parent 88dad65 commit d5b8f86

File tree

4 files changed

+1198
-139
lines changed

4 files changed

+1198
-139
lines changed

packages/plugin-legacy/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ npm add -D terser
3838
### `targets`
3939

4040
- **Type:** `string | string[] | { [key: string]: string }`
41-
- **Default:** `'defaults'`
41+
- **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

4343
If explicitly set, it's passed on to [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env#targets).
4444

45-
The query is also [Browserslist compatible](https://github.com/browserslist/browserslist). The default value, `'defaults'`, is what is recommended by Browserslist. See [Browserslist Best Practices](https://github.com/browserslist/browserslist#best-practices) for more details.
45+
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
### `polyfills`
4848

packages/plugin-legacy/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@
4141
},
4242
"homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-legacy#readme",
4343
"dependencies": {
44-
"@babel/standalone": "^7.20.14",
4544
"core-js": "^3.27.2",
45+
"@babel/core": "^7.20.12",
46+
"@babel/preset-env": "^7.20.2",
47+
"browserslist": "^4.21.4",
4648
"magic-string": "^0.27.0",
4749
"regenerator-runtime": "^0.13.11",
4850
"systemjs": "^6.13.0"
@@ -52,7 +54,6 @@
5254
"vite": "^4.0.0"
5355
},
5456
"devDependencies": {
55-
"@babel/core": "^7.20.12",
5657
"picocolors": "^1.0.0",
5758
"vite": "workspace:*"
5859
}

packages/plugin-legacy/src/index.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ import type {
2323
types as BabelTypes,
2424
} from '@babel/core'
2525
import colors from 'picocolors'
26+
import { loadConfig as browserslistLoadConfig } from 'browserslist'
2627
import type { Options } from './types'
2728

2829
// lazy load babel since it's not used during dev
2930
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
30-
let babel: typeof import('@babel/standalone') | undefined
31+
let babel: typeof import('@babel/core') | undefined
3132
async function loadBabel() {
3233
if (!babel) {
33-
babel = await import('@babel/standalone')
34+
babel = await import('@babel/core')
3435
}
3536
return babel
3637
}
@@ -122,7 +123,8 @@ const _require = createRequire(import.meta.url)
122123

123124
function viteLegacyPlugin(options: Options = {}): Plugin[] {
124125
let config: ResolvedConfig
125-
const targets = options.targets || 'defaults'
126+
let targets: Options['targets']
127+
126128
const genLegacy = options.renderLegacyChunks !== false
127129
const genDynamicFallback = genLegacy
128130

@@ -296,6 +298,12 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
296298
return
297299
}
298300

301+
targets =
302+
options.targets ||
303+
browserslistLoadConfig({ path: config.root }) ||
304+
'last 2 versions and not dead, > 0.3%, Firefox ESR'
305+
isDebug && console.log(`[@vitejs/plugin-legacy] targets:`, targets)
306+
299307
const getLegacyOutputFileName = (
300308
fileNames:
301309
| string
@@ -412,7 +420,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
412420
// transform the legacy chunk with @babel/preset-env
413421
const sourceMaps = !!config.build.sourcemap
414422
const babel = await loadBabel()
415-
const { code, map } = babel.transform(raw, {
423+
const result = babel.transform(raw, {
416424
babelrc: false,
417425
configFile: false,
418426
compact: !!config.build.minify,
@@ -431,7 +439,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
431439
}),
432440
],
433441
[
434-
'env',
442+
'@babel/preset-env',
435443
createBabelPresetEnvOptions(targets, {
436444
needPolyfills,
437445
ignoreBrowserslistConfig: options.ignoreBrowserslistConfig,
@@ -440,7 +448,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
440448
],
441449
})
442450

443-
if (code) return { code, map }
451+
if (result) return { code: result.code!, map: result.map }
444452
return null
445453
},
446454

@@ -595,20 +603,20 @@ export async function detectPolyfills(
595603
list: Set<string>,
596604
): Promise<void> {
597605
const babel = await loadBabel()
598-
const { ast } = babel.transform(code, {
606+
const result = babel.transform(code, {
599607
ast: true,
600608
babelrc: false,
601609
configFile: false,
602610
presets: [
603611
[
604-
'env',
612+
'@babel/preset-env',
605613
createBabelPresetEnvOptions(targets, {
606614
ignoreBrowserslistConfig: true,
607615
}),
608616
],
609617
],
610618
})
611-
for (const node of ast!.program.body) {
619+
for (const node of result!.ast!.program.body) {
612620
if (node.type === 'ImportDeclaration') {
613621
const source = node.source.value
614622
if (

0 commit comments

Comments
 (0)