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
25 changes: 24 additions & 1 deletion packages/void/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ function hasBinding(list: ReadonlyArray<{ binding: string }> | undefined, name:
const CLOUDFLARE_BUILTIN_IMPORT = /^cloudflare:/;
const SANDBOX_SDK_IMPORT = '@cloudflare/sandbox';
const SANDBOX_SDK_IMPORT_FILTER = /^@cloudflare\/sandbox$/;
const DEFAULT_WORKER_ENVIRONMENT_NAME = 'void_worker';
const DEFAULT_WORKER_OUT_DIR = 'dist/ssr';

function resolveSandboxSdkEntry(): string {
return fileURLToPath(import.meta.resolve(SANDBOX_SDK_IMPORT));
Expand All @@ -356,6 +358,26 @@ function createCloudflareBuiltinsExternalPlugin(): Plugin {
};
}

function createDefaultWorkerEnvironmentPlugin(): Plugin {
return {
name: 'void:default-worker-environment',
config(config) {
const currentEnvironment = config.environments?.[DEFAULT_WORKER_ENVIRONMENT_NAME];
return {
environments: {
[DEFAULT_WORKER_ENVIRONMENT_NAME]: {
...currentEnvironment,
build: {
...currentEnvironment?.build,
outDir: currentEnvironment?.build?.outDir ?? DEFAULT_WORKER_OUT_DIR,
},
},
},
};
},
};
}

function createSandboxSdkResolvePlugin(): Plugin {
let resolved: string | undefined;

Expand Down Expand Up @@ -1333,6 +1355,7 @@ export function voidPlugin(options?: {

return [
createCloudflareBuiltinsExternalPlugin(),
createDefaultWorkerEnvironmentPlugin(),
restartPlugin(),
bindingWatchPlugin,
envPlugin,
Expand Down Expand Up @@ -1369,7 +1392,7 @@ export function voidPlugin(options?: {
...inspectorPortOption,
persistState: { path: options?.persistTo ?? outDir },
auxiliaryWorkers: options?.auxiliaryWorkers,
viteEnvironment: { name: 'ssr' },
viteEnvironment: { name: DEFAULT_WORKER_ENVIRONMENT_NAME },
config: mergeBindings,
}),
];
Expand Down
65 changes: 65 additions & 0 deletions packages/void/test/unit/plugin-vite-environment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'pathe';
import { resolveConfig } from 'vite';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { voidPlugin } from '../../src/index.ts';

const roots: Array<string> = [];

function createProjectRoot(): string {
const root = mkdtempSync(join(tmpdir(), 'void-vite-env-'));
roots.push(root);
writeFileSync(
join(root, 'package.json'),
JSON.stringify({
type: 'module',
dependencies: {},
}),
);
writeFileSync(
join(root, 'void.json'),
JSON.stringify({ worker: { compatibility_date: '2026-02-24' } }),
);
return root;
}

afterEach(() => {
vi.restoreAllMocks();
for (const root of roots.splice(0, roots.length)) {
rmSync(root, { recursive: true, force: true });
}
});

describe('voidPlugin Vite environments', () => {
it('does not claim Vite SSR environments configured by test runners', async () => {
const root = createProjectRoot();
vi.spyOn(process, 'cwd').mockReturnValue(root);

await expect(
resolveConfig(
{
root,
plugins: [voidPlugin()],
environments: {
ssr: {
resolve: {
external: ['node:fs'],
},
},
},
},
'serve',
'test',
),
).resolves.toMatchObject({
environments: {
void_worker: {
build: {
outDir: 'dist/ssr',
},
},
},
});
});
});