@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, 06:00 UTC

main
ts 226 lines 7.92 KB
Raw
1 import type { InputProps } from "alchemy/Input";
2 import * as Namespace from "alchemy/Namespace";
3 import { makeFrameworkSite, type FrameworkSiteBaseProps } from "./FrameworkSite.ts";
4
5 /** The framework-integration package that drives the Vite build. */
6 export const VITE_FRAMEWORK_SPECIFIER = "@alchemy.run/frontend-frameworks/vite";
7
8 /** The AWS deploy target for the Vite build. */
9 export const VITE_AWS_TARGET_SPECIFIER = "@alchemy.run/frontend-frameworks/vite/aws";
10
11 /**
12 * Serializable Vite build overrides merged OVER the project's own
13 * `vite.config.*` at deploy time (deploy-time values win). Mirrors the
14 * integration seam's `ViteBuildConfig`
15 * (`@alchemy.run/frontend-frameworks/vite`). Deliberately a small,
16 * JSON-serializable subset — plugins and other non-serializable config
17 * belong in the config file, which loads natively.
18 */
19 export interface ViteBuildConfig {
20 /**
21 * Public base path the site deploys under (vite's `base`). Overrides
22 * the config file's `base` for both the production build and the dev
23 * server under `alchemy dev`.
24 */
25 base?: string;
26 /**
27 * Build output directory, relative to `rootDir` (vite's
28 * `build.outDir`). Overrides the config file's `build.outDir`.
29 * @default the config file's `build.outDir` (vite's default: "dist")
30 */
31 outDir?: string;
32 }
33
34 export interface ViteProps extends Omit<
35 FrameworkSiteBaseProps,
36 // Assets-only: a plain Vite site never creates a server function.
37 "env" | "timeout"
38 > {
39 /**
40 * Environment variables for the Vite process: the production build on
41 * deploy, and the dev server under `alchemy dev`. Values can come from
42 * other resources' `Output`s, and every entry is readable from
43 * `process.env` in `vite.config.*` (e.g. a dev `server.proxy` target).
44 * Only `VITE_*` variables reach the client bundle (`import.meta.env`).
45 * Changing a value restarts the dev server.
46 */
47 env?: Record<string, any>;
48 /**
49 * Deploy-time Vite overrides merged over your `vite.config.*`. The
50 * config file is the primary home for Vite configuration (it loads
51 * natively, plugins included) — reach for this bag when a value is
52 * decided by the deployment rather than the project, since config
53 * files cannot consume alchemy `Output`s.
54 */
55 vite?: ViteBuildConfig;
56 /**
57 * Alternate Vite config file to load instead of the auto-discovered
58 * `vite.config.*`, relative to {@link FrameworkSiteProps.rootDir | rootDir}.
59 */
60 config?: string;
61 /**
62 * Answer misses with the index page (200) instead of a 404, so
63 * client-side routes deep-link correctly. Plain Vite apps are typically
64 * single-page applications, so this defaults on; set `false` for
65 * multi-page (`index.html`-per-route) projects. Mutually exclusive with
66 * {@link errorPage}.
67 * @default true unless `errorPage` is set
68 */
69 spa?: boolean;
70 /**
71 * Serve the built error page (e.g. `404.html`) with a real `404` status
72 * for requests that match no uploaded file. Mutually exclusive with
73 * {@link spa}.
74 */
75 errorPage?: string;
76 }
77
78 /**
79 * Assemble the framework-integration `options` bag from the resource's
80 * `vite` overrides and `config` file selection. The integration seam is
81 * `ViteOptions.vite` (`@alchemy.run/frontend-frameworks/vite`), whose
82 * `configFile` is resolved relative to the project root. Shared with
83 * {@link Foldkit | AWS.Website.Foldkit}, which builds through the same
84 * pipeline.
85 *
86 * @internal
87 */
88 export const viteFrameworkOptions = (
89 p: Pick<ViteProps, "vite" | "config">,
90 ): Record<string, unknown> | undefined =>
91 p.vite !== undefined || p.config !== undefined
92 ? {
93 vite: {
94 ...p.vite,
95 ...(p.config !== undefined ? { configFile: p.config } : undefined),
96 },
97 }
98 : undefined;
99
100 /**
101 * Deploy a plain [Vite](https://vite.dev) application to AWS: static assets
102 * (the `vite build` output) in S3 behind a CloudFront distribution. For
103 * client-only projects — React/Vue/Solid SPAs, `index.html` multi-page
104 * apps — whose entire deployable output is static assets.
105 *
106 * The build runs through `@alchemy.run/frontend-frameworks/vite` with the
107 * `@alchemy.run/frontend-frameworks/vite/aws` deploy target — the package
108 * must be installed in your project. Your project's own `vite.config.*`
109 * (plugins included) drives the build; input files are content-hashed so
110 * unchanged projects skip the build and deploy entirely.
111 *
112 * During `alchemy dev` the site is Vite's own dev server (native HMR) and
113 * no cloud resources are created — the site's `url` is the dev server's
114 * local address. `Alchemy.remote()` opts back into the full deployment.
115 *
116 * SSR frameworks that wrap Vite deploy through their own composites
117 * ({@link Astro | AWS.Website.Astro}, {@link SvelteKit | AWS.Website.SvelteKit},
118 * {@link Octane | AWS.Website.Octane}, ...) — this composite never creates
119 * a server function.
120 *
121 * ### Creating Vite Sites
122 * **Example:** Basic Vite SPA
123 * ```typescript
124 * const site = yield* AWS.Website.Vite("Web");
125 * ```
126 *
127 * **Example:** Project in a Subdirectory
128 * ```typescript
129 * const site = yield* AWS.Website.Vite("Web", {
130 * rootDir: "./app",
131 * });
132 * ```
133 *
134 * **Example:** Custom Domain
135 * ```typescript
136 * const site = yield* AWS.Website.Vite("Web", {
137 * domain: {
138 * name: "app.example.com",
139 * hostedZoneId: zone.hostedZoneId,
140 * },
141 * });
142 * ```
143 *
144 * ### Multi-Page Sites
145 * **Example:** Per-Route HTML Pages with a 404 Page
146 * ```typescript
147 * const site = yield* AWS.Website.Vite("Docs", {
148 * spa: false,
149 * errorPage: "404.html",
150 * });
151 * ```
152 *
153 * ### Sharing a Router
154 * **Example:** Serve Through an Existing AWS.Website.Router
155 * ```typescript
156 * const router = yield* AWS.Website.Router("Router", {});
157 * const site = yield* AWS.Website.Vite("Web", {
158 * domain: { router },
159 * });
160 * ```
161 *
162 * ### Build Configuration
163 * Vite configuration lives in your project's own `vite.config.*`, which
164 * loads natively — plugins included. The `vite` bag is for deploy-time
165 * overrides merged over that file (config files cannot consume alchemy
166 * `Output`s), and `config` selects an alternate config file.
167 *
168 * **Example:** Deploy-Time Base Path Override
169 * ```typescript
170 * const site = yield* AWS.Website.Vite("Docs", {
171 * vite: { base: "/docs/" },
172 * });
173 * ```
174 *
175 * **Example:** Alternate Config File
176 * ```typescript
177 * const site = yield* AWS.Website.Vite("Web", {
178 * config: "vite.deploy.config.ts",
179 * });
180 * ```
181 *
182 * **Example:** Build-Time Environment
183 * ```typescript
184 * // Inlined into the bundle as `import.meta.env.VITE_API_URL`.
185 * const site = yield* AWS.Website.Vite("Web", {
186 * env: { VITE_API_URL: api.url },
187 * });
188 * ```
189 *
190 * ### Local Development
191 * **Example:** Vite Dev Server Under `alchemy dev`
192 * ```typescript
193 * // `alchemy dev` starts `vite` programmatically: site.url is the local
194 * // dev server (HMR included); no bucket or distribution is created.
195 * const site = yield* AWS.Website.Vite("Web");
196 * ```
197 *
198 * **Example:** Dev Proxy to Another Resource
199 * ```typescript
200 * // `env` reaches the dev server's process, so vite.config.ts can read
201 * // a value resolved from another resource:
202 * // server: { proxy: { "/api": process.env.API_URL! } }
203 * const site = yield* AWS.Website.Vite("Web", {
204 * env: { API_URL: api.functionUrl },
205 * });
206 * ```
207 *
208 * @resource
209 */
210 export const Vite = (id: string, props: InputProps<ViteProps> = {}) => {
211 const p = props as ViteProps;
212 return makeFrameworkSite(id, props, {
213 name: "Vite",
214 framework: VITE_FRAMEWORK_SPECIFIER,
215 target: VITE_AWS_TARGET_SPECIFIER,
216 options: viteFrameworkOptions(p),
217 // Plain Vite is assets-only: every page is client-rendered from the
218 // uploaded bundle, so the deploy never creates a server function.
219 // `spa` defaults on (Vite apps are typically SPAs) but yields to an
220 // explicit `errorPage` — the two are mutually exclusive downstream.
221 static: {
222 spa: p.spa ?? (p.errorPage === undefined ? true : undefined),
223 errorPage: p.errorPage,
224 },
225 }).pipe(Namespace.push(id));
226 };