Skip to content

Commit

Permalink
feat: use rolldown minify (#73)
Browse files Browse the repository at this point in the history
Co-authored-by: Yury <[email protected]>
  • Loading branch information
sapphi-red and YurySolovyov authored Dec 4, 2024
1 parent 135a44a commit 6ff7d2e
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 15 deletions.
19 changes: 18 additions & 1 deletion packages/vite/src/node/__tests__/plugins/css.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,21 +310,38 @@ require("other-module");`

const replacer = getEmptyChunkReplacer(['pure_css_chunk.js'], 'cjs')
const newCode = replacer(code)
expect(newCode.length).toBe(code.length)
expect(newCode).toMatchInlineSnapshot(
`"require("some-module"),/* empty css */require("other-module");"`,
)
// So there should be no pure css chunk anymore
expect(newCode).not.toContain('pure_css_chunk.js')
})

test('replaces require call in minified code that uses comma operator 2', () => {
const code = 'require("pure_css_chunk.js"),console.log();'
const replacer = getEmptyChunkReplacer(['pure_css_chunk.js'], 'cjs')
const newCode = replacer(code)
expect(newCode.length).toBe(code.length)
expect(newCode).toMatchInlineSnapshot(
`"/* empty css */console.log();"`,
)
// So there should be no pure css chunk anymore
expect(newCode).not.toContain('pure_css_chunk.js')
})

test('replaces require call in minified code that uses comma operator followed by assignment', () => {
const code =
'require("some-module"),require("pure_css_chunk.js");const v=require("other-module");'

const replacer = getEmptyChunkReplacer(['pure_css_chunk.js'], 'cjs')
expect(replacer(code)).toMatchInlineSnapshot(
const newCode = replacer(code)
expect(newCode.length).toBe(code.length)
expect(newCode).toMatchInlineSnapshot(
`"require("some-module");/* empty css */const v=require("other-module");"`,
)
// So there should be no pure css chunk anymore
expect(newCode).not.toContain('pure_css_chunk.js')
})
})

Expand Down
11 changes: 6 additions & 5 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ export interface BuildEnvironmentOptions {
sourcemap?: boolean | 'inline' | 'hidden'
/**
* Set to `false` to disable minification, or specify the minifier to use.
* Available options are 'terser' or 'esbuild'.
* @default 'esbuild'
* Available options are 'terser' or 'esbuild' or 'oxc'.
* @default 'oxc'
*/
minify?: boolean | 'terser' | 'esbuild'
minify?: boolean | 'terser' | 'esbuild' | 'oxc'
/**
* Options for terser
* https://terser.org/docs/api-reference#minify-options
Expand Down Expand Up @@ -418,7 +418,7 @@ export function resolveBuildEnvironmentOptions(
{
...buildEnvironmentOptionsDefaults,
cssCodeSplit: !raw.lib,
minify: consumer === 'server' ? false : 'esbuild',
minify: consumer === 'server' ? false : 'oxc',
rollupOptions: {
platform: consumer === 'server' ? 'node' : 'browser',
},
Expand All @@ -438,7 +438,7 @@ export function resolveBuildEnvironmentOptions(
if ((merged.minify as string) === 'false') {
merged.minify = false
} else if (merged.minify === true) {
merged.minify = 'esbuild'
merged.minify = 'oxc'
}

const defaultModulePreload = {
Expand Down Expand Up @@ -777,6 +777,7 @@ async function buildEnvironment(
: path.posix.join(options.assetsDir, `[name]-[hash].[ext]`),
inlineDynamicImports:
output.format === 'umd' || output.format === 'iife',
minify: options.minify === 'oxc',
...output,
}
}
Expand Down
13 changes: 9 additions & 4 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1151,10 +1151,15 @@ export function getEmptyChunkReplacer(
code.replace(
emptyChunkRE,
// remove css import while preserving source map location
(m) =>
outputFormat === 'es'
? `/* empty css ${''.padEnd(m.length - 15)}*/`
: `${m.at(-1)}/* empty css ${''.padEnd(m.length - 16)}*/`,
(m, p1, p2) => {
if (outputFormat === 'es') {
return `/* empty css ${''.padEnd(m.length - 15)}*/`
}
if (p2 === ',') {
return `${p1}/* empty css ${''.padEnd(m.length - 15 - p1.length)}*/`
}
return `${p2}/* empty css ${''.padEnd(m.length - 16)}*/`
},
)
}

Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ async function bundleWorkerEntry(
config.build.assetsDir,
'[name]-[hash].[ext]',
),
minify: config.build.minify === 'oxc',
...workerConfig,
format,
sourcemap: config.build.sourcemap,
Expand Down
2 changes: 1 addition & 1 deletion playground/css-codesplit/__tests__/css-codesplit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe.runIf(isBuild)('build', () => {
expect(sharedCSSWithJSChunk).toMatch(`/* empty css`)
// there are functions and modules in the src code that should be tree-shaken
expect(sharedCSSWithJSChunk).not.toMatch('function')
expect(sharedCSSWithJSChunk).not.toMatch(/import(?!".\/modulepreload)/)
expect(sharedCSSWithJSChunk).not.toMatch(/import(?!\s*".\/modulepreload)/)
})

test('should generate correct manifest', async () => {
Expand Down
2 changes: 1 addition & 1 deletion playground/js-sourcemap/__tests__/js-sourcemap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe.runIf(isBuild)('build tests', () => {
expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(`
{
"ignoreList": [],
"mappings": ";4jCAAA,OAAO,6BAAuB,wBAE9B,QAAQ,IAAI",
"mappings": ";ypCAAA,OAAO,6BAAuB,wBAE9B,QAAQ,IAAI,wBAAuB",
"sources": [
"../../after-preload-dynamic.js",
],
Expand Down
4 changes: 2 additions & 2 deletions playground/worker/__tests__/es/worker-es.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe.runIf(isBuild)('build', () => {
)

// worker should have all imports resolved and no exports
expect(workerContent).not.toMatch(/import[^.]/)
expect(workerContent).not.toMatch(/import\s*["(]/)
expect(workerContent).not.toMatch(/\bexport\b/)
// chunk
expect(content).toMatch(`new Worker("/es/assets`)
Expand All @@ -113,7 +113,7 @@ describe.runIf(isBuild)('build', () => {
expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`)
expect(content).toMatch(`self.Blob`)
expect(content).toMatch(
/try\{if\(\w+=\w+&&\(self\.URL\|\|self\.webkitURL\)\.createObjectURL\(\w+\),!\w+\)throw""/,
/try\{\w+=\w+&&\(self\.URL\|\|self\.webkitURL\)\.createObjectURL\(\w+\)[;\w()!]+throw\s*""/,
)
// inlined shared worker
expect(content).toMatch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe.runIf(isBuild)('build', () => {
)

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

0 comments on commit 6ff7d2e

Please sign in to comment.