The best way to control Claude Code, Codex and more from any computer or phone

main
ts 70 lines 1.86 KB
Raw
1 import path from "node:path";
2 import type {
3 InputOptions,
4 OutputChunk,
5 OutputOptions,
6 RolldownOutput,
7 RolldownPluginOption,
8 } from "rolldown";
9 import { rolldown } from "rolldown";
10 import type { RolldownPluginOptions } from "../../plugin.ts";
11 import cloudflare from "../../plugin.ts";
12 import { getEntryChunk } from "./output.ts";
13
14 const DEFAULT_PLUGIN_OPTIONS: RolldownPluginOptions = {
15 compatibilityDate: "2025-07-01",
16 };
17
18 interface BuildFixtureOptions {
19 fixture: string;
20 pluginOptions?: RolldownPluginOptions;
21 plugins?: Array<RolldownPluginOption>;
22 inputOptions?: Omit<InputOptions, "input" | "plugins">;
23 generateOptions?: Omit<OutputOptions, "file" | "format" | "sourcemap">;
24 }
25
26 export interface BuiltFixture {
27 fixture: string;
28 output: RolldownOutput;
29 entry: OutputChunk;
30 }
31
32 export async function buildFixture(options: BuildFixtureOptions): Promise<BuiltFixture> {
33 const fixture = normalizeFixturePath(options.fixture);
34 const bundle = await rolldown({
35 input: fixture,
36 ...options.inputOptions,
37 plugins: [
38 ...(options.plugins ?? []),
39 cloudflare(options.pluginOptions ?? DEFAULT_PLUGIN_OPTIONS),
40 ],
41 });
42
43 try {
44 const output = await bundle.write({
45 file: defaultOutputPath(fixture),
46 format: "esm",
47 sourcemap: true,
48 ...options.generateOptions,
49 });
50 return {
51 fixture,
52 output,
53 entry: getEntryChunk(output),
54 };
55 } finally {
56 await bundle.close();
57 }
58 }
59
60 function normalizeFixturePath(fixture: string): string {
61 return path.isAbsolute(fixture) ? fixture : path.join("test", "fixtures", fixture);
62 }
63
64 function defaultOutputPath(fixture: string): string {
65 const fixtureName =
66 path.basename(path.dirname(fixture)) === "fixtures"
67 ? path.basename(fixture, path.extname(fixture))
68 : path.basename(path.dirname(fixture));
69 return path.join("out", fixtureName, "index.js");
70 }