| 1 | /** |
| 2 | * Cloudflare Workers deploy target for Octane. |
| 3 | * |
| 4 | * Alchemy preserves the native Octane compiler and client hooks, builds the |
| 5 | * server for Workers, and generates `dist/server/worker.js`. No adapter is |
| 6 | * required in `octane.config.ts`; existing Cloudflare adapters remain valid. |
| 7 | * |
| 8 | * Static assets are served first. Keep `notFoundHandling` unset so missing |
| 9 | * assets reach server-rendered routes. Cloudflare bindings are available in |
| 10 | * production and preview; native Octane HMR does not supply context.platform. |
| 11 | */ |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import { DeployTargetError, makeDeployTarget } from "../core/index.ts"; |
| 14 | import { buildCloudflare } from "./CloudflareBuild.ts"; |
| 15 | import type { OctaneTarget, OctaneTargetConfig } from "./Octane.ts"; |
| 16 | |
| 17 | /** The `adapter.name` Octane's Cloudflare adapter declares. */ |
| 18 | export const ADAPTER_NAME = "cloudflare"; |
| 19 | |
| 20 | /** The npm package providing Octane's Cloudflare adapter. */ |
| 21 | export const ADAPTER_PACKAGE = "@octanejs/adapter-cloudflare"; |
| 22 | |
| 23 | /** The Worker entry the adapter emits into the server output directory. */ |
| 24 | export const SERVER_ENTRY_FILE_NAME = "worker.js"; |
| 25 | |
| 26 | /** |
| 27 | * Create the Cloudflare Workers {@link OctaneTarget}. See the module doc for |
| 28 | * the seams. The config's `compatibilityDate`/`compatibilityFlags` are |
| 29 | * carried for serve/deploy consumers; the build reuses Octane's native tooling. |
| 30 | */ |
| 31 | export const makeCloudflareTarget = ( |
| 32 | config: OctaneTargetConfig = {}, |
| 33 | ): OctaneTarget => |
| 34 | makeDeployTarget({ |
| 35 | platform: "cloudflare", |
| 36 | config, |
| 37 | bundle: { |
| 38 | conditions: ["workerd", "worker", "module", "browser"], |
| 39 | external: ["cloudflare:"], |
| 40 | }, |
| 41 | adapterName: ADAPTER_NAME, |
| 42 | adapterPackage: ADAPTER_PACKAGE, |
| 43 | serverEntryFileName: SERVER_ENTRY_FILE_NAME, |
| 44 | build: (context) => |
| 45 | buildCloudflare(context.root).pipe( |
| 46 | Effect.mapError( |
| 47 | (cause) => |
| 48 | new DeployTargetError({ |
| 49 | platform: "cloudflare", |
| 50 | message: cause.message, |
| 51 | cause, |
| 52 | }), |
| 53 | ), |
| 54 | ), |
| 55 | }); |
| 56 | |
| 57 | /** |
| 58 | * The deploy-target module contract (`resolveDeployTarget` accepts the |
| 59 | * default export — or the named `target` export — as a value or factory). |
| 60 | */ |
| 61 | export const target = makeCloudflareTarget; |
| 62 | |
| 63 | export default makeCloudflareTarget; |