Skip to content

Commit

Permalink
feat: don't override build.target if terser is 5.16.0+ (#12197)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Mar 1, 2023
1 parent fbbf8fe commit 9885f6f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
18 changes: 16 additions & 2 deletions packages/vite/src/node/build.ts
Expand Up @@ -34,6 +34,7 @@ import {
joinUrlSegments,
lookupFile,
normalizePath,
requireResolveFromRootWithFallback,
} from './utils'
import { manifestPlugin } from './plugins/manifest'
import type { Logger } from './logger'
Expand Down Expand Up @@ -298,6 +299,7 @@ export interface ResolvedBuildOptions
export function resolveBuildOptions(
raw: BuildOptions | undefined,
logger: Logger,
root: string,
): ResolvedBuildOptions {
const deprecatedPolyfillModulePreload = raw?.polyfillModulePreload
if (raw) {
Expand Down Expand Up @@ -378,8 +380,20 @@ export function resolveBuildOptions(
if (resolved.target === 'modules') {
resolved.target = ESBUILD_MODULES_TARGET
} else if (resolved.target === 'esnext' && resolved.minify === 'terser') {
// esnext + terser: limit to es2021 so it can be minified by terser
resolved.target = 'es2021'
try {
const terserPackageJsonPath = requireResolveFromRootWithFallback(
root,
'terser/package.json',
)
const terserPackageJson = JSON.parse(
fs.readFileSync(terserPackageJsonPath, 'utf-8'),
)
const v = terserPackageJson.version.split('.')
if (v[0] === '5' && v[1] < 16) {
// esnext + terser 5.16<: limit to es2021 so it can be minified by terser
resolved.target = 'es2021'
}
} catch {}
}

if (!resolved.cssTarget) {
Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/node/config.ts
Expand Up @@ -529,7 +529,11 @@ export async function resolveConfig(
: './'
: resolveBaseUrl(config.base, isBuild, logger) ?? '/'

const resolvedBuildOptions = resolveBuildOptions(config.build, logger)
const resolvedBuildOptions = resolveBuildOptions(
config.build,
logger,
resolvedRoot,
)

// resolve cache directory
const pkgPath = lookupFile(resolvedRoot, [`package.json`], { pathOnly: true })
Expand Down

0 comments on commit 9885f6f

Please sign in to comment.