Skip to content

Commit

Permalink
fix: always read targets options (fix: #15788)
Browse files Browse the repository at this point in the history
If `modernPolyfills` is set to `true`, but `renderLegacyChunks` is set
to false, `genLegacy` will be false and we would return early. If this
return happens before `modernTargets` is initialized, the modern
polyfills will be generated with the wrong (default?) target instead of
the specified `modernTargets`.

Also, config.build.target must be set from the `modernTargets`, even if
`genLegacy` is to set.
  • Loading branch information
jgosmann committed Feb 3, 2024
1 parent 775bb50 commit 2e80e8a
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,13 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
config.build.cssTarget = 'chrome61'
}

if (genLegacy) {
// Vite's default target browsers are **not** the same.
// See https://github.com/vitejs/vite/pull/10052#issuecomment-1242076461
overriddenBuildTarget = config.build.target !== undefined
overriddenDefaultModernTargets = options.modernTargets !== undefined
config.build.target = options.modernTargets
? resolveToEsbuildTarget(browserslist(options.modernTargets))
: modernTargetsEsbuild
}
// Vite's default target browsers are **not** the same.
// See https://github.com/vitejs/vite/pull/10052#issuecomment-1242076461
overriddenBuildTarget = config.build.target !== undefined
overriddenDefaultModernTargets = options.modernTargets !== undefined
config.build.target = options.modernTargets
? resolveToEsbuildTarget(browserslist(options.modernTargets))
: modernTargetsEsbuild
}

return {
Expand Down Expand Up @@ -325,10 +323,6 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
}
config = _config

if (!genLegacy || config.build.ssr) {
return
}

targets =
options.targets ||
browserslistLoadConfig({ path: config.root }) ||
Expand All @@ -339,6 +333,10 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
isDebug &&
console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets)

if (!genLegacy || config.build.ssr) {
return
}

const getLegacyOutputFileName = (
fileNames:
| string
Expand Down

0 comments on commit 2e80e8a

Please sign in to comment.