Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/guide/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
64 changes: 62 additions & 2 deletions packages/void/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -96,6 +96,49 @@ const scheduledPointerCleanups = new Set<string>();

const scheduledPreviewPointerCleanups = new Set<string>();

/** 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;
Expand Down Expand Up @@ -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)) {
Expand Down
113 changes: 108 additions & 5 deletions packages/void/test/unit/framework-worker-minify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -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<unknown> = [];
const chunks: Array<string> = [];
// 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();
Expand All @@ -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) => {
Expand Down Expand Up @@ -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({
Expand All @@ -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');
});