@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 154 lines 6.46 KB
Raw
1 import * as NodePath from "node:path";
2 /**
3 * The Cloudflare Workers deploy target for `@alchemy.run/frontend-frameworks/astro`
4 * (`@alchemy.run/frontend-frameworks/astro/cloudflare`).
5 *
6 * This module is the ONLY seam between the Astro framework module and
7 * Cloudflare: it owns the wrangler-free `@astrojs/cloudflare` fork (the
8 * integration over `@alchemy.run/cloudflare-runtime/vite`, its vendored
9 * server entrypoints, workerd dev via the vite module runner) and exposes it
10 * through the `AstroTarget` contract. The framework module
11 * (`Astro.ts`/`index.ts`) never imports anything Cloudflare-specific — it
12 * receives this target as a value (or resolves this module by specifier when
13 * no target is given).
14 *
15 * The default export is the target factory the framework's target resolution
16 * applies to the caller's config (`resolveDeployTarget`); the named exports
17 * re-expose the underlying integration surface for direct/advanced use.
18 */
19 import type { CloudflareVitePluginOptions } from "@alchemy.run/cloudflare-runtime/vite";
20 import * as Effect from "effect/Effect";
21 import { makeDeployTarget } from "../core/index.ts";
22 import { distilledCloudflare } from "./integration.ts";
23 import type { AstroTarget } from "./Target.ts";
24
25 /**
26 * Cloudflare target configuration. Matches the e2e harness's target-scoped
27 * carriage (`target.cloudflare.worker`): `worker` is the full
28 * `CloudflareVitePluginOptions` shape (compatibility date/flags, worker
29 * name/bindings/assets, runtime context).
30 */
31 export interface AstroCloudflareConfig {
32 /**
33 * Options forwarded to `@alchemy.run/cloudflare-runtime/vite`
34 * (compatibility date/flags, worker name/bindings/assets, runtime
35 * context). `viteEnvironments` and Astro's node-side environments in
36 * `skipEnvironments` are managed by the integration; `main` defaults to
37 * the vendored server entrypoint (see `makeIntegrationPluginOptions`).
38 */
39 readonly worker?: CloudflareVitePluginOptions | undefined;
40 /**
41 * Which runtime prerenders static pages at build time: `'workerd'`
42 * (default) renders them inside the production runtime via the built
43 * `prerender` environment worker; `'node'` falls back to Astro's stock
44 * node prerenderer. See `DistilledCloudflareOptions.prerenderEnvironment`.
45 * @default "workerd"
46 */
47 readonly prerenderEnvironment?: "workerd" | "node" | undefined;
48 /**
49 * The name of the KV binding injected into Astro's session config when
50 * present on the Worker env.
51 * @default "SESSION"
52 */
53 readonly sessionKVBindingName?: string | undefined;
54 /**
55 * Zero-config sessions: default Astro's session driver to Cloudflare KV
56 * (binding `sessionKVBindingName`) when none is configured. `false` leaves
57 * the session config untouched.
58 * @default true
59 */
60 readonly sessions?: boolean | undefined;
61 /**
62 * Auto-inject an in-memory local KV namespace for the session binding: in
63 * dev on the dev worker, at build time on the workerd prerender worker
64 * (session-touching pages can prerender without the real namespace).
65 * Disable when the worker bindings already carry a KV binding with that
66 * name.
67 * @default true
68 */
69 readonly sessionDevKV?: boolean | undefined;
70 }
71
72 export interface AstroCloudflareTarget extends AstroTarget<AstroCloudflareConfig> {}
73
74 /**
75 * Build the Cloudflare Workers deploy target for Astro.
76 *
77 * - `integration` — the wrangler-free `@astrojs/cloudflare` fork over
78 * `@alchemy.run/cloudflare-runtime/vite` (entry env `ssr`, node-side
79 * `astro` environment skipped, workerd prerendering by default with a
80 * `'node'` fallback, passthrough image service).
81 * - `bundle` — workerd resolve conditions + `cloudflare:` externals
82 * (informational for Astro: the integration configures the bundler itself).
83 */
84 export const target = (config: AstroCloudflareConfig = {}): AstroCloudflareTarget => {
85 // Written by the integration's `astro:config:done` (the client directory
86 // before the `base !== "/"` remap nests it); read by `finish` below.
87 let originalClientDir: string | undefined;
88 // The resolved build output mode from `astro:config:done`: `"static"`
89 // means every route is prerendered, so the deploy must be assets-only.
90 let buildOutput: "static" | "server" | undefined;
91 return makeDeployTarget({
92 platform: "cloudflare",
93 config,
94 bundle: {
95 conditions: ["workerd", "worker", "module", "browser"],
96 external: ["cloudflare:"],
97 },
98 integration: () =>
99 distilledCloudflare({
100 vite: config.worker,
101 prerenderEnvironment: config.prerenderEnvironment,
102 sessionKVBindingName: config.sessionKVBindingName,
103 sessions: config.sessions,
104 sessionDevKV: config.sessionDevKV,
105 onOriginalClientDir: (dir) => {
106 // Normalize (drop the directory-URL trailing slash) so the
107 // comparison with the collector's `resolve`d path is exact.
108 originalClientDir = NodePath.resolve(dir);
109 },
110 onBuildOutput: (mode) => {
111 buildOutput = mode;
112 },
113 }),
114 // Two in-memory equivalents of upstream's emitted-wrangler.json patches:
115 //
116 // - With `base !== "/"` the client build is nested under the base
117 // (`dist/client/<base>/`), so the collector captures the nested
118 // directory. The directory that must be served at the URL root is the
119 // original one (upstream: the wrangler.json `assets.directory` patch).
120 // - A fully-static build (`buildOutput === "static"` at
121 // `astro:config:done`: every route prerendered) deploys ASSETS-ONLY:
122 // the SSR entry astro bundled for prerendering is dropped from the
123 // output (`serverModules: undefined`) and the client directory carries
124 // all prerendered HTML, `404.html` included.
125 finish: (output) => {
126 const clientDirectory =
127 originalClientDir !== undefined &&
128 output.clientDirectory !== undefined &&
129 output.clientDirectory !== originalClientDir
130 ? originalClientDir
131 : output.clientDirectory;
132 return Effect.succeed(
133 buildOutput === "static"
134 ? { ...output, clientDirectory, serverModules: undefined }
135 : clientDirectory !== output.clientDirectory
136 ? { ...output, clientDirectory }
137 : output,
138 );
139 },
140 });
141 };
142
143 export default target;
144
145 export {
146 distilledCloudflare,
147 IMAGE_PASSTHROUGH_ENDPOINT,
148 makeIntegrationPluginOptions,
149 SERVER_ENTRYPOINT,
150 usesCloudflareKVSessionDriver,
151 withDevSessionKv,
152 withPrerenderSessionKv,
153 type DistilledCloudflareOptions,
154 } from "./integration.ts";