diff --git a/packages/plugin-legacy/README.md b/packages/plugin-legacy/README.md index 956205871d5bd0..63078de2939eee 100644 --- a/packages/plugin-legacy/README.md +++ b/packages/plugin-legacy/README.md @@ -38,11 +38,11 @@ npm add -D terser ### `targets` - **Type:** `string | string[] | { [key: string]: string }` -- **Default:** `undefined` +- **Default:** `'last 2 versions and not dead, > 0.3%, 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 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` diff --git a/packages/plugin-legacy/package.json b/packages/plugin-legacy/package.json index dab5ec1cfea5c6..883713f5fb643e 100644 --- a/packages/plugin-legacy/package.json +++ b/packages/plugin-legacy/package.json @@ -37,6 +37,7 @@ "dependencies": { "@babel/core": "^7.20.5", "@babel/preset-env": "^7.20.2", + "browserslist": "^4.21.4", "core-js": "^3.26.1", "magic-string": "^0.27.0", "regenerator-runtime": "^0.13.11", diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index 609b9052d0bba9..4f8cede3c64e08 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -20,6 +20,7 @@ import type { } from 'rollup' import type { PluginItem as BabelPlugin } from '@babel/core' import colors from 'picocolors' +import browserslist from 'browserslist' import type { Options } from './types' // lazy load babel since it's not used during dev @@ -119,7 +120,10 @@ const _require = createRequire(import.meta.url) function viteLegacyPlugin(options: Options = {}): Plugin[] { let config: ResolvedConfig - const targets = options.targets + const targets = + browserslist() || + options.targets || + 'last 2 versions and not dead, > 0.3%, Firefox ESR' const genLegacy = options.renderLegacyChunks !== false const genDynamicFallback = genLegacy diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7452ea03fe2aef..3f2ebdfa75f0fe 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -144,6 +144,7 @@ importers: specifiers: '@babel/core': ^7.20.5 '@babel/preset-env': ^7.20.2 + browserslist: ^4.21.4 core-js: ^3.26.1 magic-string: ^0.27.0 picocolors: ^1.0.0 @@ -153,6 +154,7 @@ importers: dependencies: '@babel/core': 7.20.5 '@babel/preset-env': 7.20.2_@babel+core@7.20.5 + browserslist: 4.21.4 core-js: 3.26.1 magic-string: 0.27.0 regenerator-runtime: 0.13.11