Skip to content

Commit

Permalink
Use esbuild's "supported" field for bigints and nullish coalescing.
Browse files Browse the repository at this point in the history
An alternative approach to vitejs#9062
  • Loading branch information
lgarron committed Jul 13, 2022
1 parent f020066 commit ef7fe35
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand All @@ -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.
Expand Down Expand Up @@ -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'
Expand Down
7 changes: 6 additions & 1 deletion packages/vite/src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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: {
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/node/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -301,6 +302,7 @@ export function resolveEsbuildTranspileOptions(
const options: TransformOptions = {
...config.esbuild,
target: target || undefined,
supported: supported || undefined,
format: rollupToEsbuildFormatMap[format]
}

Expand Down

0 comments on commit ef7fe35

Please sign in to comment.