Color, spacing, radius and typography tokens defined in src/tokens.json, compiled to CSS custom properties on the --a-* namespace by build.mjs.
32 lines
984 B
JavaScript
32 lines
984 B
JavaScript
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
import { dirname, join } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const root = dirname(fileURLToPath(import.meta.url));
|
|
const tokens = JSON.parse(readFileSync(join(root, "src", "tokens.json"), "utf8"));
|
|
|
|
function flatten(node, prefix = []) {
|
|
return Object.entries(node).flatMap(([key, value]) =>
|
|
typeof value === "object" && "value" in value
|
|
? [[prefix.concat(key).join("-"), value.value]]
|
|
: flatten(value, prefix.concat(key)),
|
|
);
|
|
}
|
|
|
|
const lines = flatten(tokens)
|
|
.sort(([a], [b]) => a.localeCompare(b))
|
|
.map(([name, value]) => ` --a-${name}: ${value};`);
|
|
|
|
const css = `/**
|
|
* Design tokens — generated by build.mjs from src/tokens.json. Do not edit manually.
|
|
*/
|
|
:root {
|
|
${lines.join("\n")}
|
|
}
|
|
`;
|
|
|
|
mkdirSync(join(root, "dist"), { recursive: true });
|
|
writeFileSync(join(root, "dist", "tokens.css"), css);
|
|
|
|
console.log(`Emitted ${lines.length} tokens to dist/tokens.css`);
|