@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 135 lines 5.88 KB
Raw
1 import * as NodePath from "node:path";
2 import { fileURLToPath } from "node:url";
3 // Alchemy modifications are licensed under Apache-2.0.
4 // This file includes third-party code; see /THIRD_PARTY_LICENSES.md.
5 /**
6 * The Cloudflare Workers deploy target for `@alchemy.run/frontend-frameworks/waku` — the ONLY
7 * module in this package (besides the bundled `./adapter` fork it selects)
8 * that imports Cloudflare-specific code. The framework half (`./Waku.ts`)
9 * receives this target as a value and stays platform-neutral.
10 *
11 * Default export: a `(config) => WakuTarget` factory, the shape
12 * `resolveDeployTarget` expects from a target module specifier. The config is
13 * the cloudflare vite plugin's options (worker name/bindings/assets,
14 * compatibility date/flags) — exactly what the e2e harness carries at
15 * `target.cloudflare.worker`.
16 */
17 import cloudflareVitePlugin, {
18 type CloudflareVitePluginOptions,
19 } from "@alchemy.run/cloudflare-runtime/vite";
20 import * as Effect from "effect/Effect";
21 import { DeployTargetError, makeDeployTarget } from "../core/index.ts";
22 import { WAKU_SERVER_ENTRY_PATH, type WakuTarget } from "./Waku.ts";
23
24 export type { CloudflareVitePluginOptions } from "@alchemy.run/cloudflare-runtime/vite";
25 export type { WakuTarget, WakuTargetContext } from "./Waku.ts";
26
27 /** Inputs for {@link makeWakuPluginOptions} (exported for testing). */
28 export interface WakuPluginOptionsInputs {
29 /** Absolute project root (a relative user `main` resolves against it). */
30 readonly root: string;
31 /** Directory of the project's installed `waku` package. */
32 readonly wakuDirectory: string;
33 /** User cloudflare plugin options (the target's config). */
34 readonly pluginOptions?: CloudflareVitePluginOptions | undefined;
35 }
36
37 /**
38 * Waku requires `AsyncLocalStorage` on the worker runtime (its server entry
39 * imports `node:async_hooks` at the top level), so a config without any
40 * Node.js compatibility flag would fail at runtime with a module-resolution
41 * error. When the user enables neither `nodejs_als` nor
42 * `nodejs_compat`/`nodejs_compat_v2` (each of which provides ALS), the
43 * target defaults to the minimal `nodejs_als`.
44 */
45 const withNodejsAls = (flags: Array<string> | undefined): Array<string> => {
46 const hasAls = flags?.some(
47 (flag) => flag === "nodejs_als" || flag === "nodejs_compat" || flag === "nodejs_compat_v2",
48 );
49 return hasAls ? [...flags!] : [...(flags ?? []), "nodejs_als"];
50 };
51
52 /**
53 * The cloudflare plugin options for a waku project: user options with `main`
54 * defaulted to waku's rsc worker entry, the rsc/ssr environment topology, and
55 * `compatibilityFlags` defaulted to include `nodejs_als` (required by waku's
56 * server entry) unless the user already enabled ALS or full nodejs_compat.
57 *
58 * An explicit `main` is mandatory either way: waku's rsc environment declares
59 * two rolldown inputs (`index` + `build`) while the dev plugin asserts exactly
60 * one entry. A USER `main` (the deploy target's user-entry seam — a worker
61 * entry that wraps `virtual:waku/server-entry` and re-exports Durable Object
62 * classes) takes precedence over waku's own entry and is resolved against the
63 * project root; without one, waku's rsc server entry is pinned as before.
64 */
65 export const makeWakuPluginOptions = (
66 inputs: WakuPluginOptionsInputs,
67 ): CloudflareVitePluginOptions => {
68 const userMain = inputs.pluginOptions?.main;
69 return {
70 ...inputs.pluginOptions,
71 main:
72 userMain !== undefined
73 ? NodePath.isAbsolute(userMain)
74 ? userMain
75 : NodePath.resolve(inputs.root, userMain)
76 : NodePath.join(inputs.wakuDirectory, WAKU_SERVER_ENTRY_PATH),
77 viteEnvironments: { entry: "rsc", children: ["ssr"] },
78 compatibilityFlags: withNodejsAls(inputs.pluginOptions?.compatibilityFlags),
79 };
80 };
81
82 /**
83 * Build the Cloudflare deploy target for waku:
84 *
85 * - `adapter` — this package's wrangler-free fork of waku's cloudflare
86 * adapter (`@alchemy.run/frontend-frameworks/waku/adapter`): behavior-identical to upstream
87 * except `buildEnhancers: []`, dropping the sole wrangler-file writer.
88 * - `vitePlugins` — `@alchemy.run/cloudflare-runtime/vite` with `main`
89 * set to the user's own worker entry when the config carries one (the
90 * user-entry seam), or pinned to waku's rsc worker entry otherwise, plus
91 * the `{ entry: "rsc", children: ["ssr"] }` environment topology. Injected
92 * inside waku's `vite.plugins` (before waku's own plugins), so in dev the
93 * rsc environment runs in workerd and the workerd proxy middleware
94 * registers ahead of waku's Node request bridge.
95 * - `entry` — surfaces the config's `main` as the generic user-entry
96 * carriage, so the platform-neutral framework half can pin the built
97 * user-entry chunk as `serverModules[0]`.
98 * - `bundle` — the workerd resolve conditions and `cloudflare:` externals for
99 * any pass that bundles server code for this target.
100 */
101 export const makeWakuCloudflareTarget = (
102 config?: CloudflareVitePluginOptions,
103 ): WakuTarget<CloudflareVitePluginOptions | undefined> =>
104 makeDeployTarget({
105 platform: "cloudflare",
106 config,
107 entry: config?.main !== undefined ? { main: config.main } : undefined,
108 bundle: {
109 conditions: ["workerd", "worker", "module", "browser"],
110 external: ["cloudflare:"],
111 },
112 adapter: () =>
113 Effect.try({
114 try: () =>
115 fileURLToPath(import.meta.resolve("@alchemy.run/frontend-frameworks/waku/adapter")),
116 catch: (cause) =>
117 new DeployTargetError({
118 platform: "cloudflare",
119 message: "Failed to resolve the @alchemy.run/frontend-frameworks/waku adapter module",
120 cause,
121 }),
122 }),
123 vitePlugins: (context) =>
124 Effect.sync(() => [
125 cloudflareVitePlugin(
126 makeWakuPluginOptions({
127 root: context.root,
128 wakuDirectory: context.wakuDirectory,
129 pluginOptions: config,
130 }),
131 ),
132 ]),
133 });
134
135 export default makeWakuCloudflareTarget;