Skip to content

Commit 76c5e40

Browse files
authored
fix(legacy): modernTargets should set build.target (vitejs#20393)
1 parent 3f7598c commit 76c5e40

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

packages/plugin-legacy/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ 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) when rendering **legacy chunks**.
43+
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

@@ -51,12 +51,14 @@ npm add -D terser
5151
- **Type:** `string | string[]`
5252
- **Default:** [`'edge>=79, firefox>=67, chrome>=64, safari>=12, chromeAndroid>=64, iOS>=12'`](https://browsersl.ist/#q=edge%3E%3D79%2C+firefox%3E%3D67%2C+chrome%3E%3D64%2C+safari%3E%3D12%2C+chromeAndroid%3E%3D64%2C+iOS%3E%3D12)
5353

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**.
54+
It's passed on to [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env#targets) when collecting polyfills for **modern chunks**. The value set here will override the `build.target` option.
5555

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

5858
If it's not set, plugin-legacy will fallback to the default value.
5959

60+
Note that this options should not be set unless `renderLegacyChunks` is set to `false`.
61+
6062
### `polyfills`
6163

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

packages/plugin-legacy/src/index.ts

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ const _require = createRequire(import.meta.url)
125125
const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/
126126
const prefixedHashInFileNameRE = /\W?\[hash(?::\d+)?\]/
127127

128+
// browsers supporting ESM + dynamic import + import.meta + async generator
129+
const modernTargetsEsbuild = [
130+
'es2020',
131+
'edge79',
132+
'firefox67',
133+
'chrome64',
134+
'safari12',
135+
]
136+
// same with above but by browserslist syntax
137+
// es2020 = chrome 80+, safari 13.1+, firefox 72+, edge 80+
138+
// https://github.com/evanw/esbuild/issues/121#issuecomment-646956379
139+
const modernTargetsBabel =
140+
'edge>=79, firefox>=67, chrome>=64, safari>=12, chromeAndroid>=64, iOS>=12'
141+
128142
function viteLegacyPlugin(options: Options = {}): Plugin[] {
129143
if ('rolldownVersion' in vite) {
130144
const { default: viteLegacyPluginForRolldownVite } = _require(
@@ -135,21 +149,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
135149

136150
let config: ResolvedConfig
137151
let targets: Options['targets']
138-
let modernTargets: Options['modernTargets']
139-
140-
// browsers supporting ESM + dynamic import + import.meta + async generator
141-
const modernTargetsEsbuild = [
142-
'es2020',
143-
'edge79',
144-
'firefox67',
145-
'chrome64',
146-
'safari12',
147-
]
148-
// same with above but by browserslist syntax
149-
// es2020 = chrome 80+, safari 13.1+, firefox 72+, edge 80+
150-
// https://github.com/evanw/esbuild/issues/121#issuecomment-646956379
151-
const modernTargetsBabel =
152-
'edge>=79, firefox>=67, chrome>=64, safari>=12, chromeAndroid>=64, iOS>=12'
152+
const modernTargets: Options['modernTargets'] =
153+
options.modernTargets || modernTargetsBabel
153154

154155
const genLegacy = options.renderLegacyChunks !== false
155156
const genModern = options.renderModernChunks !== false
@@ -207,6 +208,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
207208
}
208209

209210
let overriddenBuildTarget = false
211+
let overriddenBuildTargetOnlyModern = false
210212
let overriddenDefaultModernTargets = false
211213
const legacyConfigPlugin: Plugin = {
212214
name: 'vite:legacy-config',
@@ -231,16 +233,18 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
231233
// See https://github.com/vitejs/vite/pull/10052#issuecomment-1242076461
232234
overriddenBuildTarget = config.build.target !== undefined
233235
overriddenDefaultModernTargets = options.modernTargets !== undefined
236+
} else {
237+
overriddenBuildTargetOnlyModern = config.build.target !== undefined
238+
}
234239

235-
if (options.modernTargets) {
236-
// Package is ESM only
237-
const { default: browserslistToEsbuild } = await import(
238-
'browserslist-to-esbuild'
239-
)
240-
config.build.target = browserslistToEsbuild(options.modernTargets)
241-
} else {
242-
config.build.target = modernTargetsEsbuild
243-
}
240+
if (options.modernTargets) {
241+
// Package is ESM only
242+
const { default: browserslistToEsbuild } = await import(
243+
'browserslist-to-esbuild'
244+
)
245+
config.build.target = browserslistToEsbuild(options.modernTargets)
246+
} else {
247+
config.build.target = modernTargetsEsbuild
244248
}
245249
}
246250

@@ -261,6 +265,13 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
261265
),
262266
)
263267
}
268+
if (overriddenBuildTargetOnlyModern) {
269+
config.logger.warn(
270+
colors.yellow(
271+
`plugin-legacy overrode 'build.target'. You should pass 'modernTargets' as an option to this plugin with the list of browsers to support instead.`,
272+
),
273+
)
274+
}
264275
if (overriddenDefaultModernTargets) {
265276
config.logger.warn(
266277
colors.yellow(
@@ -384,7 +395,6 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
384395
}
385396
config = _config
386397

387-
modernTargets = options.modernTargets || modernTargetsBabel
388398
if (isDebug) {
389399
console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets)
390400
}

0 commit comments

Comments
 (0)