The best way to control Claude Code, Codex and more from any computer or phone

main
ts 244 lines 8.74 KB
Raw
1 import type { MemoOptions } from "alchemy/Command/Memo";
2 import type { InputProps } from "alchemy/Input";
3 import { effectClass } from "alchemy/Util/effect";
4 import * as Effect from "effect/Effect";
5 import type { Providers } from "../Providers.ts";
6 import type { AssetsConfig } from "../Workers/Assets.ts";
7 import {
8 Worker,
9 type NormalizedBindings,
10 type WorkerAssetsConfig,
11 type WorkerBindingProps,
12 type WorkerProps,
13 } from "../Workers/Worker.ts";
14
15 export interface SvelteKitProps<Bindings extends WorkerBindingProps = {}> extends Omit<
16 WorkerProps<Bindings>,
17 "vite" | "main" | "assets" | "source" | "script" | "bundle" | "rules"
18 > {
19 /**
20 * SvelteKit project root (the directory containing `package.json` and
21 * `src/routes`). Relative paths resolve from the process working
22 * directory.
23 * @default process.cwd()
24 */
25 rootDir?: string;
26 /**
27 * Controls which files are content-hashed to decide whether a rebuild is
28 * needed. By default every non-gitignored file under `rootDir` (plus the
29 * nearest package-manager lockfile) is hashed; narrow the scope with
30 * `include`/`exclude` globs when the project sits in a large repository.
31 */
32 memo?: MemoOptions;
33 /**
34 * SvelteKit config overrides for the `sveltekit(...)` plugin call, merged
35 * over the options of the user's own call (these win). JSON-serializable
36 * only — no `preprocess`/`vitePlugin`/functions; construction-time options
37 * (`preprocess`, `extensions`, `compilerOptions`, `vitePlugin`) can only
38 * apply when no user `vite.config.*` exists. The `adapter` field is
39 * always owned by alchemy.
40 */
41 kit?: Record<string, unknown>;
42 /**
43 * Optional configuration for static asset routing behavior.
44 * Supports `runWorkerFirst`, `htmlHandling`, `notFoundHandling`, etc.
45 */
46 assets?: AssetsConfig;
47 }
48
49 // These options are inspected while constructing the Worker. Resolve them in
50 // the outer props Effect; pass-through properties can remain deferred Inputs.
51 type SvelteKitInput<Bindings extends WorkerBindingProps> = InputProps<
52 SvelteKitProps<Bindings>,
53 "assets"
54 >;
55
56 /**
57 * A Cloudflare Worker deployed from a SvelteKit project.
58 *
59 * `SvelteKit` builds the app with SvelteKit's own Vite pipeline and a
60 * wrangler-free in-memory Cloudflare adapter, then re-bundles the
61 * Node-flavored server output for workerd. A project-owned
62 * `vite.config.*` loads natively (its `sveltekit(...)` options apply) —
63 * no `svelte.config.js` (kit v3 dropped it), no
64 * `@sveltejs/adapter-cloudflare`, no Wrangler configuration required.
65 * Client assets and prerendered pages are deployed as Worker static
66 * assets; dynamic routes are served by the generated Worker.
67 *
68 * The `@alchemy.run/frontend-frameworks` package must be installed in your
69 * project — its `/sveltekit` export is loaded dynamically at deploy time.
70 *
71 * Input files are content-hashed (respecting `.gitignore` by default) so
72 * unchanged projects skip the build and deploy entirely.
73 *
74 * SvelteKit's server code runs under `nodejs_compat` (the server graph is
75 * built for Node), so the flag is always included in the Worker's
76 * compatibility flags.
77 *
78 * Note on local dev: `alchemy dev` runs SvelteKit's own Vite dev server
79 * (Node SSR with full HMR). `platform.env` carries the Worker's real
80 * Cloudflare bindings (KV, R2, D1, ...) served by the cloudflare-runtime
81 * platform proxy, with literal `env` values (strings and secrets)
82 * overlaid.
83 *
84 *
85 * ### Deploying a SvelteKit App
86 * A single call builds and deploys the app — server-rendered routes,
87 * prerendered pages, and client assets included.
88 *
89 * **Example:** Basic SvelteKit site
90 * ```typescript
91 * const site = yield* Cloudflare.Website.SvelteKit("Website");
92 * ```
93 *
94 * ### Bindings
95 * Values passed via `env` are exposed to server routes through
96 * SvelteKit's `platform.env`.
97 *
98 * **Example:** Reading env from a server route
99 * ```typescript
100 * const site = yield* Cloudflare.Website.SvelteKit("Website", {
101 * env: {
102 * API_KEY: Config.Redacted("API_KEY"),
103 * },
104 * });
105 *
106 * // src/routes/+page.server.ts
107 * // export const load = ({ platform }) => ({
108 * // hasKey: platform?.env?.API_KEY !== undefined,
109 * // });
110 * ```
111 *
112 * ### Kit Options and 404 Handling
113 * Kit options live in the `sveltekit(...)` call in your
114 * `vite.config.ts`, which loads natively. Fallback-page behavior is
115 * driven by the platform-native `assets.notFoundHandling` knob — the
116 * build generates the matching fallback page (rendering the app shell,
117 * so kit's own error page shows).
118 *
119 * **Example:** App-shell 404 fallback
120 * ```typescript
121 * const site = yield* Cloudflare.Website.SvelteKit("Website", {
122 * assets: {
123 * notFoundHandling: "404-page",
124 * },
125 * });
126 * ```
127 *
128 * The `kit` prop is a deploy-time override bag merged over your own
129 * `sveltekit(...)` options (the prop wins) — useful for per-stage values
130 * the config file can't compute. JSON-serializable values only.
131 *
132 * **Example:** Deploy-time kit overrides
133 * ```typescript
134 * const site = yield* Cloudflare.Website.SvelteKit("Website", {
135 * kit: {
136 * paths: { base: "/docs" },
137 * },
138 * });
139 * ```
140 *
141 * ### Custom Rebuild Scope
142 * By default, every non-gitignored file is hashed to decide whether a
143 * rebuild is needed. Use `memo` to narrow the scope when the project
144 * lives in a large repository.
145 *
146 * **Example:** Narrowing the memo scope
147 * ```typescript
148 * const site = yield* Cloudflare.Website.SvelteKit("Website", {
149 * memo: {
150 * include: ["src/**", "static/**", "package.json"],
151 * },
152 * });
153 * ```
154 *
155 * ### Class Form
156 * Calling `SvelteKit` with no arguments returns a constructor you can
157 * `extend` to declare the Worker as a named class. The class is both an
158 * `Effect` you can `yield*` to deploy and a type you can reference
159 * elsewhere — useful when other resources need to bind to this Worker.
160 *
161 * **Example:** Declaring a Worker class
162 * ```typescript
163 * class Website extends Cloudflare.Website.SvelteKit<Website>()(
164 * "Website",
165 * ) {}
166 *
167 * const site = yield* Website;
168 * ```
169 *
170 * @resource
171 * @product Website
172 * @category Workers & Compute
173 */
174 export const SvelteKit: {
175 <Self>(): {
176 <const Bindings extends WorkerBindingProps = {}, Req = never>(
177 id: string,
178 propsEff?: SvelteKitInput<Bindings> | Effect.Effect<SvelteKitInput<Bindings>, never, Req>,
179 ): Effect.Effect<Self, never, Req | Providers> & {
180 new (): Worker<{
181 [binding in keyof NormalizedBindings<Bindings, WorkerAssetsConfig>]: NormalizedBindings<
182 Bindings,
183 WorkerAssetsConfig
184 >[binding];
185 }>;
186 };
187 };
188 <const Bindings extends WorkerBindingProps = {}, Req = never>(
189 id: string,
190 propsEff?: SvelteKitInput<Bindings> | Effect.Effect<SvelteKitInput<Bindings>, never, Req>,
191 ): Effect.Effect<
192 Worker<{
193 [binding in keyof NormalizedBindings<Bindings, WorkerAssetsConfig>]: NormalizedBindings<
194 Bindings,
195 WorkerAssetsConfig
196 >[binding];
197 }>,
198 never,
199 Req | Providers
200 >;
201 } = (<const Bindings extends WorkerBindingProps = {}, Req = never>(
202 id?: string,
203 propsEff?: SvelteKitInput<Bindings> | Effect.Effect<SvelteKitInput<Bindings>, never, Req>,
204 ) =>
205 id === undefined
206 ? <const Bindings extends WorkerBindingProps = {}, Req = never>(
207 id: string,
208 propsEff?: SvelteKitInput<Bindings> | Effect.Effect<SvelteKitInput<Bindings>, never, Req>,
209 ) => effectClass(SvelteKit(id, propsEff))
210 : Worker(
211 id,
212 Effect.map(Effect.isEffect(propsEff) ? propsEff : Effect.succeed(propsEff), (props) => ({
213 ...props,
214 // SvelteKit's server graph is built for Node and needs
215 // `nodejs_compat` — `getCompatibility` already adds it to every
216 // non-python Worker.
217 assets: props?.assets,
218 source: {
219 provider: "@alchemy.run/frontend-frameworks/sveltekit/source",
220 devMode: "server",
221 rootDir: props?.rootDir,
222 options: {
223 rootDir: props?.rootDir,
224 memo: props?.memo,
225 kit: props?.kit,
226 // The adapter's build-time page GENERATION (404.html /
227 // app-shell index.html) is derived from the one
228 // platform-native knob, `assets.notFoundHandling`, so a
229 // single prop configures generation AND serving — the two
230 // halves can never disagree. The generated 404-page
231 // renders the app shell so kit's own error page shows.
232 ...(props?.assets?.notFoundHandling !== undefined &&
233 props.assets.notFoundHandling !== "none"
234 ? {
235 adapter: {
236 notFoundHandling: props.assets.notFoundHandling,
237 fallback: "spa",
238 },
239 }
240 : {}),
241 },
242 },
243 })),
244 )) as any;