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

main
ts 135 lines 4.83 KB
Raw
1 export interface BasePluginOptions {
2 /**
3 * The main entry point to use.
4 * Defaults to the one from your Rolldown configuration.
5 * @default undefined
6 */
7 main?: string;
8 /**
9 * The compatibility date to use. This is optional, but should be defined to avoid unexpected behavior.
10 * @default undefined
11 */
12 compatibilityDate?: string;
13 /**
14 * The compatibility flags to enable.
15 * @default []
16 * @example
17 * ```ts
18 * cloudflare({ compatibilityDate: "2026-08-31" });
19 * ```
20 */
21 compatibilityFlags?: Array<string>;
22 /**
23 * Whether external CommonJS `require()` calls should be rewritten as ESM.
24 * Internal single-module Workers disable this because they cannot carry the
25 * generated helper chunk; user bundles should leave it enabled.
26 * @default true
27 */
28 externalRequire?: boolean;
29 /**
30 * The exports to include in the bundle.
31 * By default, all exports are included. However, if you only want to include certain exports, you can use this option.
32 * @example
33 * ```ts
34 * cloudflare({ exports: ["default"] });
35 * ```
36 */
37 exports?: Array<string>;
38 /**
39 * Which Vite environment hosts the Worker, and any child environments it
40 * loads at runtime. Defaults to the single `ssr` environment.
41 *
42 * `@vitejs/plugin-rsc` apps run the Worker in the `rsc` environment (resolved
43 * with the `react-server` condition) and load the `ssr` environment from it,
44 * so they set `{ name: "rsc", childEnvironments: ["ssr"] }`. Every named
45 * environment is given the Worker treatment (workerd resolve conditions,
46 * dependency pre-bundling) and a module runner in dev.
47 *
48 * @default { entry: "ssr", children: [] }
49 */
50 viteEnvironments?: {
51 entry?: string;
52 children?: Array<string>;
53 };
54 /**
55 * Vite environment names that must be left untouched by the Cloudflare
56 * plugins. Frameworks can add Node-side server environments to the same dev
57 * server or builder (e.g. Astro's `astro` and `prerender` environments), and
58 * applying the Worker treatment (unenv polyfills, workerd resolve
59 * conditions, dependency pre-bundling) to them breaks their Node runtime.
60 *
61 * The `client` environment is always excluded and does not need to be
62 * listed here.
63 *
64 * @default []
65 * @example
66 * ```ts
67 * cloudflare({ viteEnvironments: { entry: "ssr" }, skipEnvironments: ["astro", "prerender"] });
68 * ```
69 */
70 skipEnvironments?: Array<string>;
71 /**
72 * Extra rules for imports emitted as separate Worker modules, checked
73 * before the built-in rules (`.wasm` → CompiledWasm, `.bin` → Data,
74 * `.txt`/`.html`/`.sql` → Text). `test` receives the resolved file path.
75 *
76 * @default []
77 * @example
78 * ```ts
79 * cloudflare({ moduleRules: [{ type: "Text", test: (path) => path.endsWith(".md") }] });
80 * ```
81 */
82 moduleRules?: Array<AdditionalModuleRule>;
83 }
84
85 /** A rule that emits matching imports as a separate Worker module. */
86 export interface AdditionalModuleRule {
87 /**
88 * The module type. Without one, the built-in rules decide by extension
89 * and other files are Data.
90 */
91 type?: "CompiledWasm" | "Data" | "Text" | undefined;
92 /** Whether the resolved file path belongs to this rule. */
93 test: (path: string) => boolean;
94 }
95
96 /**
97 * Whether the named Vite environment must be left untouched by the Cloudflare
98 * plugins — the browser `client` environment, plus anything the user listed in
99 * {@link BasePluginOptions.skipEnvironments}.
100 */
101 export const isSkippedEnvironment = (options: BasePluginOptions, name: string): boolean =>
102 name === "client" || (options.skipEnvironments?.includes(name) ?? false);
103
104 export const parseViteEnvironments = (options: BasePluginOptions): [string, ...Array<string>] => {
105 const entry = options.viteEnvironments?.entry ?? "ssr";
106 if (entry === "client") {
107 throw new Error(
108 'The "client" environment cannot be used as a worker environment because it is reserved for the browser.',
109 );
110 }
111 const children = (options.viteEnvironments?.children ?? []).map((name, index, self) => {
112 if (name === "client") {
113 throw new Error(
114 'The "client" environment cannot be used as a worker environment because it is reserved for the browser.',
115 );
116 } else if (self.indexOf(name) !== index) {
117 throw new Error(
118 `The name "${name}" appears more than once in the Vite environment list. Worker environment names must be unique.`,
119 );
120 } else if (name === entry) {
121 throw new Error(
122 `The child environment "${name}" cannot have the same name as the entry environment "${entry}".`,
123 );
124 }
125 return name;
126 });
127 for (const name of options.skipEnvironments ?? []) {
128 if (name === entry || children.includes(name)) {
129 throw new Error(
130 `The environment "${name}" cannot be both a worker environment and listed in "skipEnvironments".`,
131 );
132 }
133 }
134 return [entry, ...children];
135 };