diff --git a/scripts/generate-build-manifest.ts b/scripts/generate-build-manifest.ts index 2d47f7d7..58c1de8a 100644 --- a/scripts/generate-build-manifest.ts +++ b/scripts/generate-build-manifest.ts @@ -20,7 +20,7 @@ import { buildDate, } from "./lib/build-environment.ts"; import { writeValidatedJsonArtifact } from "./lib/validated-json-artifact.ts"; -import { CANONICAL_VITE_MANIFEST_PATH } from "./lib/build-manifest-outputs.ts"; +import { CANONICAL_VITE_MANIFEST_PATH, findRouteChunk } from "./lib/build-manifest-outputs.ts"; import { runOutputPath } from "./lib/run-isolation.ts"; assertCiBuildEnvironment(process.env); @@ -68,14 +68,13 @@ const routeChunks: Record = {}; const runtimeContracts: Readonly> = ROUTE_RUNTIME_CONTRACT; for (const definition of Object.values(ROUTE_REGISTRY)) { - const runtime = runtimeContracts[definition.routeId]; - const asset = Object.values(viteManifestObject).find( - (entry) => entry.name === runtime?.moduleId && entry.isDynamicEntry, + const chunk = findRouteChunk( + viteManifestObject, + definition.routeId, + runtimeContracts[definition.routeId]?.moduleId, ); - if (!runtime || !asset?.file) { - throw new Error(`Missing built route chunk: ${definition.routeId}`); - } - routeChunks[definition.chunkId] = asset.file; + if ("error" in chunk) throw new Error(chunk.error); + routeChunks[definition.chunkId] = chunk.file; } const runtimeConfigJsonSchema = z.toJSONSchema(runtimeConfigV2ArtifactSchema); diff --git a/scripts/lib/build-manifest-outputs.ts b/scripts/lib/build-manifest-outputs.ts index 0f7dea44..bdd8f268 100644 --- a/scripts/lib/build-manifest-outputs.ts +++ b/scripts/lib/build-manifest-outputs.ts @@ -186,3 +186,52 @@ export async function verifyBuildManifestOutputs( } return mismatches; } + +/** + * 0929/13-03. The built chunk of a route's lazy page, for the build manifest + * and the two release checks that compare its `routeChunks`. A route runtime's + * `moduleId` is the file name of the page it imports + * (`src/presentation/routes/route-runtime.tsx`, a generated feature's + * `-route-runtime.ts`), so the chunk is the dynamic entry whose source + * file — the Vite manifest key — has that name. The chunk's own `name` used to + * be matched instead, so a chunk-naming change in vite.config.ts stopped the + * build with a reason-less `Missing built route chunk`. + */ +export type RouteChunkLookup = Readonly<{ file: string }> | Readonly<{ error: string }>; + +const SOURCE_EXTENSION = /\.(?:tsx|ts|jsx|js)$/u; + +export function findRouteChunk( + manifest: Readonly>, + routeId: string, + moduleId: string | undefined, +): RouteChunkLookup { + if (moduleId === undefined) { + return { error: `Missing built route chunk: ${routeId} has no route runtime contract (ROUTE_RUNTIME_CONTRACT)` }; + } + const matches: { source: string; file: string }[] = []; + for (const [key, entry] of Object.entries(manifest)) { + if (!isManifestRecord(entry) || entry["isDynamicEntry"] !== true || typeof entry["file"] !== "string") continue; + const source = typeof entry["src"] === "string" ? entry["src"] : key; + if (path.posix.basename(source).replace(SOURCE_EXTENSION, "") === moduleId) { + matches.push({ source, file: entry["file"] }); + } + } + if (matches.length === 1) return { file: matches[0]!.file }; + if (matches.length === 0) { + return { + error: + `Missing built route chunk: ${routeId}: no lazily imported module in the Vite manifest has the source ` + + `file ${moduleId}.tsx (or .ts). A route runtime's moduleId must be the file name of the page it imports.`, + }; + } + return { + error: + `Ambiguous built route chunk: ${routeId}: the lazily imported modules ` + + `${matches.map(({ source }) => source).join(", ")} are all named ${moduleId}.`, + }; +} + +function isManifestRecord(value: unknown): value is Record { + return typeof value === "object" && value !== null && !Array.isArray(value); +} diff --git a/scripts/lib/local-release-evidence.ts b/scripts/lib/local-release-evidence.ts index 9149872e..3e1423c9 100644 --- a/scripts/lib/local-release-evidence.ts +++ b/scripts/lib/local-release-evidence.ts @@ -26,6 +26,7 @@ import { } from "../contracts/release-artifacts.ts"; import { CANONICAL_VITE_MANIFEST_PATH, + findRouteChunk, verifyBuildManifestOutputs, } from "./build-manifest-outputs.ts"; import { assertMatchesJsonSchema } from "./json-schema.ts"; @@ -1255,14 +1256,12 @@ async function verifyReleaseOutputs( const runtimeContracts: Readonly> = ROUTE_RUNTIME_CONTRACT; for (const definition of Object.values(ROUTE_REGISTRY)) { - const runtime = runtimeContracts[definition.routeId]; - const viteEntry = Object.values(viteManifest).find( - (entry) => - isRecord(entry) && - entry.name === runtime?.moduleId && - entry.isDynamicEntry === true, + const chunk = findRouteChunk( + viteManifest, + definition.routeId, + runtimeContracts[definition.routeId]?.moduleId, ); - const file = isRecord(viteEntry) ? viteEntry.file : null; + const file = "error" in chunk ? null : chunk.file; if ( typeof file !== "string" || release.routeChunks[definition.chunkId] !== file || diff --git a/scripts/verify-release.ts b/scripts/verify-release.ts index c4eb5a9f..c332cd69 100644 --- a/scripts/verify-release.ts +++ b/scripts/verify-release.ts @@ -24,6 +24,7 @@ import { import { assertMatchesJsonSchema } from "./lib/json-schema.ts"; import { CANONICAL_VITE_MANIFEST_PATH, + findRouteChunk, verifyBuildManifestOutputs, } from "./lib/build-manifest-outputs.ts"; import { verifyReleaseRuntimeCoherence } from "./lib/release-runtime-coherence.ts"; @@ -168,12 +169,13 @@ for (const chunkId of actualChunkIds) { const runtimeContracts: Readonly> = ROUTE_RUNTIME_CONTRACT; for (const definition of Object.values(ROUTE_REGISTRY)) { - const runtime = runtimeContracts[definition.routeId]; - const viteEntry = Object.values(viteManifestObject).find( - (entry) => entry.name === runtime?.moduleId && entry.isDynamicEntry, + const viteEntry = findRouteChunk( + viteManifestObject, + definition.routeId, + runtimeContracts[definition.routeId]?.moduleId, ); const routeAsset = release.routeChunks[definition.chunkId]; - if (!runtime || !viteEntry || routeAsset !== viteEntry.file) { + if ("error" in viteEntry || routeAsset !== viteEntry.file) { artifactMismatches.push(`routeChunk:mismatch:${definition.chunkId}`); continue; } diff --git a/tests/unit/route-chunks.test.ts b/tests/unit/route-chunks.test.ts new file mode 100644 index 00000000..99142cad --- /dev/null +++ b/tests/unit/route-chunks.test.ts @@ -0,0 +1,74 @@ +import { readFile } from "node:fs/promises"; + +import { describe, expect, it } from "vitest"; + +import { findRouteChunk } from "../../scripts/lib/build-manifest-outputs.ts"; + +// 0929/13-03. The route chunk was found by the chunk's `name`, so naming route +// chunks in vite.config.ts (the way tech-log did) stopped the build with +// `Missing built route chunk: APP_HOME` and no reason. It is found by the +// page's source file now, which is what the route runtime's moduleId names. +describe("route chunk lookup (0929/13-03)", () => { + const manifest = { + "index.html": { file: "assets/index-a.js", isEntry: true }, + "src/presentation/pages/home-page.tsx": { + file: "assets/route-home-page-b.js", + name: "route-home-page", + src: "src/presentation/pages/home-page.tsx", + isDynamicEntry: true, + }, + "src/features/order-feature/presentation/order-feature-list-page.tsx": { + file: "assets/order-feature-list-page-c.js", + name: "order-feature-list-page", + isDynamicEntry: true, + }, + "_home-page-shared.js": { file: "assets/home-page-d.js", name: "home-page" }, + }; + + it("finds the chunk by the page's source file even when the chunk has another name", () => { + expect(findRouteChunk(manifest, "APP_HOME", "home-page")).toEqual({ file: "assets/route-home-page-b.js" }); + expect(findRouteChunk(manifest, "ORDER_FEATURE_LIST", "order-feature-list-page")).toEqual({ + file: "assets/order-feature-list-page-c.js", + }); + }); + + it("names the reason and the rule when no lazily imported page has that file name", () => { + const lookup = findRouteChunk(manifest, "NOT_FOUND", "not-found-page"); + expect(lookup).toEqual({ + error: + "Missing built route chunk: NOT_FOUND: no lazily imported module in the Vite manifest has the source " + + "file not-found-page.tsx (or .ts). A route runtime's moduleId must be the file name of the page it imports.", + }); + }); + + it("refuses two lazily imported pages with the same file name", () => { + const lookup = findRouteChunk( + { ...manifest, "src/other/home-page.tsx": { file: "assets/home-page-e.js", isDynamicEntry: true } }, + "APP_HOME", + "home-page", + ); + expect(lookup).toEqual({ + error: + "Ambiguous built route chunk: APP_HOME: the lazily imported modules " + + "src/presentation/pages/home-page.tsx, src/other/home-page.tsx are all named home-page.", + }); + }); + + it("names a route without a runtime contract", () => { + expect(findRouteChunk(manifest, "GHOST", undefined)).toEqual({ + error: "Missing built route chunk: GHOST has no route runtime contract (ROUTE_RUNTIME_CONTRACT)", + }); + }); + + it("is the one lookup the manifest writer and both release checks use", async () => { + for (const file of [ + "scripts/generate-build-manifest.ts", + "scripts/verify-release.ts", + "scripts/lib/local-release-evidence.ts", + ]) { + const source = await readFile(file, "utf8"); + expect(source, file).toContain("findRouteChunk("); + expect(source, file).not.toMatch(/\.name === runtime\?\.moduleId/u); + } + }); +});