Files
aksel-mini/packages/core/tokens/build.mjs
T
Aksel Mini 7382e691ef ✨ Add tokens package (@navikt/ds-tokens)
Color, spacing, radius and typography tokens defined in src/tokens.json,
compiled to CSS custom properties on the --a-* namespace by build.mjs.
2026-08-04 10:30:00 +02:00

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`);