The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * `@alchemy.run/frontend-frameworks/nuxt/node` — the Node container deploy |
| 3 | * target for the Nuxt integration. |
| 4 | * |
| 5 | * Nuxt's Node story is nitro's `node` (node-listener) preset: |
| 6 | * `.output/server` is a self-contained Node ESM bundle whose entry exports |
| 7 | * a Node `(req, res)` `handler`, and `.output/public` holds the static |
| 8 | * assets. The finishing pass writes a Node HTTP program that serves |
| 9 | * `clientDirectory` first, then falls through to that handler on `PORT` |
| 10 | * (default 3000), and answers `GET /health`. |
| 11 | * |
| 12 | * - **`nitroPreset`** — `"node"`, enforced as the last word on the |
| 13 | * resolved nitro config. A user `nitro.preset` mismatch fails the build. |
| 14 | * - **`entry`** — surfaces `config.main` as the generic user-entry carriage. |
| 15 | * - **`bundle`** — Node resolve conditions (no `workerd`, no `@aws-sdk/`). |
| 16 | */ |
| 17 | import * as Effect from "effect/Effect"; |
| 18 | import * as Path from "effect/Path"; |
| 19 | import { runBuildChild } from "../core/BuildChild.ts"; |
| 20 | import { DeployTargetError, makeDeployTarget } from "../core/index.ts"; |
| 21 | import { |
| 22 | NODE_BUNDLE_CONDITIONS, |
| 23 | NODE_SERVE_ENTRY_FILE_NAME, |
| 24 | relativeClientDirExpression, |
| 25 | writeNodeServeEntry, |
| 26 | } from "../core/NodeServe.ts"; |
| 27 | import { make, type NuxtTarget, type NuxtTargetConfig } from "./Nuxt.ts"; |
| 28 | |
| 29 | /** The nitro deployment preset this target builds with. */ |
| 30 | export const NITRO_PRESET = "node"; |
| 31 | |
| 32 | /** |
| 33 | * The importable specifier of nitro's node-listener runtime handler — the |
| 34 | * module a USER entry re-exports to wrap the framework's handler. |
| 35 | */ |
| 36 | export const NITRO_HANDLER_SPECIFIER = "nitropack/presets/node/runtime/node-listener"; |
| 37 | |
| 38 | export interface NuxtNodeTargetConfig extends NuxtTargetConfig {} |
| 39 | |
| 40 | const fail = (message: string, cause?: unknown) => |
| 41 | new DeployTargetError({ platform: "node", message, cause }); |
| 42 | |
| 43 | const makeNodeAdapterTarget = (config: NuxtNodeTargetConfig = {}): NuxtTarget => |
| 44 | makeDeployTarget({ |
| 45 | platform: "node", |
| 46 | config, |
| 47 | bundle: { |
| 48 | conditions: [...NODE_BUNDLE_CONDITIONS], |
| 49 | }, |
| 50 | entry: config.main !== undefined ? { main: config.main } : undefined, |
| 51 | nitroPreset: NITRO_PRESET, |
| 52 | configureNitro: (_nitroConfig, _context) => { |
| 53 | // The user entry (context.entry) is deliberately NOT wired here — |
| 54 | // the framework package sets it on the nitro INSTANCE at `nitro:init` |
| 55 | // (a config-level entry would leak into the prerenderer's Node-preset |
| 56 | // clone). See the matching note in ./cloudflare.ts. |
| 57 | }, |
| 58 | finish: (output) => |
| 59 | Effect.gen(function* () { |
| 60 | const path = yield* Path.Path; |
| 61 | if (output.distDirectory === undefined || output.clientDirectory === undefined) { |
| 62 | return yield* fail( |
| 63 | "The Nuxt build produced no .output directories for the Node serve entry", |
| 64 | ); |
| 65 | } |
| 66 | const servePath = path.join(output.distDirectory, "server", NODE_SERVE_ENTRY_FILE_NAME); |
| 67 | return yield* writeNodeServeEntry({ |
| 68 | output, |
| 69 | servePath, |
| 70 | serveModuleName: path.join("server", NODE_SERVE_ENTRY_FILE_NAME).replaceAll("\\", "/"), |
| 71 | clientDirExpression: relativeClientDirExpression(servePath, output.clientDirectory), |
| 72 | handler: { |
| 73 | kind: "node", |
| 74 | imports: `import { handler } from "./index.mjs";`, |
| 75 | expr: "handler", |
| 76 | }, |
| 77 | platform: "node", |
| 78 | }); |
| 79 | }), |
| 80 | }); |
| 81 | |
| 82 | export interface NuxtNodeBuildChildConfig { |
| 83 | readonly rootDir: string; |
| 84 | readonly config: NuxtNodeTargetConfig; |
| 85 | } |
| 86 | |
| 87 | export const buildInChild = (config: NuxtNodeBuildChildConfig) => |
| 88 | Effect.gen(function* () { |
| 89 | const framework = yield* make({ |
| 90 | root: config.rootDir, |
| 91 | target: makeNodeAdapterTarget(config.config), |
| 92 | compatibilityDate: config.config.compatibilityDate, |
| 93 | compatibilityFlags: config.config.compatibilityFlags, |
| 94 | main: config.config.main, |
| 95 | nuxt: config.config.nuxt, |
| 96 | }); |
| 97 | return yield* framework.build({ root: config.rootDir }); |
| 98 | }); |
| 99 | |
| 100 | export const makeNodeTarget = (config: NuxtNodeTargetConfig = {}): NuxtTarget => ({ |
| 101 | ...makeNodeAdapterTarget(config), |
| 102 | build: (context) => |
| 103 | runBuildChild({ |
| 104 | module: import.meta.url, |
| 105 | rootDir: context.root, |
| 106 | env: context.env, |
| 107 | framework: "nuxt", |
| 108 | config: { |
| 109 | rootDir: context.root, |
| 110 | config, |
| 111 | } satisfies NuxtNodeBuildChildConfig, |
| 112 | }).pipe(Effect.mapError((error) => fail(error.message, error.cause))), |
| 113 | }); |
| 114 | |
| 115 | export const target = makeNodeTarget; |
| 116 | |
| 117 | export default makeNodeTarget; |