diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index b3f6533c86ad09..591ea0cc8fa760 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -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' @@ -799,8 +802,7 @@ async function prepareEsbuildOptimizerRun( charset: 'utf8', ...esbuildOptions, supported: { - 'dynamic-import': true, - 'import-meta': true, + ...defaultEsbuildSupported, ...esbuildOptions.supported, }, }) diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index 7076d0e6291c1b..54f1796afd6381 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -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 { @@ -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 { @@ -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, }, }