The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeV8 from "node:v8"; |
| 2 | import { PlatformServices, runMain } from "alchemy/Util/PlatformServices"; |
| 3 | import * as Cause from "effect/Cause"; |
| 4 | import * as Console from "effect/Console"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as FileSystem from "effect/FileSystem"; |
| 7 | import * as Stdio from "effect/Stdio"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import { viteBuildInProcess } from "./Sources/Vite.ts"; |
| 10 | import type { ViteBuildChildConfig, ViteBuildChildResult } from "./ViteChild.shared.ts"; |
| 11 | |
| 12 | /** |
| 13 | * Entry point of the one-shot Vite *build* child spawned by |
| 14 | * `runViteBuildChild` (`ViteChild.ts`), with the project root as its |
| 15 | * working directory. Running the build out of process is the isolation |
| 16 | * boundary: vite resolves a relative root against live `process.cwd()`, |
| 17 | * plugins read cwd freely, and the build's own spawns (via cross-spawn) |
| 18 | * `process.chdir` the hosting process transiently — none of which is safe |
| 19 | * inside the concurrent engine/test process. |
| 20 | * |
| 21 | * Protocol: V8-serialized {@link ViteBuildChildConfig} on stdin; the child |
| 22 | * writes the V8-serialized {@link ViteBuildChildResult} to |
| 23 | * `config.outputPath` and exits 0. Build logs stream over stdout/stderr; |
| 24 | * a failed build exits non-zero with the error on stderr. |
| 25 | */ |
| 26 | |
| 27 | const readConfig = Effect.gen(function* () { |
| 28 | const stdio = yield* Stdio.Stdio; |
| 29 | const chunks = yield* Stream.runCollect(stdio.stdin); |
| 30 | return NodeV8.deserialize(Buffer.concat(chunks)) as ViteBuildChildConfig; |
| 31 | }); |
| 32 | |
| 33 | const program = Effect.gen(function* () { |
| 34 | const config = yield* readConfig; |
| 35 | const fs = yield* FileSystem.FileSystem; |
| 36 | const { clientDirectory, foldkit, base, serverBundle, externalWorkspaces } = |
| 37 | yield* viteBuildInProcess(config.rootDir, config.env, { |
| 38 | main: config.main, |
| 39 | framework: config.framework, |
| 40 | compatibilityDate: config.compatibilityDate, |
| 41 | compatibilityFlags: config.compatibilityFlags, |
| 42 | viteEnvironments: config.viteEnvironments, |
| 43 | }); |
| 44 | const [bundle, workspaces] = yield* Effect.all([serverBundle, externalWorkspaces]); |
| 45 | const result: ViteBuildChildResult = { |
| 46 | clientDirectory, |
| 47 | foldkit, |
| 48 | base, |
| 49 | serverBundle: bundle, |
| 50 | externalWorkspaces: Array.from(workspaces), |
| 51 | }; |
| 52 | yield* fs.writeFile(config.outputPath, NodeV8.serialize(result)); |
| 53 | }); |
| 54 | |
| 55 | // The parent streams this child's output and turns its exit code + stderr |
| 56 | // into the resource-scoped build error. Skip runMain's own `✖` report, but |
| 57 | // print the cause: vite only logs `✗ Build failed in …`, so this is the only |
| 58 | // place the actual error (a rolldown diagnostic, a plugin throwing during |
| 59 | // config resolution) reaches the parent. |
| 60 | runMain( |
| 61 | program.pipe( |
| 62 | Effect.tapCause((cause) => cause.pipe(Cause.pretty, Console.error)), |
| 63 | Effect.provide(PlatformServices), |
| 64 | ), |
| 65 | { disableErrorReporting: true }, |
| 66 | ); |