The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { BundleError } from "alchemy/Bundle/Bundle"; |
| 2 | import * as Schema from "effect/Schema"; |
| 3 | import type { Plugin } from "vite"; |
| 4 | import type { AssetsConfig } from "../Workers/Assets.ts"; |
| 5 | |
| 6 | /** The versioned manifest returned by Foldkit's completed-build API. */ |
| 7 | export const FoldkitBuildManifest = Schema.Struct({ |
| 8 | schemaVersion: Schema.Literals([1]), |
| 9 | client: Schema.String, |
| 10 | server: Schema.String, |
| 11 | serverEntry: Schema.String, |
| 12 | prerendered: Schema.Array(Schema.String), |
| 13 | }); |
| 14 | |
| 15 | export type FoldkitBuildManifest = typeof FoldkitBuildManifest.Type; |
| 16 | |
| 17 | /** Validate the app's plugin result without loading a second Foldkit copy. */ |
| 18 | export const FoldkitBuildMetadata = Schema.Struct({ |
| 19 | root: Schema.String, |
| 20 | clientDirectory: Schema.String, |
| 21 | serverDirectory: Schema.String, |
| 22 | serverEntry: Schema.String, |
| 23 | manifest: FoldkitBuildManifest, |
| 24 | }); |
| 25 | |
| 26 | export type FoldkitBuildMetadata = typeof FoldkitBuildMetadata.Type; |
| 27 | |
| 28 | /** Prepare before building; call the reader only after buildApp succeeds. */ |
| 29 | export const foldkitBuildMetadataReader = ( |
| 30 | plugins: ReadonlyArray<Pick<Plugin, "name" | "api">>, |
| 31 | main: string | undefined, |
| 32 | ): (() => FoldkitBuildMetadata) | undefined => { |
| 33 | const plugin = plugins.find((plugin) => plugin.name === "foldkit:build"); |
| 34 | if (plugin === undefined) return undefined; |
| 35 | if (main !== undefined) { |
| 36 | throw BundleError.make({ |
| 37 | message: |
| 38 | "Foldkit ssr.build generates the Worker fetch handler and cannot be combined with main. Remove main or disable ssr.build for a custom Worker entry.", |
| 39 | }); |
| 40 | } |
| 41 | if (typeof plugin.api?.getBuildMetadata !== "function") { |
| 42 | throw BundleError.make({ |
| 43 | message: |
| 44 | "Foldkit's build plugin does not expose getBuildMetadata(). Upgrade @foldkit/vite-plugin to 0.25.0 or newer.", |
| 45 | }); |
| 46 | } |
| 47 | // oxlint-disable-next-line effecttsgo/schema-sync -- read inside Vite's build callback; a throw is caught by its tryPromise |
| 48 | return () => Schema.decodeUnknownSync(FoldkitBuildMetadata)(plugin.api.getBuildMetadata()); |
| 49 | }; |
| 50 | |
| 51 | /** |
| 52 | * Client-only builds need SPA fallback. Server builds contain only generated |
| 53 | * pages in the client directory, so normal asset lookup serves prerendered |
| 54 | * routes and misses reach Foldkit's generated document handler. |
| 55 | * Explicit resource asset settings override this default. |
| 56 | */ |
| 57 | export const foldkitAssetsFromManifest = ( |
| 58 | manifest: FoldkitBuildManifest | undefined, |
| 59 | ): AssetsConfig | undefined => |
| 60 | manifest === undefined ? { notFoundHandling: "single-page-application" } : undefined; |