Skip to content

Commit 2ac7b1d

Browse files
fix: avoid ssr environment collision in void plugin (#188)
1 parent 3c05d08 commit 2ac7b1d

2 files changed

Lines changed: 89 additions & 1 deletion

File tree

‎packages/void/src/index.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ function hasBinding(list: ReadonlyArray<{ binding: string }> | undefined, name:
332332
const CLOUDFLARE_BUILTIN_IMPORT = /^cloudflare:/;
333333
const SANDBOX_SDK_IMPORT = '@cloudflare/sandbox';
334334
const SANDBOX_SDK_IMPORT_FILTER = /^@cloudflare\/sandbox$/;
335+
const DEFAULT_WORKER_ENVIRONMENT_NAME = 'void_worker';
336+
const DEFAULT_WORKER_OUT_DIR = 'dist/ssr';
335337

336338
function resolveSandboxSdkEntry(): string {
337339
return fileURLToPath(import.meta.resolve(SANDBOX_SDK_IMPORT));
@@ -356,6 +358,26 @@ function createCloudflareBuiltinsExternalPlugin(): Plugin {
356358
};
357359
}
358360

361+
function createDefaultWorkerEnvironmentPlugin(): Plugin {
362+
return {
363+
name: 'void:default-worker-environment',
364+
config(config) {
365+
const currentEnvironment = config.environments?.[DEFAULT_WORKER_ENVIRONMENT_NAME];
366+
return {
367+
environments: {
368+
[DEFAULT_WORKER_ENVIRONMENT_NAME]: {
369+
...currentEnvironment,
370+
build: {
371+
...currentEnvironment?.build,
372+
outDir: currentEnvironment?.build?.outDir ?? DEFAULT_WORKER_OUT_DIR,
373+
},
374+
},
375+
},
376+
};
377+
},
378+
};
379+
}
380+
359381
function createSandboxSdkResolvePlugin(): Plugin {
360382
let resolved: string | undefined;
361383

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

13341356
return [
13351357
createCloudflareBuiltinsExternalPlugin(),
1358+
createDefaultWorkerEnvironmentPlugin(),
13361359
restartPlugin(),
13371360
bindingWatchPlugin,
13381361
envPlugin,
@@ -1369,7 +1392,7 @@ export function voidPlugin(options?: {
13691392
...inspectorPortOption,
13701393
persistState: { path: options?.persistTo ?? outDir },
13711394
auxiliaryWorkers: options?.auxiliaryWorkers,
1372-
viteEnvironment: { name: 'ssr' },
1395+
viteEnvironment: { name: DEFAULT_WORKER_ENVIRONMENT_NAME },
13731396
config: mergeBindings,
13741397
}),
13751398
];
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
2+
import { tmpdir } from 'node:os';
3+
import { join } from 'pathe';
4+
import { resolveConfig } from 'vite';
5+
import { afterEach, describe, expect, it, vi } from 'vitest';
6+
import { voidPlugin } from '../../src/index.ts';
7+
8+
const roots: Array<string> = [];
9+
10+
function createProjectRoot(): string {
11+
const root = mkdtempSync(join(tmpdir(), 'void-vite-env-'));
12+
roots.push(root);
13+
writeFileSync(
14+
join(root, 'package.json'),
15+
JSON.stringify({
16+
type: 'module',
17+
dependencies: {},
18+
}),
19+
);
20+
writeFileSync(
21+
join(root, 'void.json'),
22+
JSON.stringify({ worker: { compatibility_date: '2026-02-24' } }),
23+
);
24+
return root;
25+
}
26+
27+
afterEach(() => {
28+
vi.restoreAllMocks();
29+
for (const root of roots.splice(0, roots.length)) {
30+
rmSync(root, { recursive: true, force: true });
31+
}
32+
});
33+
34+
describe('voidPlugin Vite environments', () => {
35+
it('does not claim Vite SSR environments configured by test runners', async () => {
36+
const root = createProjectRoot();
37+
vi.spyOn(process, 'cwd').mockReturnValue(root);
38+
39+
await expect(
40+
resolveConfig(
41+
{
42+
root,
43+
plugins: [voidPlugin()],
44+
environments: {
45+
ssr: {
46+
resolve: {
47+
external: ['node:fs'],
48+
},
49+
},
50+
},
51+
},
52+
'serve',
53+
'test',
54+
),
55+
).resolves.toMatchObject({
56+
environments: {
57+
void_worker: {
58+
build: {
59+
outDir: 'dist/ssr',
60+
},
61+
},
62+
},
63+
});
64+
});
65+
});

0 commit comments

Comments
 (0)