import { loadSekhem } from "./loadSekhem.js"; import path from "node:path"; import { readFile, cp } from "node:fs/promises"; import { readFileSync } from "node:fs"; import { existsSync } from "node:fs"; import type { Engine } from "@devorajs/core/config"; import { createHash } from "node:crypto"; import { listRouteFiles, toBuildKey } from "@devorajs/core"; import { discoverCsrRouteFiles } from "./discoverCsrRouteFiles.js"; import { viteLogLevel } from "../terminal/output.js"; interface ViteManifestEntry { file: string; src?: string; isEntry?: boolean; } export interface ClientBuildResult { /** Absolute island source file → its real, hashed public URL (e.g. "/assets/components/Counter-B2LbqBKm.js"). */ islandUrls: Map; /** Public URL of the built island-client.tsx bootstrap, if any islands were found. */ islandClientUrl?: string; /** Absolute csr route file → its real, hashed public URL. */ csrUrls: Map; /** Public URL of the built csr-client.tsx bootstrap, if any csr routes were found. */ csrClientUrl?: string; } /** * Client build for islands (ROADMAP.md #3/#4) AND renderMode: "csr" routes * (ROADMAP.md's render-modes item) — both need a real client-side Vite * build producing a real hashed asset URL, so they share one build pass * rather than two (an app using only one of the two pays for exactly what * it uses; an app using neither skips this file's build entirely). Real * Vite manifest (`build.manifest: true`), verified against its actual shape * (a quick throwaway build + inspecting `.vite/manifest.json`) rather than * assumed — entries are keyed by root-relative source path with * `.src`/`.file`. * * `islandFiles` comes from the SSR build (islandsBuildPlugin.ts), which saw * every module the app's routes import — not just the route files. */ export async function buildAppClient(appRoot: string, islandFiles: string[], engine: Engine = "vite"): Promise { if (engine === "own") return buildAppClientOwn(appRoot, islandFiles); const routesDir = path.join(appRoot, "routes"); const routeFiles = listRouteFiles(routesDir); const csrFiles = discoverCsrRouteFiles(routeFiles); if (islandFiles.length === 0 && csrFiles.length === 0) { // No JS to bundle — but a real bug found while verifying the shared // branding header (ROADMAP.md's shared-design item): an app with no // islands/csr routes never ran a Vite client build at all, so its // `publicDir` (the logo/favicon, see apps/*/vite.config.ts) never got // copied to `dist/client` — the app-wide header's logo 404'd in // production for exactly this reason on marketing (no islands/csr). // Copy publicDir directly, skipping the (real, still avoided) cost of // a full JS bundling pass for an app that doesn't need one. const { resolveConfig } = await import("vite"); const resolved = await resolveConfig( { root: appRoot, configFile: path.join(appRoot, "vite.config.ts") }, "build" ); if (resolved.publicDir && existsSync(resolved.publicDir)) { await cp(resolved.publicDir, path.join(appRoot, "dist", "client"), { recursive: true }); } return { islandUrls: new Map(), csrUrls: new Map() }; } const islandClientPath = path.join(appRoot, "island-client.tsx"); const csrClientPath = path.join(appRoot, "csr-client.tsx"); const clientOutDir = path.join(appRoot, "dist", "client"); const input: Record = {}; if (islandFiles.length > 0) input["island-client"] = islandClientPath; if (csrFiles.length > 0) input["csr-client"] = csrClientPath; for (const file of islandFiles) { input[clientEntryName(appRoot, file)] = file; } for (const file of csrFiles) { input[toBuildKey(appRoot, file)] = file; } const { build: viteBuild } = await import("vite"); await viteBuild({ root: appRoot, logLevel: viteLogLevel(), configFile: path.join(appRoot, "vite.config.ts"), build: { outDir: clientOutDir, emptyOutDir: true, manifest: true, rollupOptions: { input, // Real, previously-undiscovered bug, found while verifying csr's // production build by actually inspecting a built chunk's exports // (not just its content-type/status code, which is all earlier // island verification checked): Rollup's default // preserveEntrySignatures ("exports-only") does NOT reliably keep // an entry's `export default` when nothing in the same build // graph statically imports it — which is every entry here, since // each is only ever reached via a browser's own runtime // `import(url)` to a URL outside this build's static graph, not a // static import Rollup can see. Confirmed directly: without this, // `import()`-ing a built island/csr chunk in plain Node returned // `{ default: undefined }` — the component code was silently // tree-shaken away entirely, not just misplaced. "strict" forces // Rollup to treat every entry's exports as used, regardless of // whether anything in the graph statically references them. preserveEntrySignatures: "strict", }, }, }); const manifestPath = path.join(clientOutDir, ".vite", "manifest.json"); if (!existsSync(manifestPath)) { throw new Error(`[devora] client build for islands/csr produced no manifest at ${manifestPath}`); } const manifest = JSON.parse(await readFile(manifestPath, "utf-8")) as Record; const islandUrls = new Map(); const csrUrls = new Map(); let islandClientUrl: string | undefined; let csrClientUrl: string | undefined; for (const entry of Object.values(manifest)) { if (!entry.isEntry || !entry.src) continue; const absoluteSrc = path.resolve(appRoot, entry.src); if (absoluteSrc === islandClientPath) { islandClientUrl = `/${entry.file}`; } else if (absoluteSrc === csrClientPath) { csrClientUrl = `/${entry.file}`; } else if (islandFiles.includes(absoluteSrc)) { islandUrls.set(absoluteSrc, `/${entry.file}`); } else if (csrFiles.includes(absoluteSrc)) { csrUrls.set(absoluteSrc, `/${entry.file}`); } } return { islandUrls, islandClientUrl, csrUrls, csrClientUrl }; } /** * Rollup entry name for an island file: its build key inside the app; for a * file elsewhere in the repo (a shared `packages/ui` component), a name that * can't climb out of the output directory the way "../../packages/..." would. */ function clientEntryName(appRoot: string, file: string): string { const relative = path.relative(appRoot, file); if (!relative.startsWith("..") && !path.isAbsolute(relative)) return toBuildKey(appRoot, file); const hash = createHash("sha256").update(file).digest("hex").slice(0, 8); return `shared/${path.basename(file, path.extname(file))}-${hash}`.replace(/[[\]]/g, "_"); } /** * The same client build with the own engine (esbuild, no Vite): the same entries, named the same way, with the browser URLs * read from esbuild's metafile instead of Vite's manifest. The public folder is copied to dist/client first, always (the * Vite path copies it in both branches too). */ async function buildAppClientOwn(appRoot: string, islandFiles: string[]): Promise { const routeFiles = listRouteFiles(path.join(appRoot, "routes")); const csrFiles = discoverCsrRouteFiles(routeFiles); const islandClientPath = path.join(appRoot, "island-client.tsx"); const csrClientPath = path.join(appRoot, "csr-client.tsx"); const clientOutDir = path.join(appRoot, "dist", "client"); const publicDir = resolveOwnPublicDir(appRoot); const entries: Record = {}; if (islandFiles.length > 0) entries["island-client"] = islandClientPath; if (csrFiles.length > 0) entries["csr-client"] = csrClientPath; for (const file of islandFiles) entries[clientEntryName(appRoot, file)] = file; for (const file of csrFiles) entries[toBuildKey(appRoot, file)] = file; const { buildClientBundle } = await loadSekhem(); const { urls } = await buildClientBundle({ appRoot, entries, outDir: clientOutDir, publicDir, logLevel: "warning" }); const islandUrls = new Map(); const csrUrls = new Map(); for (const file of islandFiles) { const url = urls.get(clientEntryName(appRoot, file)); if (url) islandUrls.set(file, url); } for (const file of csrFiles) { const url = urls.get(toBuildKey(appRoot, file)); if (url) csrUrls.set(file, url); } return { islandUrls, islandClientUrl: urls.get("island-client"), csrUrls, csrClientUrl: urls.get("csr-client") }; } /** `publicDir` the way the own dev server finds it: the literal form in vite.config.ts if there is one, else `/public`, else the project's `assets/`. */ export function resolveOwnPublicDir(appRoot: string): string | undefined { const config = path.join(appRoot, "vite.config.ts"); if (existsSync(config)) { const m = /publicDir:\s*path\.resolve\(\s*__dirname\s*,\s*["']([^"']+)["']\s*\)/.exec(readFileSync(config, "utf8")); if (m) return path.resolve(appRoot, m[1]!); } const own = path.join(appRoot, "public"); if (existsSync(own)) return own; const shared = path.resolve(appRoot, "..", "..", "assets"); return !existsSync(config) && existsSync(shared) ? shared : undefined; }