| 1 | import type { InputProps } from "../../Input.ts"; |
| 2 | import * as Namespace from "../../Namespace.ts"; |
| 3 | import { makeFrameworkSite, type FrameworkSiteProps } from "./FrameworkSite.ts"; |
| 4 | |
| 5 | /** The framework-integration package that drives the SvelteKit build. */ |
| 6 | export const SVELTEKIT_FRAMEWORK_SPECIFIER = |
| 7 | "@alchemy.run/frontend-frameworks/sveltekit"; |
| 8 | |
| 9 | /** The AWS Lambda deploy target for the SvelteKit build. */ |
| 10 | export const SVELTEKIT_AWS_TARGET_SPECIFIER = |
| 11 | "@alchemy.run/frontend-frameworks/sveltekit/aws"; |
| 12 | |
| 13 | export interface SvelteKitProps extends FrameworkSiteProps { |
| 14 | /** |
| 15 | * SvelteKit config overrides for the `sveltekit(...)` plugin call, merged |
| 16 | * over the options of the user's own call (these win). JSON-serializable |
| 17 | * only — no `preprocess`/`vitePlugin`/functions; construction-time options |
| 18 | * (`preprocess`, `extensions`, `compilerOptions`, `vitePlugin`) can only |
| 19 | * apply when no user `vite.config.*` exists. The `adapter` field is |
| 20 | * always owned by alchemy. |
| 21 | */ |
| 22 | kit?: Record<string, unknown>; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Deploy a SvelteKit application to AWS: kit's SSR server on a streaming |
| 27 | * Lambda Function URL, static assets (prerendered pages included) in S3, |
| 28 | * and a CloudFront distribution whose edge router serves uploaded files |
| 29 | * from S3 and forwards everything else to the server. |
| 30 | * |
| 31 | * The build runs through `@alchemy.run/frontend-frameworks/sveltekit` with the |
| 32 | * `@alchemy.run/frontend-frameworks/sveltekit/aws` deploy target (an in-memory |
| 33 | * kit adapter emitting a streaming Lambda handler) — both must be |
| 34 | * installed in your project. |
| 35 | * |
| 36 | * ### Creating SvelteKit Sites |
| 37 | * **Example:** Basic SvelteKit App |
| 38 | * ```typescript |
| 39 | * const site = yield* AWS.Website.SvelteKit("Web", { |
| 40 | * rootDir: "./app", |
| 41 | * }); |
| 42 | * ``` |
| 43 | * |
| 44 | * **Example:** Custom Domain |
| 45 | * ```typescript |
| 46 | * const site = yield* AWS.Website.SvelteKit("Web", { |
| 47 | * rootDir: "./app", |
| 48 | * domain: { |
| 49 | * name: "app.example.com", |
| 50 | * hostedZoneId: zone.hostedZoneId, |
| 51 | * }, |
| 52 | * }); |
| 53 | * ``` |
| 54 | * |
| 55 | * ### Server Configuration |
| 56 | * **Example:** Tune The Server Function |
| 57 | * ```typescript |
| 58 | * const site = yield* AWS.Website.SvelteKit("Web", { |
| 59 | * rootDir: "./app", |
| 60 | * memorySize: 2048, |
| 61 | * env: { |
| 62 | * API_BASE: api.url, |
| 63 | * }, |
| 64 | * }); |
| 65 | * ``` |
| 66 | * |
| 67 | * ### Kit Overrides |
| 68 | * Kit options live in the `sveltekit(...)` call in your `vite.config.ts`, |
| 69 | * which loads natively. The `kit` prop is a deploy-time override bag |
| 70 | * merged over your own options (the prop wins) — useful for per-stage |
| 71 | * values the config file can't compute. JSON-serializable values only. |
| 72 | * |
| 73 | * **Example:** Deploy-time kit overrides |
| 74 | * ```typescript |
| 75 | * const site = yield* AWS.Website.SvelteKit("Web", { |
| 76 | * rootDir: "./app", |
| 77 | * kit: { |
| 78 | * paths: { base: "/docs" }, |
| 79 | * }, |
| 80 | * }); |
| 81 | * ``` |
| 82 | * |
| 83 | * @resource |
| 84 | */ |
| 85 | export const SvelteKit = ( |
| 86 | id: string, |
| 87 | props: InputProps<SvelteKitProps> = {}, |
| 88 | ) => { |
| 89 | const p = props as SvelteKitProps; |
| 90 | return makeFrameworkSite(id, props, { |
| 91 | name: "SvelteKit", |
| 92 | framework: SVELTEKIT_FRAMEWORK_SPECIFIER, |
| 93 | target: SVELTEKIT_AWS_TARGET_SPECIFIER, |
| 94 | options: { kit: p.kit }, |
| 95 | }).pipe(Namespace.push(id)); |
| 96 | }; |