Skip to content

Commit bba3bb8

Browse files
hktitofsapphi-red
andauthored
fix: merge build.rolldownOptions.output.minify correctly (#23536)
Co-authored-by: sapphi-red <green@sapphi.red>
1 parent 5e4b9ca commit bba3bb8

2 files changed

Lines changed: 75 additions & 14 deletions

File tree

‎packages/vite/src/node/__tests__/build.spec.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,60 @@ describe('resolveBuildOutputs', () => {
579579
)
580580
})
581581
})
582+
583+
describe('output minify and comments resolution in resolveRolldownOptions', () => {
584+
const buildProjectRoot = resolve(dirname, 'packages/build-project')
585+
586+
test('merges a partial output.minify object with the lib defaults', async () => {
587+
const builder = await createBuilder({
588+
root: buildProjectRoot,
589+
logLevel: 'silent',
590+
build: {
591+
minify: 'oxc',
592+
lib: { ...baseLibOptions, formats: ['es'] },
593+
rolldownOptions: {
594+
output: {
595+
minify: { mangle: { keepNames: true } },
596+
},
597+
},
598+
},
599+
})
600+
const options = resolveRolldownOptions(
601+
builder.environments.client,
602+
new ChunkMetadataMap(),
603+
)
604+
const outputs = options.output!
605+
const output = Array.isArray(outputs) ? outputs[0] : outputs
606+
expect(output.minify).toStrictEqual({
607+
compress: true,
608+
mangle: { keepNames: true },
609+
codegen: false,
610+
})
611+
})
612+
613+
test('keeps an explicit boolean output.minify untouched', async () => {
614+
const builder = await createBuilder({
615+
root: buildProjectRoot,
616+
logLevel: 'silent',
617+
build: {
618+
minify: 'oxc',
619+
lib: { ...baseLibOptions, formats: ['es'] },
620+
rolldownOptions: {
621+
output: {
622+
minify: false,
623+
},
624+
},
625+
},
626+
})
627+
const options = resolveRolldownOptions(
628+
builder.environments.client,
629+
new ChunkMetadataMap(),
630+
)
631+
const outputs = options.output!
632+
const output = Array.isArray(outputs) ? outputs[0] : outputs
633+
expect(output.minify).toBe(false)
634+
})
635+
})
582636
})
583637

584638
describe('resolveLibFilename', () => {

‎packages/vite/src/node/build.ts‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,20 @@ export function resolveRolldownOptions(
745745
findNearestPackageData(root, packageCache)?.data.type,
746746
)
747747
: 'js'
748+
const resolvedMinify =
749+
options.minify === 'oxc'
750+
? libOptions && (format === 'es' || format === 'esm')
751+
? {
752+
compress: true,
753+
mangle: true,
754+
// Do not minify whitespace for ES lib output since that would remove
755+
// pure annotations and break tree-shaking
756+
codegen: false,
757+
}
758+
: true
759+
: options.minify === false
760+
? 'dce-only'
761+
: false
748762
return {
749763
dir: outDir,
750764
// Default format is 'es' for regular and for SSR builds
@@ -786,22 +800,15 @@ export function resolveRolldownOptions(
786800
(typeof input === 'string' || Object.keys(input).length === 1))
787801
? false
788802
: undefined),
789-
minify:
790-
options.minify === 'oxc'
791-
? libOptions && (format === 'es' || format === 'esm')
792-
? {
793-
compress: true,
794-
mangle: true,
795-
// Do not minify whitespace for ES lib output since that would remove
796-
// pure annotations and break tree-shaking
797-
codegen: false,
798-
}
799-
: true
800-
: options.minify === false
801-
? 'dce-only'
802-
: false,
803803
topLevelVar: true,
804804
...output,
805+
minify:
806+
output.minify === undefined
807+
? resolvedMinify
808+
: typeof output.minify === 'object' &&
809+
typeof resolvedMinify === 'object'
810+
? { ...resolvedMinify, ...output.minify }
811+
: output.minify,
805812
comments:
806813
typeof output.comments === 'boolean'
807814
? output.comments

0 commit comments

Comments
 (0)