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

fix(plugin-legacy)!: support browserslist and update default target #11318

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/plugin-legacy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ npm add -D terser
### `targets`

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

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

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.
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.

### `polyfills`

Expand Down
5 changes: 3 additions & 2 deletions packages/plugin-legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
},
"homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-legacy#readme",
"dependencies": {
"@babel/standalone": "^7.20.14",
"core-js": "^3.27.2",
"@babel/core": "^7.20.12",
"@babel/preset-env": "^7.20.2",
"browserslist": "^4.21.4",
"magic-string": "^0.27.0",
"regenerator-runtime": "^0.13.11",
"systemjs": "^6.13.0"
Expand All @@ -52,7 +54,6 @@
"vite": "^4.0.0"
},
"devDependencies": {
"@babel/core": "^7.20.12",
"picocolors": "^1.0.0",
"vite": "workspace:*"
}
Expand Down
26 changes: 17 additions & 9 deletions packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import type {
types as BabelTypes,
} from '@babel/core'
import colors from 'picocolors'
import { loadConfig as browserslistLoadConfig } from 'browserslist'
import type { Options } from './types'

// lazy load babel since it's not used during dev
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
let babel: typeof import('@babel/standalone') | undefined
let babel: typeof import('@babel/core') | undefined
async function loadBabel() {
if (!babel) {
babel = await import('@babel/standalone')
babel = await import('@babel/core')
}
return babel
}
Expand Down Expand Up @@ -122,7 +123,8 @@ const _require = createRequire(import.meta.url)

function viteLegacyPlugin(options: Options = {}): Plugin[] {
let config: ResolvedConfig
const targets = options.targets || 'defaults'
let targets: Options['targets']

const genLegacy = options.renderLegacyChunks !== false
const genDynamicFallback = genLegacy

Expand Down Expand Up @@ -296,6 +298,12 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
return
}

targets =
options.targets ||
browserslistLoadConfig({ path: config.root }) ||
'last 2 versions and not dead, > 0.3%, Firefox ESR'
isDebug && console.log(`[@vitejs/plugin-legacy] targets:`, targets)

const getLegacyOutputFileName = (
fileNames:
| string
Expand Down Expand Up @@ -412,7 +420,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
// transform the legacy chunk with @babel/preset-env
const sourceMaps = !!config.build.sourcemap
const babel = await loadBabel()
const { code, map } = babel.transform(raw, {
const result = babel.transform(raw, {
babelrc: false,
configFile: false,
compact: !!config.build.minify,
Expand All @@ -431,7 +439,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
}),
],
[
'env',
'@babel/preset-env',
createBabelPresetEnvOptions(targets, {
needPolyfills,
ignoreBrowserslistConfig: options.ignoreBrowserslistConfig,
Expand All @@ -440,7 +448,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
],
})

if (code) return { code, map }
if (result) return { code: result.code!, map: result.map }
return null
},

Expand Down Expand Up @@ -595,20 +603,20 @@ export async function detectPolyfills(
list: Set<string>,
): Promise<void> {
const babel = await loadBabel()
const { ast } = babel.transform(code, {
const result = babel.transform(code, {
ast: true,
babelrc: false,
configFile: false,
presets: [
[
'env',
'@babel/preset-env',
createBabelPresetEnvOptions(targets, {
ignoreBrowserslistConfig: true,
}),
],
],
})
for (const node of ast!.program.body) {
for (const node of result!.ast!.program.body) {
if (node.type === 'ImportDeclaration') {
const source = node.source.value
if (
Expand Down
Loading