Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(esbuild): preserve import.meta even if esbuild.target is set to lower versions #16151

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import {
tryStatSync,
unique,
} from '../utils'
import { transformWithEsbuild } from '../plugins/esbuild'
import {
defaultEsbuildSupported,
transformWithEsbuild,
} from '../plugins/esbuild'
import { ESBUILD_MODULES_TARGET, METADATA_FILENAME } from '../constants'
import { isWindows } from '../../shared/utils'
import { esbuildCjsExternalPlugin, esbuildDepPlugin } from './esbuildDepPlugin'
Expand Down Expand Up @@ -799,8 +802,7 @@ async function prepareEsbuildOptimizerRun(
charset: 'utf8',
...esbuildOptions,
supported: {
'dynamic-import': true,
'import-meta': true,
...defaultEsbuildSupported,
...esbuildOptions.supported,
},
})
Expand Down
18 changes: 13 additions & 5 deletions packages/vite/src/node/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ const IIFE_BEGIN_RE =
const validExtensionRE = /\.\w+$/
const jsxExtensionsRE = /\.(?:j|t)sx\b/

// the final build should always support dynamic import and import.meta.
// if they need to be polyfilled, plugin-legacy should be used.
// plugin-legacy detects these two features when checking for modern code.
export const defaultEsbuildSupported = {
'dynamic-import': true,
'import-meta': true,
}

let server: ViteDevServer

export interface ESBuildOptions extends TransformOptions {
Expand Down Expand Up @@ -235,6 +243,10 @@ export function esbuildPlugin(config: ResolvedConfig): Plugin {
// Also transforming multiple times with keepNames enabled breaks
// tree-shaking. (#9164)
keepNames: false,
supported: {
...defaultEsbuildSupported,
...esbuildTransformOptions.supported,
},
}

return {
Expand Down Expand Up @@ -360,12 +372,8 @@ export function resolveEsbuildTranspileOptions(
loader: 'js',
target: target || undefined,
format: rollupToEsbuildFormatMap[format],
// the final build should always support dynamic import and import.meta.
// if they need to be polyfilled, plugin-legacy should be used.
// plugin-legacy detects these two features when checking for modern code.
supported: {
'dynamic-import': true,
'import-meta': true,
...defaultEsbuildSupported,
...esbuildOptions.supported,
},
}
Expand Down