// The web image must carry the style rules of the source it was built from. // On 2026-11-20 a staging image shipped new pages with the previous build's // globals.css (BUG-2069): the markup was new, its class names had no rules. // This runs right after `next build` and fails the image build when a class // that starts a rule in globals.css is absent from every built sheet. import { readdirSync, readFileSync, statSync } from "node:fs"; import { join } from "node:path"; import { pathToFileURL } from "node:url"; function cssFiles(dir) { const found = []; for (const name of readdirSync(dir)) { const path = join(dir, name); if (statSync(path).isDirectory()) found.push(...cssFiles(path)); else if (name.endsWith(".css") && !name.endsWith(".module.css")) found.push(path); } return found; } /** Class names that open a rule at the start of a line in a source style sheet. */ export function sourceClassNames(css) { const names = new Set(); for (const match of css.replace(/\/\*[\s\S]*?\*\//g, "").matchAll(/^[ \t]*\.([A-Za-z_][\w-]*)/gm)) names.add(match[1]); return names; } export function missingClassNames(sourceFiles, builtDir) { const built = cssFiles(builtDir).map((path) => readFileSync(path, "utf8")).join("\n"); const builtNames = new Set([...built.matchAll(/\.([A-Za-z_][\w-]*)/g)].map((match) => match[1])); const missing = []; for (const path of sourceFiles) { for (const name of sourceClassNames(readFileSync(path, "utf8"))) { if (!builtNames.has(name)) missing.push(`${path}: .${name}`); } } return missing; } if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) { // globals.css is loaded by every page, so its rules are always in the build. const [sourceFile = "src/app/globals.css", builtDir = ".next/static"] = process.argv.slice(2); const missing = missingClassNames([sourceFile], builtDir); if (missing.length > 0) { console.error(`built style sheets are missing ${missing.length} class(es) from the source; the build reused old CSS:`); for (const line of missing.slice(0, 20)) console.error(` ${line}`); process.exit(1); } console.log("built style sheets carry every source class"); }