@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 109 lines 5.17 KB
Raw
1 /**
2 * `@alchemy.run/frontend-frameworks/nuxt/cloudflare` — the Cloudflare Workers deploy target
3 * for `@alchemy.run/frontend-frameworks/nuxt`.
4 *
5 * Nuxt 4's Cloudflare story is nitro's `cloudflare_module` preset: the
6 * `.output` it produces is already self-contained workerd ESM
7 * (`.output/server/index.mjs` + chunks, `.output/public` static assets with
8 * prerendered pages and `_headers` merged in), so this target needs no
9 * finishing pass. What it owns:
10 *
11 * - **`nitroPreset`** — `"cloudflare_module"`, enforced as the last word on
12 * the resolved nitro config.
13 * - **`configureNitro`** — wrangler-free doctrine: `cloudflare.deployConfig:
14 * false` (nitro must never write a `wrangler.json` into user projects) and
15 * `cloudflare.nodeCompat: true` by default (without a wrangler config on
16 * disk nitro would otherwise skip its hybrid workerd node-compat; the
17 * deployed worker enables the `nodejs_compat` flag to match). Also wires
18 * the user-entry seam: a configured `main` becomes nitro's `entry`.
19 * - **`entry`** — surfaces `config.main` as the generic user-entry carriage.
20 * Nitro's entry module is the exports seam: the emitted worker exports are
21 * exactly the entry module's exports, so a user entry that re-exports
22 * nitro's runtime handler (`nitropack/presets/cloudflare/runtime/cloudflare-module`)
23 * and adds its own classes gets Durable Objects et al. onto the same
24 * worker.
25 * - **`bundle`** — workerd resolve conditions and the `cloudflare:`
26 * externals, for callers that post-process server code.
27 * - **`devPlatform`** — the wrangler-free dev bridge: opens
28 * cloudflare-runtime's platform proxy (a local workerd instance hosting
29 * the configured binding hooks) and injects a nitro plugin that serves
30 * `event.context.cf` / `event.context.waitUntil` /
31 * `event.context.cloudflare = { request, env, context }` inside nitro's
32 * dev SSR worker thread (see `./dev/host.ts`).
33 */
34 import { makeDeployTarget } from "../core/index.ts";
35 import { makeCloudflareDevPlatform } from "./dev/host.ts";
36 import type { NuxtTarget, NuxtTargetConfig } from "./Nuxt.ts";
37
38 /** The nitro deployment preset this target builds with. */
39 export const NITRO_PRESET = "cloudflare_module";
40
41 /**
42 * The importable specifier of nitro's cloudflare-module runtime handler — the
43 * module a USER worker entry re-exports to wrap the framework's fetch
44 * handler (the "wrappable handler" half of the user-entry seam):
45 *
46 * ```ts
47 * // worker-entry.mjs
48 * import nitroHandler from "nitropack/presets/cloudflare/runtime/cloudflare-module";
49 * export { MyDurableObject } from "./durable-object.mjs";
50 * export default nitroHandler;
51 * ```
52 */
53 export const NITRO_HANDLER_SPECIFIER = "nitropack/presets/cloudflare/runtime/cloudflare-module";
54
55 /**
56 * Create the Cloudflare Workers {@link NuxtTarget}. See the module doc for
57 * the seams. The config's `compatibilityDate`/`compatibilityFlags` are
58 * carried for serve/deploy consumers; the build itself only needs
59 * `nodeCompat` (default `true`) and the optional user `main`.
60 */
61 export const makeCloudflareTarget = (config: NuxtTargetConfig = {}): NuxtTarget =>
62 makeDeployTarget({
63 platform: "cloudflare",
64 config,
65 bundle: {
66 conditions: ["workerd", "worker", "module", "browser"],
67 external: ["cloudflare:"],
68 },
69 entry: config.main !== undefined ? { main: config.main } : undefined,
70 nitroPreset: NITRO_PRESET,
71 configureNitro: (nitroConfig, _context) => {
72 const cloudflare =
73 nitroConfig.cloudflare !== null && typeof nitroConfig.cloudflare === "object"
74 ? nitroConfig.cloudflare
75 : {};
76 nitroConfig.cloudflare = {
77 ...cloudflare,
78 // Wrangler-free: never write a wrangler.json into the user project.
79 deployConfig: false,
80 // Without a wrangler config on disk, nitro only enables its hybrid
81 // workerd node-compat when told to. The deployed worker enables the
82 // `nodejs_compat` compatibility flag to match.
83 nodeCompat: config.nodeCompat ?? true,
84 };
85 // NOTE: the user entry (context.entry) is deliberately NOT wired here.
86 // The framework package sets it on the nitro INSTANCE at `nitro:init`:
87 // a config-level entry would leak into the prerenderer's Node-preset
88 // clone (it copies `options._config`) and a workerd-flavored entry
89 // (importing `cloudflare:` modules) cannot load there.
90 },
91 // Dev: `event.context.cloudflare` served through cloudflare-runtime's
92 // platform proxy (workerd hosting the binding hooks), bridged into
93 // nitro's dev SSR worker thread by the shipped nitro plugin. Note the
94 // proxy cannot host user worker-entry classes (Durable Objects declared
95 // via the entry/exports seam only exist in the production build), so
96 // DO namespace bindings are not servable in dev yet.
97 devPlatform: makeCloudflareDevPlatform({
98 compatibilityDate: config.compatibilityDate,
99 compatibilityFlags: config.compatibilityFlags,
100 }),
101 });
102
103 /**
104 * The deploy-target module contract (`resolveDeployTarget` accepts the
105 * default export — or the named `target` export — as a value or factory).
106 */
107 export const target = makeCloudflareTarget;
108
109 export default makeCloudflareTarget;