import * as path from "@std/path"; import type { Command } from "./commands.ts"; import { fsItemsToCommands, type FsRouteFile } from "./fs_routes.ts"; import type { ServerIslandRegistry } from "./context.ts"; import type { AnyComponent } from "preact"; import { UniqueNamer } from "./utils.ts"; import { setBuildId } from "@paulaboks/sweet-build-id"; export interface FileSnapshot { name: string; filePath: string; hash: string | null; contentType: string; immutable?: boolean; } export interface BuildSnapshot { version: string; clientEntry: string; /** * When defined, forces the boot script to be emitted in dev so HMR * listeners attach even on island-free pages. The value itself is * currently unused — only its presence matters. Undefined outside of dev. */ hmrClientEntry?: string; fsRoutes: FsRouteFile[]; staticFiles: Map; islands: ServerIslandRegistry; entryAssets: string[]; } export interface StaticFile { hash: string | null; size: number; contentType: string; readable: ReadableStream | Uint8Array; close(): void; immutable?: boolean; } // deno-lint-ignore no-explicit-any export interface BuildCache { root: string; islandRegistry: ServerIslandRegistry; clientEntry: string; /** Pathname for the HMR-only chunk (development only). Undefined in production. */ hmrClientEntry?: string; features: { errorOverlay: boolean; }; getFsRoutes(): Command[]; readFile(pathname: string): Promise; getEntryAssets(): string[]; } export class ProdBuildCache implements BuildCache { #snapshot: BuildSnapshot; islandRegistry: ServerIslandRegistry; clientEntry: string; hmrClientEntry: string | undefined; features = { errorOverlay: false }; constructor(public root: string, snapshot: BuildSnapshot) { setBuildId(snapshot.version); this.#snapshot = snapshot; this.islandRegistry = snapshot.islands; this.clientEntry = snapshot.clientEntry; this.hmrClientEntry = snapshot.hmrClientEntry; } getEntryAssets(): string[] { return this.#snapshot.entryAssets; } getFsRoutes(): Command[] { return fsItemsToCommands(this.#snapshot.fsRoutes); } async readFile(pathname: string): Promise { const { staticFiles } = this.#snapshot; const info = staticFiles.get(pathname); if (info === undefined) return null; const filePath = path.isAbsolute(info.filePath) ? info.filePath : path.join(this.root, info.filePath); const [stat, file] = await Promise.all([ Deno.stat(filePath), Deno.open(filePath), ]); return { hash: info.hash, contentType: info.contentType, size: stat.size, readable: file.readable, close: () => file.close(), immutable: info.immutable, }; } } /** Fallback build cache for when there's no build output (and for tests). */ export class MockBuildCache implements BuildCache { #files: FsRouteFile[]; root = ""; clientEntry = ""; islandRegistry: ServerIslandRegistry = new Map(); features = { errorOverlay: false }; constructor(files: FsRouteFile[], mode: "development" | "production") { this.features.errorOverlay = mode === "development"; this.#files = files; } getEntryAssets(): string[] { return []; } getFsRoutes(): Command[] { return fsItemsToCommands(this.#files); } readFile(_pathname: string): Promise { return Promise.resolve(null); } } export class IslandPreparer { #namer = new UniqueNamer(); prepare( registry: ServerIslandRegistry, mod: Record, chunkName: string, modName: string, css: string[], ) { for (const [name, value] of Object.entries(mod)) { if (typeof value !== "function") continue; const islandName = name === "default" ? modName : name; const uniqueName = this.#namer.getUniqueName(islandName); const fn = value as AnyComponent; registry.set(fn, { exportName: name, file: chunkName, fn, name: uniqueName, css, }); } } }