// The CLJS compiler numbers protocol `…$dyn_<N>` vars with the JVM-global
// gensym counter, so two otherwise-identical release builds differ in those
// digits alone (verified: renumbering them is the ONLY delta between clean
// builds). These vars are file-local and only ever referenced within the same
// file, so a consistent per-file renumbering in first-occurrence order is
// semantics-preserving. Canon copy (train-kit), adapted: the hub emits TWO
// asset dirs, dist/assets (the shell) and dist/id/assets (the bridge build).
import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
const DIRS = [
new URL("../dist/assets", import.meta.url).pathname,
new URL("../dist/id/assets", import.meta.url).pathname,
];
const DYN = /([A-Za-z0-9_$]+\$dyn)_(\d+)/g;
for (const dir of DIRS) {
if (!existsSync(dir)) continue;
for (const name of readdirSync(dir)) {
if (!name.endsWith(".js")) continue;
const file = join(dir, name);
const src = readFileSync(file, "utf8");
const mapping = new Map();
const out = src.replace(DYN, (m, stem) => {
if (!mapping.has(m)) mapping.set(m, `${stem}_${mapping.size + 1}`);
return mapping.get(m);
});
if (out !== src) writeFileSync(file, out);
}
}
console.log("normalize-gensyms: dyn vars renumbered deterministically");