| 1 | /** |
| 2 | * `@alchemy.run/frontend-frameworks/vite/aws` — the AWS deploy target for |
| 3 | * the plain Vite integration. |
| 4 | * |
| 5 | * A plain Vite build is assets-only (client bundle + `index.html`), so |
| 6 | * there is no server entry to wrap for Lambda and no finishing pass. The |
| 7 | * target owns exactly one seam: the wholesale `build` hook runs the |
| 8 | * project's `vite build` in a disposable child process whose working |
| 9 | * directory IS the project root (see `core/BuildChild.ts`) — `vite.config.*` |
| 10 | * executes user plugins that may read the cwd, mutate `process.env`, or |
| 11 | * `process.chdir`, none of which may touch the engine process. |
| 12 | */ |
| 13 | import * as Effect from "effect/Effect"; |
| 14 | import { runBuildChild } from "../core/BuildChild.ts"; |
| 15 | import { DeployTargetError, makeDeployTarget } from "../core/index.ts"; |
| 16 | import { make, type ViteTarget, type ViteTargetConfig } from "./Vite.ts"; |
| 17 | |
| 18 | const fail = (message: string, cause?: unknown) => |
| 19 | new DeployTargetError({ platform: "aws", message, cause }); |
| 20 | |
| 21 | /** |
| 22 | * The in-child target: no wholesale `build` hook, so the child runs the |
| 23 | * regular vite-build pipeline (no recursion). |
| 24 | */ |
| 25 | const makeAwsChildTarget = (config: ViteTargetConfig = {}): ViteTarget => |
| 26 | makeDeployTarget({ |
| 27 | platform: "aws", |
| 28 | config, |
| 29 | }); |
| 30 | |
| 31 | /** The runner's JSON payload for the build child (see `core/BuildChild.ts`). */ |
| 32 | export interface ViteAwsBuildChildConfig { |
| 33 | readonly rootDir: string; |
| 34 | /** The (JSON-serializable) target config the parent was created with. */ |
| 35 | readonly config: ViteTargetConfig; |
| 36 | } |
| 37 | |
| 38 | /** The pure in-child build entry the shared `BuildChildRunner` invokes. */ |
| 39 | export const buildInChild = (config: ViteAwsBuildChildConfig) => |
| 40 | Effect.gen(function* () { |
| 41 | const framework = yield* make({ |
| 42 | root: config.rootDir, |
| 43 | target: makeAwsChildTarget(config.config), |
| 44 | vite: config.config.vite, |
| 45 | }); |
| 46 | return yield* framework.build({ root: config.rootDir }); |
| 47 | }); |
| 48 | |
| 49 | /** |
| 50 | * Create the AWS {@link ViteTarget}: the assets-only target whose `build` |
| 51 | * spawns the vite build in a child process. |
| 52 | */ |
| 53 | export const makeAwsTarget = (config: ViteTargetConfig = {}): ViteTarget => ({ |
| 54 | ...makeAwsChildTarget(config), |
| 55 | build: (context) => |
| 56 | runBuildChild({ |
| 57 | module: import.meta.url, |
| 58 | rootDir: context.root, |
| 59 | framework: "vite", |
| 60 | config: { |
| 61 | rootDir: context.root, |
| 62 | config, |
| 63 | } satisfies ViteAwsBuildChildConfig, |
| 64 | }).pipe(Effect.mapError((error) => fail(error.message, error.cause))), |
| 65 | }); |
| 66 | |
| 67 | /** |
| 68 | * The deploy-target module contract (`resolveDeployTarget` accepts the |
| 69 | * default export — or the named `target` export — as a value or factory). |
| 70 | */ |
| 71 | export const target = makeAwsTarget; |
| 72 | |
| 73 | export default makeAwsTarget; |