Skip to content

Commit a1bc821

Browse files
TheAlexLichterBrooooooklynclaude
authored
fix(email): convert email-validator for installed Void worker runtimes in dev (#407)
When running `vp dev`, importing `void/email` crashes with `ReferenceError: exports is not defined`, and the crash takes down every page, not just the email route. That's because Void excludes itself from the worker dep optimizer, so its CommonJS dependency `email-validator` reaches workerd without conversion. If Void owns the workerd runtime (no framework, TanStack Start, React Router, vinext), it now adds `include: ['void > email-validator']`. Note: This *might* be also the case with hyperdrive and mysql2, but I couldnt repro this yet. --------- Co-authored-by: LongYinan <lynweklm@gmail.com> Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 6aab28c commit a1bc821

4 files changed

Lines changed: 51 additions & 5 deletions

File tree

‎.github/scripts/packed-consumer.mjs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ try {
272272
join(fixture, 'routes/api/packed.ts'),
273273
`import { defineHandler } from 'void';\nimport { kv } from 'void/kv';\nexport const GET = defineHandler(async () => { await kv.put('packed', 'ok'); return { value: await kv.get('packed') }; });\n`,
274274
);
275+
// void/email pulls a CommonJS dependency into the Worker; only an installed package shows whether dev serves it.
276+
writeFileSync(
277+
join(fixture, 'routes/api/packed-email.ts'),
278+
`import { defineHandler } from 'void';\nimport { sendEmail } from 'void/email';\nexport const GET = defineHandler(async () => { return sendEmail({ from: 'packed@example.com', to: 'inbox@example.com', subject: 'Packed', text: 'ok' }); });\n`,
279+
);
275280
mkdirSync(join(fixture, 'public'), { recursive: true });
276281
writeFileSync(join(fixture, 'public/packed.txt'), label);
277282
const vite = join(fixture, 'node_modules/vite/bin/vite.js');
@@ -335,6 +340,16 @@ if (runtime.manifest.platformVersion !== ${JSON.stringify(version)}) throw new E
335340
const apiBody = await apiResponse.text();
336341
assert.equal(apiResponse.status, 200, `${apiBody}\n${task.failure()}`);
337342
assert.deepEqual(JSON.parse(apiBody), { value: 'ok' });
343+
const emailResponse = await fetch(`${base}/api/packed-email`);
344+
const emailBody = await emailResponse.text();
345+
assert.equal(emailResponse.status, 200, `${emailBody}\n${task.failure()}`);
346+
// Preview runs the built Worker: no dev inbox and no send binding.
347+
const email = JSON.parse(emailBody);
348+
if (mode === 'dev') {
349+
assert.equal(email.ok, true, emailBody);
350+
} else {
351+
assert.equal(email.error?.code, 'BINDING_MISSING', emailBody);
352+
}
338353
if (full && framework === 'react') {
339354
const markdown = await fetch(`${base}/markdown`);
340355
assert.equal(markdown.status, 200, task.failure());
@@ -375,7 +390,7 @@ if (runtime.manifest.platformVersion !== ${JSON.stringify(version)}) throw new E
375390
}
376391
}
377392
console.log(
378-
`Passed packed ${label} (${suite}): init, frozen install, public Vite build/typecheck, assets, KV, runtime integrity${full ? ', dev/preview, hydration/navigation' : ', dev HTTP'}`,
393+
`Passed packed ${label} (${suite}): init, frozen install, public Vite build/typecheck, assets, KV, email, runtime integrity${full ? ', dev/preview, hydration/navigation' : ', dev HTTP'}`,
379394
);
380395
} catch (error) {
381396
failures.push({ fixture: label, error: String(error.stack ?? error) });

‎packages/void/src/index.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,9 @@ export function voidPlugin(options?: {
208208
// graph and therefore another envContext, so request bindings disappear for
209209
// the prebundled copy. Every return path includes this plugin, including
210210
// framework and Node targets that do not all use bindingRuntimePlugin.
211-
const serverRuntimeOptimizerPlugin = createServerRuntimeOptimizerPlugin();
211+
const serverRuntimeOptimizerPlugin = createServerRuntimeOptimizerPlugin(
212+
!isNodeTarget(config.target) && !frameworkOwnsCloudflareRuntime,
213+
);
212214
// Scan queues/websocket routes early so both framework-mode and default-mode
213215
// branches can safely close over these bindings.
214216
const queues = framework ? [] : application.queues;

‎packages/void/src/plugin-worker-environment.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,26 @@ export function createWranglerCompatPersistPlugin(
130130
};
131131
}
132132

133-
export function createServerRuntimeOptimizerPlugin(): Plugin {
133+
export function createServerRuntimeOptimizerPlugin(voidOwnsWorkerRuntime: boolean): Plugin {
134134
const serverRuntimeOptimizerPlugin: Plugin = {
135135
name: 'void:server-runtime-optimizer',
136136
configEnvironment(name) {
137-
if (name !== 'client') {
137+
if (name === 'client') {
138+
return;
139+
}
140+
if (!voidOwnsWorkerRuntime) {
138141
return { optimizeDeps: { exclude: ['void'] } };
139142
}
143+
return {
144+
optimizeDeps: {
145+
exclude: ['void'],
146+
// Excluding void also skips converting its CommonJS dependencies, which workerd cannot
147+
// evaluate: an installed void/email fails with "exports is not defined". Runtimes a
148+
// framework owns stay untouched; Node loads CommonJS itself, and forcing an include
149+
// there turns on SSR prebundling the framework did not ask for.
150+
include: ['void > email-validator'],
151+
},
152+
};
140153
},
141154
};
142155

‎packages/void/test/unit/plugin-dev-asset-routing.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ function createFrameworkProjectRoot(): string {
7272
return root;
7373
}
7474

75+
function createFrameworkOwnedRuntimeProjectRoot(): string {
76+
const root = createProjectRoot();
77+
writeFileSync(
78+
join(root, 'package.json'),
79+
JSON.stringify({ type: 'module', dependencies: { '@analogjs/platform': '^2.0.0' } }),
80+
);
81+
return root;
82+
}
83+
7584
function createNodeProjectRoot(): string {
7685
const root = createProjectRoot();
7786
writeFileSync(
@@ -220,12 +229,19 @@ describe('voidPlugin dev asset routing', () => {
220229
});
221230

222231
it('keeps one Void runtime module graph in core, framework, and Node servers', async () => {
232+
const workerdOptimizer = {
233+
optimizeDeps: { exclude: ['void'], include: ['void > email-validator'] },
234+
};
223235
await expect(
224236
resolveRuntimeOptimizerConfig(createApiOnlyProjectRoot(), 'void_worker'),
225-
).resolves.toEqual({ optimizeDeps: { exclude: ['void'] } });
237+
).resolves.toEqual(workerdOptimizer);
226238
vi.restoreAllMocks();
227239
await expect(
228240
resolveRuntimeOptimizerConfig(createFrameworkProjectRoot(), 'ssr'),
241+
).resolves.toEqual(workerdOptimizer);
242+
vi.restoreAllMocks();
243+
await expect(
244+
resolveRuntimeOptimizerConfig(createFrameworkOwnedRuntimeProjectRoot(), 'ssr'),
229245
).resolves.toEqual({ optimizeDeps: { exclude: ['void'] } });
230246
vi.restoreAllMocks();
231247
await expect(resolveRuntimeOptimizerConfig(createNodeProjectRoot(), 'ssr')).resolves.toEqual({

0 commit comments

Comments
 (0)