Skip to content

Commit a13a7eb

Browse files
authored
fix(plugin-legacy): force set build.target (#10072)
1 parent 414d2ef commit a13a7eb

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

packages/plugin-legacy/build.config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default defineBuildConfig({
55
clean: true,
66
declaration: true,
77
rollup: {
8-
emitCJS: true
8+
emitCJS: true,
9+
inlineDependencies: true
910
}
1011
})

packages/plugin-legacy/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
},
4848
"devDependencies": {
4949
"@babel/core": "^7.19.0",
50+
"picocolors": "^1.0.0",
5051
"vite": "workspace:*"
5152
}
5253
}

packages/plugin-legacy/src/index.ts

+23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
RenderedChunk
2020
} from 'rollup'
2121
import type { PluginItem as BabelPlugin } from '@babel/core'
22+
import colors from 'picocolors'
2223
import type { Options } from './types'
2324

2425
// lazy load babel since it's not used during dev
@@ -156,6 +157,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
156157
})
157158
}
158159

160+
let overriddenBuildTarget = false
159161
const legacyConfigPlugin: Plugin = {
160162
name: 'vite:legacy-config',
161163

@@ -173,6 +175,18 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
173175
// So targeting `chrome61` suffices to fix the compatibility issue.
174176
config.build.cssTarget = 'chrome61'
175177
}
178+
179+
// Vite's default target browsers are **not** the same.
180+
// See https://github.com/vitejs/vite/pull/10052#issuecomment-1242076461
181+
overriddenBuildTarget = config.build.target !== undefined
182+
// browsers supporting ESM + dynamic import + import.meta
183+
config.build.target = [
184+
'es2020',
185+
'edge79',
186+
'firefox67',
187+
'chrome64',
188+
'safari11.1'
189+
]
176190
}
177191

178192
return {
@@ -183,6 +197,15 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
183197
: legacyEnvVarMarker
184198
}
185199
}
200+
},
201+
configResolved(config) {
202+
if (overriddenBuildTarget) {
203+
config.logger.warn(
204+
colors.yellow(
205+
`plugin-legacy overrode 'build.target'. You should pass 'targets' as an option to this plugin with the list of legacy browsers to support instead.`
206+
)
207+
)
208+
}
186209
}
187210
}
188211

pnpm-lock.yaml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)