diff --git a/docs/guide/deployment.md b/docs/guide/deployment.md index 221aa677b..637907b9c 100644 --- a/docs/guide/deployment.md +++ b/docs/guide/deployment.md @@ -74,7 +74,7 @@ export default defineConfig({ If a Vite plugin overrides minification late, set `build.rolldownOptions.output.minify` for `void_worker` or set `minify` in the plugin's `outputOptions` hook. Use `false` to disable minification or `true` for standard minification. -React Router, TanStack Start, and vinext server builds are minified by default, and you can disable minification with `environments.ssr.build.minify: false` (and `environments.rsc.build.minify: false` for vinext App Router). +React Router, TanStack Start, and vinext server builds are minified by default with most function and class names preserved, and you can disable minification with `environments.ssr.build.minify: false` (and `environments.rsc.build.minify: false` for vinext App Router). If only a server environment selects `minify: 'esbuild'`, Void warns and uses Oxc for that environment. To select Oxc explicitly, set `minify: 'oxc'` there. diff --git a/packages/void/src/index.ts b/packages/void/src/index.ts index 5f8de99d9..f82a925e0 100644 --- a/packages/void/src/index.ts +++ b/packages/void/src/index.ts @@ -1,7 +1,7 @@ import { existsSync, mkdirSync, readFileSync, rmSync } from 'node:fs'; import { join, resolve } from 'pathslash'; import pc from 'picocolors'; -import type { BuildEnvironmentOptions, Plugin } from 'vite'; +import type { BuildEnvironmentOptions, Plugin, Rolldown } from 'vite'; import { analyzeApplicationSync, getNativeWorkerSourceFiles } from './application-analysis.ts'; import type { ApplicationAnalysis } from './application-analysis.ts'; import { applyBindingOverrides } from './application-features.ts'; @@ -96,6 +96,49 @@ const scheduledPointerCleanups = new Set(); const scheduledPreviewPointerCleanups = new Set(); +/** Preserve names only while this output is using Void's default Oxc minifier. */ +function withFrameworkNameDefaults(output: Rolldown.OutputOptions = {}): Rolldown.OutputOptions { + if (output.minify !== undefined || output.keepNames !== undefined) { + return output; + } + const keepNames = { function: true, class: true }; + const defaultMinifier = { compress: { keepNames }, mangle: { keepNames } }; + const configured: Rolldown.OutputOptions = { + ...output, + plugins: [ + { + name: 'void:framework-keep-names', + outputOptions: { + order: 'pre', + handler(options) { + if ( + configured.minify === undefined && + options.minify === true && + options.keepNames === undefined + ) { + return { ...options, minify: defaultMinifier }; + } + }, + }, + }, + output.plugins, + { + name: 'void:framework-keep-names-override', + outputOptions: { + order: 'post', + handler(options) { + // Oxc's nested keepNames settings override Rolldown's top-level option. + if (options.minify === defaultMinifier && options.keepNames !== undefined) { + return { ...options, minify: true }; + } + }, + }, + }, + ], + }; + return configured; +} + /** Returns the Vite plugins that build and serve a Void app for its configured target. */ export function voidPlugin(options?: { persistTo?: string; @@ -579,11 +622,28 @@ export function voidPlugin(options?: { ); } if (!framework || (minify ?? rootMinify) === undefined || useOxcFallback) { - environments[name] = { build: { minify: true } }; + environments[name] = { + build: { + minify: true, + ...(framework && !useOxcFallback && { voidKeepFrameworkNames: true }), + }, + }; } } return { ...serverConfig, environments }; }, + configResolved(resolved) { + if (resolved.command === 'build') { + for (const { build } of Object.values(resolved.environments)) { + if ('voidKeepFrameworkNames' in build) { + const options = build.rolldownOptions; + options.output = Array.isArray(options.output) + ? options.output.map(withFrameworkNameDefaults) + : withFrameworkNameDefaults(options.output); + } + } + } + }, configureServer(server) { // Remote mode status — log which bindings are using the proxy if (remoteMode && (bindings.needsD1 || bindings.needsKV || bindings.needsR2)) { diff --git a/packages/void/test/unit/framework-worker-minify.test.ts b/packages/void/test/unit/framework-worker-minify.test.ts index 6c8eb0bb8..f55f09ec7 100644 --- a/packages/void/test/unit/framework-worker-minify.test.ts +++ b/packages/void/test/unit/framework-worker-minify.test.ts @@ -8,6 +8,9 @@ import { voidPlugin } from '../../src/index.ts'; vi.mock('../../src/plugin-cloudflare.ts', () => import('../helpers/cloudflare-vite-plugin.ts')); +const keepNames = { function: true, class: true }; +const keepBothNames = { compress: { keepNames }, mangle: { keepNames } }; + const workers = [ { framework: 'react-router', environment: 'ssr' }, { framework: 'vinext', environment: 'ssr' }, @@ -27,11 +30,17 @@ async function buildServer( framework === 'react-router' ? { '@react-router/dev': '*' } : { vinext: '*' }; writeFileSync(join(root, 'package.json'), JSON.stringify({ type: 'module', dependencies })); const input = join(root, 'entry.ts'); - writeFileSync(input, 'export function probe() { return new Error("probe"); }'); + writeFileSync( + input, + `class ReadableError extends Error {} +function makeReadableError() { return new ReadableError("probe"); } +export function probe() { return [makeReadableError().constructor.name, makeReadableError.name]; }`, + ); if (framework === 'vinext') { mkdirSync(join(root, 'app')); } const outputMinifiers: Array = []; + const chunks: Array = []; // Only Void's plugin factory needs the fixture cwd. esbuild keeps its initial cwd for the service process. const cwd = vi.spyOn(process, 'cwd').mockReturnValueOnce(root); const plugins = voidPlugin(); @@ -49,24 +58,32 @@ async function buildServer( }, plugins: [ ...plugins, + config.plugins, { name: 'test:framework-worker-minify', configResolved(resolved) { resolved.environments[environment].build.rolldownOptions.input = input; }, - generateBundle(output) { + generateBundle(output, bundle) { outputMinifiers.push(output.minify); + for (const file of Object.values(bundle)) { + if (file.type === 'chunk') { + chunks.push(file.code); + } + } }, }, ], }); const server = builder.environments[environment]; await builder.build(server); - return { minify: server.config.build.minify, outputMinifiers }; + return { minify: server.config.build.minify, outputMinifiers, code: chunks.join('\n') }; } -it.each(workers)('defaults $framework $environment to Oxc', async (worker) => { - expect((await buildServer({}, worker)).minify).toBe('oxc'); +it.each(workers)('preserves names by default in $framework $environment', async (worker) => { + const result = await buildServer({}, worker); + expect(result.minify).toBe('oxc'); + expect(result.outputMinifiers).toMatchObject([keepBothNames]); }); it.each(workers)('honors root build.minify false for $framework $environment', async (worker) => { @@ -98,6 +115,17 @@ it('uses Oxc for an RSC-only esbuild request even if root minification is disabl expect(log).toHaveBeenCalledWith(expect.stringContaining('Using Oxc')); }); +it('uses standard Oxc name mangling when only SSR selects esbuild', async () => { + const result = await buildServer({ + environments: { ssr: { build: { minify: 'esbuild' } } }, + }); + const keepNames = { function: false, class: false }; + expect(result.minify).toBe('oxc'); + expect(result.outputMinifiers).toMatchObject([ + { compress: { keepNames }, mangle: { keepNames } }, + ]); +}); + it('uses esbuild when it is selected at the root and in the server environment', async () => { const log = vi.spyOn(console, 'log').mockImplementation(() => {}); const result = await buildServer({ @@ -124,3 +152,78 @@ it('honors explicit output minifier options at both levels', async () => { expect((await buildServer({ build })).outputMinifiers).toEqual([{}]); expect((await buildServer({ environments: { ssr: { build } } })).outputMinifiers).toEqual([{}]); }); + +it('runs a singleton app output hook at each config level', async () => { + const rootHook = vi.fn(); + const environmentHook = vi.fn(); + await buildServer({ + build: { + rolldownOptions: { plugins: { name: 'test:root-output-hook', outputOptions: rootHook } }, + }, + }); + await buildServer({ + environments: { + ssr: { + build: { + rolldownOptions: { + plugins: { name: 'test:environment-output-hook', outputOptions: environmentHook }, + }, + }, + }, + }, + }); + expect([rootHook.mock.calls.length, environmentHook.mock.calls.length]).toEqual([1, 1]); +}); + +it('emits unminified code without name helpers when a post hook disables minification', async () => { + const result = await buildServer({ + build: { + rolldownOptions: { + plugins: [ + { + name: 'test:disable-minify', + outputOptions: { + order: 'post', + handler: (output) => ({ ...output, minify: false }), + }, + }, + ], + }, + }, + }); + expect(result.outputMinifiers).toEqual([false]); + expect(result.code).toContain('function makeReadableError()'); + expect(result.code).not.toContain('__name'); +}); + +it('keeps names in the default output of a mixed output array', async () => { + const result = await buildServer({ + build: { rolldownOptions: { output: [{ minify: false }, {}] } }, + }); + expect(result.outputMinifiers).toMatchObject([false, keepBothNames]); +}); + +it('uses standard minification for an explicit output minify true', async () => { + const result = await buildServer({ + build: { rolldownOptions: { output: { minify: true } } }, + }); + expect(result.code).not.toContain('ReadableError'); +}); + +it('honors keepNames false from a late Vite output hook', async () => { + const result = await buildServer({ + plugins: [ + { + name: 'test:late-keep-names', + enforce: 'post', + outputOptions: { + order: 'post', + handler(output) { + return { ...output, keepNames: false }; + }, + }, + }, + ], + }); + expect(result.code).not.toContain('ReadableError'); +});