diff --git a/scripts/lib/vite-build-inputs.ts b/scripts/lib/vite-build-inputs.ts new file mode 100644 index 00000000..bd039d04 --- /dev/null +++ b/scripts/lib/vite-build-inputs.ts @@ -0,0 +1,20 @@ +/** + * 1105/15-03. The files `vite build` reads from the repository root, for the + * measurements and tests that copy them into a temporary root and build + * there: scripts/scale/measure-feature-shape-growth.ts (initial JavaScript + * record) and tests/unit/product-feature-selection-build.test.ts + * (`VITE_PRODUCT_FEATURES=none`). Each used to keep its own copy of this list; + * a new local import in vite.config.ts broke both, and both had to be edited + * (7f9a515, 799ab7f). tests/unit/vite-build-inputs.test.ts fails, naming the + * file, when vite.config.ts imports a local file this list does not cover. + */ +export const VITE_BUILD_INPUTS = Object.freeze([ + "src", + "public", + "index.html", + "package.json", + "vite.config.ts", + "tsconfig.base.json", + "tsconfig.app.json", + "scripts/lib/vite-module-inventory.ts", +] as const); diff --git a/scripts/scale/measure-feature-shape-growth.ts b/scripts/scale/measure-feature-shape-growth.ts index 94b6e6b5..a90ac987 100644 --- a/scripts/scale/measure-feature-shape-growth.ts +++ b/scripts/scale/measure-feature-shape-growth.ts @@ -7,6 +7,7 @@ import { gzipSync } from "node:zlib"; import { classifyViteJavascript } from "../lib/classify-vite-bundle.ts"; import { linkFixtureNodeModules } from "../lib/fixture-node-modules.ts"; +import { VITE_BUILD_INPUTS } from "../lib/vite-build-inputs.ts"; import { parseTscExtendedDiagnostics, type TscDiagnostics } from "./collect-tsc-diagnostics.ts"; /** @@ -37,16 +38,6 @@ export const FEATURE_SHAPE_CEILINGS_PATH = "config/scale/feature-shape-ceilings. const SOURCE_FEATURE = "src/features/reference-feature"; const FEATURES_PER_CONTEXT = 200; -const BUILD_INPUTS = [ - "src", - "public", - "index.html", - "package.json", - "vite.config.ts", - "tsconfig.base.json", - "tsconfig.app.json", - "scripts/lib/vite-module-inventory.ts", -] as const; // Platform-owned names that only happen to contain "reference". const PLATFORM_TOKENS = [ "REFERENCE_FEATURE_V1", @@ -110,7 +101,7 @@ async function copyRenamed(from: string, to: string, rename: (text: string) => s async function createFixture(features: number): Promise { const root = await mkdtemp(path.join(tmpdir(), `feature-shape-${features}-`)); - for (const input of BUILD_INPUTS) { + for (const input of VITE_BUILD_INPUTS) { await mkdir(path.dirname(path.join(root, input)), { recursive: true }); await cp(path.resolve(input), path.join(root, input), { recursive: true }); } diff --git a/tests/unit/product-feature-selection-build.test.ts b/tests/unit/product-feature-selection-build.test.ts index 2a8242a3..c9e665ea 100644 --- a/tests/unit/product-feature-selection-build.test.ts +++ b/tests/unit/product-feature-selection-build.test.ts @@ -8,6 +8,7 @@ import { afterAll, describe, expect, it } from "vitest"; import { linkFixtureNodeModules } from "../../scripts/lib/fixture-node-modules.ts"; import { filesBelow } from "../../scripts/lib/removal-fixture.ts"; +import { VITE_BUILD_INPUTS } from "../../scripts/lib/vite-build-inputs.ts"; import { ROUTE_FEATURE_OWNER } from "../../src/features/installed-feature-contracts.ts"; import { COMPILED_PRODUCT_FEATURE_IDS } from "../../src/features/installed-product-manifest.ts"; @@ -16,19 +17,10 @@ import { COMPILED_PRODUCT_FEATURE_IDS } from "../../src/features/installed-produ * every consumer, not only the browser bundle: the generated context index * (router), the installed contract contributions (Node contract-set * generation) and the built assets. This runs the production generator and a - * Vite build over a copy of the build inputs. Written against whatever - * features the tree declares, so it holds after the sample's removal too. + * Vite build over a copy of the build inputs (scripts/lib/vite-build-inputs.ts, + * 1105/15-03). Written against whatever features the tree declares, so it + * holds after the sample's removal too. */ -const BUILD_INPUTS = [ - "src", - "public", - "index.html", - "package.json", - "vite.config.ts", - "tsconfig.base.json", - "tsconfig.app.json", - "scripts/lib/vite-module-inventory.ts", -] as const; // Feature ids stay: the generated index still declares them so the platform // overview reports NOT_INSTALLED. Their modules and route ids go. const FEATURE_ROUTE_IDS = Object.keys(ROUTE_FEATURE_OWNER); @@ -44,7 +36,7 @@ describe("VITE_PRODUCT_FEATURES=none build (09-02)", () => { it("leaves every feature out of the context index, the contract set and dist", async () => { const root = await mkdtemp(path.join(tmpdir(), "product-selection-build-")); roots.push(root); - for (const input of BUILD_INPUTS) { + for (const input of VITE_BUILD_INPUTS) { await mkdir(path.dirname(path.join(root, input)), { recursive: true }); await cp(path.resolve(input), path.join(root, input), { recursive: true }); } diff --git a/tests/unit/vite-build-inputs.test.ts b/tests/unit/vite-build-inputs.test.ts new file mode 100644 index 00000000..228272c1 --- /dev/null +++ b/tests/unit/vite-build-inputs.test.ts @@ -0,0 +1,72 @@ +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"; +// The measurement goes with the reference feature it copies +// (test:sample-removal), so a project without that feature checks the build +// test alone. In the template, tests/unit/feature-shape-growth.test.ts +// imports the measurement and fails if the file is gone. +const CONSUMERS = [ + "scripts/scale/measure-feature-shape-growth.ts", + "tests/unit/product-feature-selection-build.test.ts", +].filter((file) => existsSync(file)); + +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([]); + }); +});