import { existsSync, readFileSync } from "node:fs"; import path from "node:path"; import { describe, expect, it } from "vitest"; /** * 1105/15-03. Two copies of the list of files `vite build` reads from the * root (the initial JavaScript measurement and the * `VITE_PRODUCT_FEATURES=none` build test) had to change together each time * vite.config.ts imported a new local file (7f9a515, 799ab7f), and a missed * copy failed with an unexplained "Could not resolve". There is one list * now, and it has to cover what vite.config.ts imports. */ const LIST_MODULE = "scripts/lib/vite-build-inputs.ts"; const MEASUREMENT = "scripts/scale/measure-feature-shape-growth.ts"; // The build test and the feature-shape measurement are template files and // are always read (1106/10-01: filtering on "the file exists" let a renamed // consumer drop out of the check unnoticed). 2026-11-07: the measurement used // to copy the reference sample and went with it; it copies the generated // feature now and stays. const CONSUMERS = ["tests/unit/product-feature-selection-build.test.ts", MEASUREMENT]; const LOCAL_IMPORT = /\bfrom\s+["'](\.{1,2}\/[^"']+)["']|\bimport\s+["'](\.{1,2}\/[^"']+)["']/gu; /** Every repository file vite.config.ts reaches through relative imports. */ function localImportGraph(entry: string): string[] { const seen = new Set(); const pending = [entry]; while (pending.length > 0) { const file = pending.pop()!; if (seen.has(file)) continue; seen.add(file); const source = readFileSync(file, "utf8"); for (const match of source.matchAll(LOCAL_IMPORT)) { const specifier = match[1] ?? match[2]!; const resolved = path .relative(process.cwd(), path.resolve(path.dirname(file), specifier)) .split(path.sep) .join("/"); if (existsSync(resolved)) pending.push(resolved); } } return [...seen]; } describe("vite build inputs (1105/15-03)", () => { it("defines the list in one module that both builds import", () => { for (const consumer of CONSUMERS) { const source = readFileSync(consumer, "utf8"); expect(source, consumer).not.toMatch(/\bconst BUILD_INPUTS\s*=/u); expect(source, consumer).toMatch(/\bVITE_BUILD_INPUTS\b/u); expect(source, consumer).toContain("vite-build-inputs.ts"); } }); it("covers every local file vite.config.ts imports", async () => { // Imported here, not at the top, so the first test still reports the // copies when the module is missing. const { VITE_BUILD_INPUTS } = await import("../../scripts/lib/vite-build-inputs.ts"); const covered = (file: string) => VITE_BUILD_INPUTS.some((input) => file === input || file.startsWith(`${input}/`)); const missing = localImportGraph("vite.config.ts").filter((file) => !covered(file)); expect( missing, `vite.config.ts reads files ${LIST_MODULE} does not copy; add them there`, ).toEqual([]); }); });