Skip to content

Commit

Permalink
fix(esbuild): preserve import.meta even if esbuild.target is set to l…
Browse files Browse the repository at this point in the history
…ower versions (#16151)
  • Loading branch information
sapphi-red authored Mar 13, 2024
1 parent 8cd846c commit 6f77b2b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
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

0 comments on commit 6f77b2b

Please sign in to comment.