@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, 04:45 UTC

main
ts 104 lines 3.67 KB
Raw
1 /**
2 * `@alchemy.run/frontend-frameworks/vite/node` — the Node container deploy
3 * target for the plain Vite integration.
4 *
5 * A plain Vite build is assets-only (client bundle + `index.html`). The
6 * finishing pass writes a Node HTTP program (`serve-node.mjs`) into
7 * `clientDirectory` that serves those files, answers `GET /health`, and
8 * applies SPA / 404-page fallback. Platform composites deploy that file
9 * as-is (`isExternal`) — they do not generate a second static server.
10 *
11 * The wholesale `build` hook runs the project's `vite build` in a
12 * disposable child process whose working directory IS the project root
13 * (see `core/BuildChild.ts`).
14 */
15 import * as Effect from "effect/Effect";
16 import * as Path from "effect/Path";
17 import { runBuildChild } from "../core/BuildChild.ts";
18 import { DeployTargetError, makeDeployTarget } from "../core/index.ts";
19 import {
20 NODE_BUNDLE_CONDITIONS,
21 NODE_SERVE_ENTRY_FILE_NAME,
22 writeNodeServeEntry,
23 } from "../core/NodeServe.ts";
24 import { make, type ViteTarget, type ViteTargetConfig } from "./Vite.ts";
25
26 const fail = (message: string, cause?: unknown) =>
27 new DeployTargetError({ platform: "node", message, cause });
28
29 /**
30 * The in-child target: no wholesale `build` hook, so the child runs the
31 * regular vite-build pipeline (no recursion) then this finish.
32 */
33 const makeNodeChildTarget = (config: ViteTargetConfig = {}): ViteTarget =>
34 makeDeployTarget({
35 platform: "node",
36 config,
37 bundle: {
38 conditions: [...NODE_BUNDLE_CONDITIONS],
39 },
40 finish: (output) =>
41 Effect.gen(function* () {
42 const path = yield* Path.Path;
43 if (output.clientDirectory === undefined) {
44 return yield* fail(
45 "The Vite build produced no client directory for the Node serve entry",
46 );
47 }
48 const servePath = path.join(output.clientDirectory, NODE_SERVE_ENTRY_FILE_NAME);
49 return yield* writeNodeServeEntry({
50 output,
51 servePath,
52 serveModuleName: NODE_SERVE_ENTRY_FILE_NAME,
53 clientDirExpression: `fileURLToPath(new URL("./", import.meta.url))`,
54 notFoundHandling: config.notFoundHandling ?? "spa",
55 htmlHandling: config.htmlHandling ?? "none",
56 platform: "node",
57 });
58 }),
59 });
60
61 /** The runner's JSON payload for the build child (see `core/BuildChild.ts`). */
62 export interface ViteNodeBuildChildConfig {
63 readonly rootDir: string;
64 /** The (JSON-serializable) target config the parent was created with. */
65 readonly config: ViteTargetConfig;
66 }
67
68 /** The pure in-child build entry the shared `BuildChildRunner` invokes. */
69 export const buildInChild = (config: ViteNodeBuildChildConfig) =>
70 Effect.gen(function* () {
71 const framework = yield* make({
72 root: config.rootDir,
73 target: makeNodeChildTarget(config.config),
74 vite: config.config.vite,
75 });
76 return yield* framework.build({ root: config.rootDir });
77 });
78
79 /**
80 * Create the Node {@link ViteTarget}: the assets-only target whose `build`
81 * spawns the vite build in a child process.
82 */
83 export const makeNodeTarget = (config: ViteTargetConfig = {}): ViteTarget => ({
84 ...makeNodeChildTarget(config),
85 build: (context) =>
86 runBuildChild({
87 module: import.meta.url,
88 rootDir: context.root,
89 env: context.env,
90 framework: "vite",
91 config: {
92 rootDir: context.root,
93 config,
94 } satisfies ViteNodeBuildChildConfig,
95 }).pipe(Effect.mapError((error) => fail(error.message, error.cause))),
96 });
97
98 /**
99 * The deploy-target module contract (`resolveDeployTarget` accepts the
100 * default export — or the named `target` export — as a value or factory).
101 */
102 export const target = makeNodeTarget;
103
104 export default makeNodeTarget;