Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: don't override build.target if terser is 5.16.0+ #12197

Merged
merged 3 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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