diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 973dcef199d8ee..7c878c7edac2fc 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -47,7 +47,7 @@ import { loadFallbackPlugin } from './plugins/loadFallback' import type { PackageData } from './packages' import { watchPackageDataPlugin } from './packages' import { ensureWatchPlugin } from './plugins/ensureWatch' -import { ESBUILD_MODULES_TARGET, VERSION } from './constants' +import { ESBUILD_MODULES_SUPPORTED_OVERRIDES, ESBUILD_MODULES_TARGET, VERSION } from './constants' export interface BuildOptions { /** @@ -67,6 +67,10 @@ export interface BuildOptions { * https://esbuild.github.io/content-types/#javascript for more details. */ target?: 'modules' | TransformOptions['target'] | false + supported?: { + "bigint"?: boolean, + "nullish-coalescing"?: boolean + }, /** * whether to inject module preload polyfill. * Note: does not apply to library mode. @@ -271,6 +275,7 @@ export function resolveBuildOptions( // handle special build targets if (resolved.target === 'modules') { resolved.target = ESBUILD_MODULES_TARGET + resolved.supported = ESBUILD_MODULES_SUPPORTED_OVERRIDES } else if (resolved.target === 'esnext' && resolved.minify === 'terser') { // esnext + terser: limit to es2021 so it can be minified by terser resolved.target = 'es2021' diff --git a/packages/vite/src/node/constants.ts b/packages/vite/src/node/constants.ts index 5a108652ef7ce6..7c0ed5e795e6d9 100644 --- a/packages/vite/src/node/constants.ts +++ b/packages/vite/src/node/constants.ts @@ -18,9 +18,14 @@ export const ESBUILD_MODULES_TARGET = [ 'edge88', 'firefox78', 'chrome87', - 'safari13' // transpile nullish coalescing + 'safari13.1' ] +export const ESBUILD_MODULES_SUPPORTED_OVERRIDES = { + "nullish-coalescing": false, + "bigint": true +} + export const DEFAULT_EXTENSIONS = [ '.mjs', '.js', diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 4cbd13f8d965a7..f06bada3714764 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -24,7 +24,7 @@ import { writeFile } from '../utils' import { transformWithEsbuild } from '../plugins/esbuild' -import { ESBUILD_MODULES_TARGET } from '../constants' +import { ESBUILD_MODULES_SUPPORTED_OVERRIDES, ESBUILD_MODULES_TARGET } from '../constants' import { esbuildCjsExternalPlugin, esbuildDepPlugin } from './esbuildDepPlugin' import { scanImports } from './scan' export { @@ -600,6 +600,7 @@ export async function runOptimizeDeps( } : undefined, target: isBuild ? config.build.target || undefined : ESBUILD_MODULES_TARGET, + supported: isBuild ? undefined : ESBUILD_MODULES_SUPPORTED_OVERRIDES, external, logLevel: 'error', splitting: true, @@ -1041,6 +1042,7 @@ export function getDepHash(config: ResolvedConfig, ssr: boolean): string { root: config.root, resolve: config.resolve, buildTarget: config.build.target, + buildSupported: config.build.supported, assetsInclude: config.assetsInclude, plugins: config.plugins.map((p) => p.name), optimizeDeps: { diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index 42e5bbac76316a..fa8f83b7bae18a 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -288,6 +288,7 @@ export function resolveEsbuildTranspileOptions( format: InternalModuleFormat ): TransformOptions | null { const target = config.build.target + const supported = config.build.supported const minify = config.build.minify === 'esbuild' if ((!target || target === 'esnext') && !minify) { @@ -301,6 +302,7 @@ export function resolveEsbuildTranspileOptions( const options: TransformOptions = { ...config.esbuild, target: target || undefined, + supported: supported || undefined, format: rollupToEsbuildFormatMap[format] }