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, 06:00 UTC
| 1 | import type { InputProps } from "alchemy/Input"; |
| 2 | import * as Namespace from "alchemy/Namespace"; |
| 3 | import { makeFrameworkSite, type FrameworkSiteProps } from "./FrameworkSite.ts"; |
| 4 | import { viteFrameworkOptions, type ViteProps } from "./Vite.ts"; |
| 5 | |
| 6 | /** The framework-integration package that drives the Foldkit build. */ |
| 7 | export const FOLDKIT_FRAMEWORK_SPECIFIER = "@alchemy.run/frontend-frameworks/foldkit"; |
| 8 | |
| 9 | /** The AWS Lambda deploy target for the Foldkit build (`server: "function"`). */ |
| 10 | export const FOLDKIT_AWS_TARGET_SPECIFIER = "@alchemy.run/frontend-frameworks/foldkit/aws"; |
| 11 | |
| 12 | /** The Node deploy target for the Lambda Web Function server (the default). */ |
| 13 | export const FOLDKIT_WEB_TARGET_SPECIFIER = "@alchemy.run/frontend-frameworks/foldkit/node"; |
| 14 | |
| 15 | /** Configuration for a Foldkit website on AWS. */ |
| 16 | export type FoldkitProps = FrameworkSiteProps< |
| 17 | Pick<ViteProps, "vite" | "config" | "spa" | "errorPage"> & { |
| 18 | /** |
| 19 | * Deployment topology, declared before building. `"server"` deploys a |
| 20 | * server for SSR/hybrid routes; `"static"` deploys only prerendered |
| 21 | * assets. Omit for a browser-only SPA. A server build requires an |
| 22 | * explicit choice. |
| 23 | */ |
| 24 | output?: "server" | "static"; |
| 25 | } |
| 26 | >; |
| 27 | |
| 28 | /** |
| 29 | * Deploy a [Foldkit](https://foldkit.dev) app to AWS: assets in S3 behind |
| 30 | * a CloudFront distribution. Browser-only apps route on the client, so |
| 31 | * `spa` defaults on. With `output: "server"`, Foldkit's generated fetch |
| 32 | * handler runs on a Lambda Web Function (or a Lambda Function with |
| 33 | * `server: "function"`); prerendered pages and assets stay in S3. |
| 34 | * |
| 35 | * The Foldkit Vite plugin in your project's own `vite.config.*` owns |
| 36 | * rendering and prerendering — enable `ssr.build` there for SSR or SSG. |
| 37 | * The build runs through `@alchemy.run/frontend-frameworks/foldkit`, which |
| 38 | * must be installed in your project. Input files are content-hashed so |
| 39 | * unchanged projects skip the build and deploy entirely. |
| 40 | * |
| 41 | * During `alchemy dev` the site is Foldkit's own Vite dev server (HMR and |
| 42 | * devtools included) and no AWS resources are created. `Alchemy.remote()` |
| 43 | * opts back into the full deployment. |
| 44 | * |
| 45 | * ### Creating Foldkit Sites |
| 46 | * **Example:** Browser-only SPA |
| 47 | * ```typescript |
| 48 | * const site = yield* AWS.Website.Foldkit("Web"); |
| 49 | * ``` |
| 50 | * |
| 51 | * **Example:** Project in a Subdirectory |
| 52 | * ```typescript |
| 53 | * const site = yield* AWS.Website.Foldkit("Web", { |
| 54 | * rootDir: "applications/web", |
| 55 | * }); |
| 56 | * ``` |
| 57 | * |
| 58 | * ### Server Rendering |
| 59 | * Set `output: "server"` when `ssr.build` produces a server handler. |
| 60 | * Prerendered routes are served from S3; every other request reaches the |
| 61 | * server. |
| 62 | * |
| 63 | * **Example:** SSR with Prerendered Routes |
| 64 | * ```typescript |
| 65 | * const site = yield* AWS.Website.Foldkit("Web", { |
| 66 | * output: "server", |
| 67 | * env: { API_BASE: "https://api.example.com" }, |
| 68 | * }); |
| 69 | * ``` |
| 70 | * |
| 71 | * **Example:** SSR on a Lambda Function |
| 72 | * ```typescript |
| 73 | * const site = yield* AWS.Website.Foldkit("Web", { |
| 74 | * output: "server", |
| 75 | * server: "function", |
| 76 | * memorySize: 2048, |
| 77 | * }); |
| 78 | * ``` |
| 79 | * |
| 80 | * ### Static Generation |
| 81 | * `output: "static"` deploys only the prerendered pages; no server is |
| 82 | * created. |
| 83 | * |
| 84 | * **Example:** Prerendered Pages Without a Server |
| 85 | * ```typescript |
| 86 | * const site = yield* AWS.Website.Foldkit("Docs", { |
| 87 | * output: "static", |
| 88 | * errorPage: "404.html", |
| 89 | * }); |
| 90 | * ``` |
| 91 | * |
| 92 | * ### Deep Links |
| 93 | * A deep link like `/counter/42` arrives at the edge as a request for a |
| 94 | * file that does not exist. A browser-only app serves the shell instead of |
| 95 | * a 404. An app that ships a real 404 page opts out with `errorPage` — the |
| 96 | * two are mutually exclusive. |
| 97 | * |
| 98 | * **Example:** Serving a Real 404 Page |
| 99 | * ```typescript |
| 100 | * const site = yield* AWS.Website.Foldkit("Web", { |
| 101 | * spa: false, |
| 102 | * errorPage: "404.html", |
| 103 | * }); |
| 104 | * ``` |
| 105 | * |
| 106 | * ### Custom Domains |
| 107 | * **Example:** Custom Domain |
| 108 | * ```typescript |
| 109 | * const site = yield* AWS.Website.Foldkit("Web", { |
| 110 | * domain: { |
| 111 | * name: "app.example.com", |
| 112 | * hostedZoneId: zone.hostedZoneId, |
| 113 | * }, |
| 114 | * }); |
| 115 | * ``` |
| 116 | * |
| 117 | * **Example:** Serve Through an Existing AWS.Website.Router |
| 118 | * ```typescript |
| 119 | * const router = yield* AWS.Website.Router("Router", {}); |
| 120 | * const site = yield* AWS.Website.Foldkit("Web", { |
| 121 | * domain: { router }, |
| 122 | * }); |
| 123 | * ``` |
| 124 | * |
| 125 | * ### Build Configuration |
| 126 | * The `vite` bag holds deploy-time overrides merged over your |
| 127 | * `vite.config.*`, and `config` selects an alternate config file. |
| 128 | * |
| 129 | * **Example:** Deploy-Time Base Path Override |
| 130 | * ```typescript |
| 131 | * const site = yield* AWS.Website.Foldkit("Web", { |
| 132 | * vite: { base: "/app/" }, |
| 133 | * }); |
| 134 | * ``` |
| 135 | * |
| 136 | * @resource |
| 137 | */ |
| 138 | export const Foldkit = (id: string, props: InputProps<FoldkitProps, "server"> = {}) => { |
| 139 | const p = props as FoldkitProps; |
| 140 | return makeFrameworkSite(id, props, { |
| 141 | name: "Foldkit", |
| 142 | framework: FOLDKIT_FRAMEWORK_SPECIFIER, |
| 143 | target: FOLDKIT_AWS_TARGET_SPECIFIER, |
| 144 | webTarget: FOLDKIT_WEB_TARGET_SPECIFIER, |
| 145 | options: { ...viteFrameworkOptions(p), output: p.output ?? "spa" }, |
| 146 | // Browser-only and prerendered builds are assets-only. Browser-only |
| 147 | // apps route on the client, so `spa` defaults on unless an |
| 148 | // `errorPage` is set (the two are mutually exclusive downstream). |
| 149 | static: |
| 150 | p.output === "server" |
| 151 | ? undefined |
| 152 | : { |
| 153 | spa: p.spa ?? (p.output === undefined && p.errorPage === undefined), |
| 154 | errorPage: p.errorPage, |
| 155 | }, |
| 156 | }).pipe(Namespace.push(id)); |
| 157 | }; |