Files
Jyotisha/frontend/scripts/check-built-css.mjs
ClaudeandClaude Fable 5.2 2fa61ecd40
Independent Staging Quality Gate / validate-frontend (push) Successful in 5m34s
Independent Staging Quality Gate / validate-python (push) Successful in 7m12s
Independent Staging Quality Gate / publish (push) Canceled after 0s
fix(deploy): the web image fails to build when it carries an older globals.css; fresh Next build cache (BUG-2069)
The e4b25c26 staging image shipped new settings markup with the previous
build's global style sheet. The image build now uses a new cache id and
checks, after next build, that every class opening a rule in globals.css
is present in the built style sheets.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8
2026-11-20 12:00:13 +08:00

50 lines
2.2 KiB
JavaScript

// 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");
}