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
11 changes: 9 additions & 2 deletions packages/void/src/cli/cf-build-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ async function prepareBundledBuildOutput(options: {
const scratch = mkdtempSync(join(tmpdir(), options.scratchPrefix));
try {
const { build } = await import('vite');
await build({
const result = await build({
configFile: false,
root: options.projectRoot,
publicDir: false,
Expand All @@ -586,9 +586,16 @@ async function prepareBundledBuildOutput(options: {
target: 'es2022',
},
});
// Library mode returns one bundle per format; we only build ES modules.
const entry =
Array.isArray(result) &&
result[0]?.output.find((file) => file.type === 'chunk' && file.isEntry);
if (!entry) {
throw new Error('Vite did not emit a Worker entry chunk.');
}
writeFileSync(
join(scratch, 'wrangler.json'),
JSON.stringify({ ...options.config, main: 'index.js', assets: options.assets }),
JSON.stringify({ ...options.config, main: entry.fileName, assets: options.assets }),
{ mode: 0o600 },
);
return await prepareCfBuildOutput(scratch, options.accountId, undefined, options.projectRoot);
Expand Down
10 changes: 7 additions & 3 deletions packages/void/test/unit/cf-build-output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1175,10 +1175,14 @@ it('keeps Durable Objects, Queue triggers, and Containers in cf prebuilt output'
);
}, 15_000);

it('bundles a generated static Worker and staged assets into prebuilt cf output', async () => {
it.each([
['implicit CommonJS', undefined, 'index.mjs'],
['explicit CommonJS', 'commonjs', 'index.mjs'],
['ES module', 'module', 'index.js'],
])('bundles a generated static Worker and staged assets in %s', async (_label, type, main) => {
const root = mkdtempSync(packageFixturePath(import.meta.dirname, '.tmp-cf-static-'));
roots.push(root);
writeFileSync(join(root, 'package.json'), '{"name":"static-app","type":"module"}');
writeFileSync(join(root, 'package.json'), JSON.stringify({ name: 'static-app', type }));
mkdirSync(join(root, 'node_modules'));
symlinkSync(
fileURLToPath(new URL('../../', import.meta.url)),
Expand Down Expand Up @@ -1227,7 +1231,7 @@ it('bundles a generated static Worker and staged assets into prebuilt cf output'
readFileSync(join(root, 'package.json')),
);
const built = await readBuildOutput(prepared.directory);
expect(built.workers.default.config.manifest?.mainModule).toBe('index.js');
expect(built.workers.default.config.manifest?.mainModule).toBe(main);
expect(built.workers.default.assetsDir).toBeDefined();
expect(prepared.assetsDirectory).toBe(toSlash(assets));
expectCfDryRun(prepared.directory, ['deploy']);
Expand Down