Skip to content

Commit 6ff7d2e

Browse files
feat: use rolldown minify (#73)
Co-authored-by: Yury <[email protected]>
1 parent 135a44a commit 6ff7d2e

File tree

8 files changed

+39
-15
lines changed

8 files changed

+39
-15
lines changed

packages/vite/src/node/__tests__/plugins/css.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,21 +310,38 @@ require("other-module");`
310310

311311
const replacer = getEmptyChunkReplacer(['pure_css_chunk.js'], 'cjs')
312312
const newCode = replacer(code)
313+
expect(newCode.length).toBe(code.length)
313314
expect(newCode).toMatchInlineSnapshot(
314315
`"require("some-module"),/* empty css */require("other-module");"`,
315316
)
316317
// So there should be no pure css chunk anymore
317318
expect(newCode).not.toContain('pure_css_chunk.js')
318319
})
319320

321+
test('replaces require call in minified code that uses comma operator 2', () => {
322+
const code = 'require("pure_css_chunk.js"),console.log();'
323+
const replacer = getEmptyChunkReplacer(['pure_css_chunk.js'], 'cjs')
324+
const newCode = replacer(code)
325+
expect(newCode.length).toBe(code.length)
326+
expect(newCode).toMatchInlineSnapshot(
327+
`"/* empty css */console.log();"`,
328+
)
329+
// So there should be no pure css chunk anymore
330+
expect(newCode).not.toContain('pure_css_chunk.js')
331+
})
332+
320333
test('replaces require call in minified code that uses comma operator followed by assignment', () => {
321334
const code =
322335
'require("some-module"),require("pure_css_chunk.js");const v=require("other-module");'
323336

324337
const replacer = getEmptyChunkReplacer(['pure_css_chunk.js'], 'cjs')
325-
expect(replacer(code)).toMatchInlineSnapshot(
338+
const newCode = replacer(code)
339+
expect(newCode.length).toBe(code.length)
340+
expect(newCode).toMatchInlineSnapshot(
326341
`"require("some-module");/* empty css */const v=require("other-module");"`,
327342
)
343+
// So there should be no pure css chunk anymore
344+
expect(newCode).not.toContain('pure_css_chunk.js')
328345
})
329346
})
330347

packages/vite/src/node/build.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ export interface BuildEnvironmentOptions {
167167
sourcemap?: boolean | 'inline' | 'hidden'
168168
/**
169169
* Set to `false` to disable minification, or specify the minifier to use.
170-
* Available options are 'terser' or 'esbuild'.
171-
* @default 'esbuild'
170+
* Available options are 'terser' or 'esbuild' or 'oxc'.
171+
* @default 'oxc'
172172
*/
173-
minify?: boolean | 'terser' | 'esbuild'
173+
minify?: boolean | 'terser' | 'esbuild' | 'oxc'
174174
/**
175175
* Options for terser
176176
* https://terser.org/docs/api-reference#minify-options
@@ -418,7 +418,7 @@ export function resolveBuildEnvironmentOptions(
418418
{
419419
...buildEnvironmentOptionsDefaults,
420420
cssCodeSplit: !raw.lib,
421-
minify: consumer === 'server' ? false : 'esbuild',
421+
minify: consumer === 'server' ? false : 'oxc',
422422
rollupOptions: {
423423
platform: consumer === 'server' ? 'node' : 'browser',
424424
},
@@ -438,7 +438,7 @@ export function resolveBuildEnvironmentOptions(
438438
if ((merged.minify as string) === 'false') {
439439
merged.minify = false
440440
} else if (merged.minify === true) {
441-
merged.minify = 'esbuild'
441+
merged.minify = 'oxc'
442442
}
443443

444444
const defaultModulePreload = {
@@ -777,6 +777,7 @@ async function buildEnvironment(
777777
: path.posix.join(options.assetsDir, `[name]-[hash].[ext]`),
778778
inlineDynamicImports:
779779
output.format === 'umd' || output.format === 'iife',
780+
minify: options.minify === 'oxc',
780781
...output,
781782
}
782783
}

packages/vite/src/node/plugins/css.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,10 +1151,15 @@ export function getEmptyChunkReplacer(
11511151
code.replace(
11521152
emptyChunkRE,
11531153
// remove css import while preserving source map location
1154-
(m) =>
1155-
outputFormat === 'es'
1156-
? `/* empty css ${''.padEnd(m.length - 15)}*/`
1157-
: `${m.at(-1)}/* empty css ${''.padEnd(m.length - 16)}*/`,
1154+
(m, p1, p2) => {
1155+
if (outputFormat === 'es') {
1156+
return `/* empty css ${''.padEnd(m.length - 15)}*/`
1157+
}
1158+
if (p2 === ',') {
1159+
return `${p1}/* empty css ${''.padEnd(m.length - 15 - p1.length)}*/`
1160+
}
1161+
return `${p2}/* empty css ${''.padEnd(m.length - 16)}*/`
1162+
},
11581163
)
11591164
}
11601165

packages/vite/src/node/plugins/worker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ async function bundleWorkerEntry(
116116
config.build.assetsDir,
117117
'[name]-[hash].[ext]',
118118
),
119+
minify: config.build.minify === 'oxc',
119120
...workerConfig,
120121
format,
121122
sourcemap: config.build.sourcemap,

playground/css-codesplit/__tests__/css-codesplit.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe.runIf(isBuild)('build', () => {
5555
expect(sharedCSSWithJSChunk).toMatch(`/* empty css`)
5656
// there are functions and modules in the src code that should be tree-shaken
5757
expect(sharedCSSWithJSChunk).not.toMatch('function')
58-
expect(sharedCSSWithJSChunk).not.toMatch(/import(?!".\/modulepreload)/)
58+
expect(sharedCSSWithJSChunk).not.toMatch(/import(?!\s*".\/modulepreload)/)
5959
})
6060

6161
test('should generate correct manifest', async () => {

playground/js-sourcemap/__tests__/js-sourcemap.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ describe.runIf(isBuild)('build tests', () => {
140140
expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(`
141141
{
142142
"ignoreList": [],
143-
"mappings": ";4jCAAA,OAAO,6BAAuB,wBAE9B,QAAQ,IAAI",
143+
"mappings": ";ypCAAA,OAAO,6BAAuB,wBAE9B,QAAQ,IAAI,wBAAuB",
144144
"sources": [
145145
"../../after-preload-dynamic.js",
146146
],

playground/worker/__tests__/es/worker-es.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe.runIf(isBuild)('build', () => {
104104
)
105105

106106
// worker should have all imports resolved and no exports
107-
expect(workerContent).not.toMatch(/import[^.]/)
107+
expect(workerContent).not.toMatch(/import\s*["(]/)
108108
expect(workerContent).not.toMatch(/\bexport\b/)
109109
// chunk
110110
expect(content).toMatch(`new Worker("/es/assets`)
@@ -113,7 +113,7 @@ describe.runIf(isBuild)('build', () => {
113113
expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`)
114114
expect(content).toMatch(`self.Blob`)
115115
expect(content).toMatch(
116-
/try\{if\(\w+=\w+&&\(self\.URL\|\|self\.webkitURL\)\.createObjectURL\(\w+\),!\w+\)throw""/,
116+
/try\{\w+=\w+&&\(self\.URL\|\|self\.webkitURL\)\.createObjectURL\(\w+\)[;\w()!]+throw\s*""/,
117117
)
118118
// inlined shared worker
119119
expect(content).toMatch(

playground/worker/__tests__/relative-base/worker-relative-base.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe.runIf(isBuild)('build', () => {
7575
)
7676

7777
// worker should have all imports resolved and no exports
78-
expect(workerContent).not.toMatch(/import(?!\.)/) // accept import.meta.url
78+
expect(workerContent).not.toMatch(/import\s*["(]/)
7979
expect(workerContent).not.toMatch(/\bexport\b/)
8080
// chunk
8181
expect(content).toMatch(`new Worker(""+new URL("../worker-entries/`)

0 commit comments

Comments
 (0)