Skip to content

Commit 233a0f7

Browse files
authored
fix(deploy): resolve hosted framework bundles with Worker conditions (#592)
Fixes #422 `bundleEntry` bundles the SvelteKit Worker again for `--platform void` with a plain Vite lib build. That build used Vite's default client conditions, so dependencies resolved to their `browser` exports: `@sentry/sveltekit` failed to build, and `postgres` built but picked its Node build. #540 fixed the same problem for `--platform cloudflare` in `prepareBundledBuildOutput`. `bundleEntry` now uses the same conditions: `WORKER_CONDITIONS` plus `production`. `production` is fixed rather than taken from `NODE_ENV`, because a deploy bundle is always a production build. The module rules part of #540 (`.wasm`, `.txt`, `.bin`, …) is not covered here (see #535).
1 parent 6a2024e commit 233a0f7

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

‎packages/void/src/cli/wrapper.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,9 @@ export async function bundleEntry(root: string, entry: string): Promise<WrapperR
523523
await build({
524524
root: tmpDir,
525525
logLevel: 'silent',
526+
// Like prepareBundledBuildOutput: Worker conditions plus the build mode. A deploy
527+
// bundle is always a production build, whatever NODE_ENV says.
528+
resolve: { conditions: [...WORKER_CONDITIONS, 'production'] },
526529
build: {
527530
outDir,
528531
emptyOutDir: true,

‎packages/void/test/unit/wrapper.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { createServer as createPgServer } from 'pglite-server';
2222
import { parseSync } from 'vite';
2323
import { afterEach, beforeAll, describe, expect, it, vi } from 'vite-plus/test';
2424
import {
25+
bundleEntry,
2526
cleanupWrapper,
2627
needsWrapper,
2728
generateWrapper,
@@ -294,6 +295,58 @@ describe('selectBundledEntryFilename', () => {
294295
});
295296
});
296297

298+
describe('bundleEntry', () => {
299+
const roots: Array<string> = [];
300+
afterEach(() => {
301+
for (const root of roots.splice(0)) {
302+
rmSync(root, { recursive: true, force: true });
303+
}
304+
});
305+
306+
it('resolves dependencies with Worker conditions in a production build', async () => {
307+
const root = mkdtempSync(join(tmpdir(), 'void-bundle-entry-conditions-'));
308+
roots.push(root);
309+
writeFileSync(join(root, 'package.json'), JSON.stringify({ type: 'module' }));
310+
const packages = {
311+
dual: {
312+
exports: { workerd: './worker.js', browser: './browser.js', default: './node.js' },
313+
files: {
314+
'worker.js': 'dual-workerd',
315+
'browser.js': 'dual-browser',
316+
'node.js': 'dual-node',
317+
},
318+
},
319+
modes: {
320+
exports: { development: './dev.js', production: './prod.js', default: './dev.js' },
321+
files: { 'dev.js': 'modes-development', 'prod.js': 'modes-production' },
322+
},
323+
};
324+
for (const [name, { exports, files }] of Object.entries(packages)) {
325+
const dir = join(root, 'node_modules', name);
326+
mkdirSync(dir, { recursive: true });
327+
writeFileSync(join(dir, 'package.json'), JSON.stringify({ name, type: 'module', exports }));
328+
for (const [file, value] of Object.entries(files)) {
329+
writeFileSync(join(dir, file), `export const value = '${value}';\n`);
330+
}
331+
}
332+
const entry = join(root, 'build', '_worker.js');
333+
mkdirSync(dirname(entry));
334+
writeFileSync(
335+
entry,
336+
"import { value as target } from 'dual'; import { value as mode } from 'modes';\n" +
337+
'export default { fetch() { return new Response(target + mode) } };\n',
338+
);
339+
340+
const { outputDir, entryFilename } = await bundleEntry(root, entry);
341+
342+
const bundle = readFileSync(join(outputDir, entryFilename), 'utf8');
343+
expect(bundle).toContain('dual-workerd');
344+
expect(bundle).not.toContain('dual-browser');
345+
expect(bundle).toContain('modes-production');
346+
expect(bundle).not.toContain('modes-development');
347+
});
348+
});
349+
297350
describe('cleanupWrapper', () => {
298351
const roots: Array<string> = [];
299352

0 commit comments

Comments
 (0)