Skip to content

Commit 6f77b2b

Browse files
authored
fix(esbuild): preserve import.meta even if esbuild.target is set to lower versions (#16151)
1 parent 8cd846c commit 6f77b2b

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

packages/vite/src/node/optimizer/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ import {
2222
tryStatSync,
2323
unique,
2424
} from '../utils'
25-
import { transformWithEsbuild } from '../plugins/esbuild'
25+
import {
26+
defaultEsbuildSupported,
27+
transformWithEsbuild,
28+
} from '../plugins/esbuild'
2629
import { ESBUILD_MODULES_TARGET, METADATA_FILENAME } from '../constants'
2730
import { isWindows } from '../../shared/utils'
2831
import { esbuildCjsExternalPlugin, esbuildDepPlugin } from './esbuildDepPlugin'
@@ -799,8 +802,7 @@ async function prepareEsbuildOptimizerRun(
799802
charset: 'utf8',
800803
...esbuildOptions,
801804
supported: {
802-
'dynamic-import': true,
803-
'import-meta': true,
805+
...defaultEsbuildSupported,
804806
...esbuildOptions.supported,
805807
},
806808
})

packages/vite/src/node/plugins/esbuild.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ const IIFE_BEGIN_RE =
3333
const validExtensionRE = /\.\w+$/
3434
const jsxExtensionsRE = /\.(?:j|t)sx\b/
3535

36+
// the final build should always support dynamic import and import.meta.
37+
// if they need to be polyfilled, plugin-legacy should be used.
38+
// plugin-legacy detects these two features when checking for modern code.
39+
export const defaultEsbuildSupported = {
40+
'dynamic-import': true,
41+
'import-meta': true,
42+
}
43+
3644
let server: ViteDevServer
3745

3846
export interface ESBuildOptions extends TransformOptions {
@@ -235,6 +243,10 @@ export function esbuildPlugin(config: ResolvedConfig): Plugin {
235243
// Also transforming multiple times with keepNames enabled breaks
236244
// tree-shaking. (#9164)
237245
keepNames: false,
246+
supported: {
247+
...defaultEsbuildSupported,
248+
...esbuildTransformOptions.supported,
249+
},
238250
}
239251

240252
return {
@@ -360,12 +372,8 @@ export function resolveEsbuildTranspileOptions(
360372
loader: 'js',
361373
target: target || undefined,
362374
format: rollupToEsbuildFormatMap[format],
363-
// the final build should always support dynamic import and import.meta.
364-
// if they need to be polyfilled, plugin-legacy should be used.
365-
// plugin-legacy detects these two features when checking for modern code.
366375
supported: {
367-
'dynamic-import': true,
368-
'import-meta': true,
376+
...defaultEsbuildSupported,
369377
...esbuildOptions.supported,
370378
},
371379
}

0 commit comments

Comments
 (0)