|
| 1 | +/** |
| 2 | + * Bundle the Tokens Studio CSS export into one committed file. |
| 3 | + * |
| 4 | + * Concatenates every generated CSS file under `src/tokens/css/` into |
| 5 | + * `src/tokens/css/variables.css` (ADR-0010). The bundle adds no |
| 6 | + * behaviour on top of the generated files — every mode file is |
| 7 | + * `:root`- or attribute-scoped, so plain concatenation is |
| 8 | + * conflict-free, and `var()` references are late-bound so source order |
| 9 | + * does not affect resolution. Files are still concatenated in sorted |
| 10 | + * path order to keep the committed artifact deterministic. |
| 11 | + * |
| 12 | + * The bundle is deliberately NOT minified: the committed file stays a |
| 13 | + * pure function of the source files (no toolchain-version churn in |
| 14 | + * release-PR diffs, no third-party minifier in the pipeline) and diffs |
| 15 | + * stay reviewable line by line. Gzip closes most of the size gap, and |
| 16 | + * consumers that bundle minify with their own tooling anyway. |
| 17 | + * |
| 18 | + * The directory is globbed rather than hard-coded so future changes to |
| 19 | + * the export set are followed automatically; only the output file |
| 20 | + * itself is excluded. |
| 21 | + * |
| 22 | + * On the beta line the committed bundle is importable as |
| 23 | + * `@equinor/eds-tokens/next/css/variables.css` through the |
| 24 | + * `./next/css/*` wildcard that publish_tokens.yaml injects. |
| 25 | + * |
| 26 | + * Usage: node scripts/generate-css-bundle.mjs [--css <dir>] [--out <file>] |
| 27 | + */ |
| 28 | +import { execFileSync } from 'node:child_process' |
| 29 | +import { readFile, readdir, writeFile } from 'node:fs/promises' |
| 30 | +import { join, relative, resolve } from 'node:path' |
| 31 | +import process from 'node:process' |
| 32 | +import { fileURLToPath } from 'node:url' |
| 33 | + |
| 34 | +const args = parseArgs(process.argv.slice(2)) |
| 35 | +const CSS_DIR = args.css ?? 'src/tokens/css' |
| 36 | +const OUT_FILE = args.out ?? join(CSS_DIR, 'variables.css') |
| 37 | + |
| 38 | +// Path-independent on purpose: the committed bytes must be a pure |
| 39 | +// function of the source file contents, never of how the script was |
| 40 | +// invoked |
| 41 | +const HEADER = |
| 42 | + '/* Do not edit directly — generated by scripts/generate-css-bundle.mjs (concatenation of the Tokens Studio CSS export) */\n' |
| 43 | + |
| 44 | +const files = (await readdir(CSS_DIR, { recursive: true })) |
| 45 | + .filter((file) => file.endsWith('.css')) |
| 46 | + .map((file) => join(CSS_DIR, file)) |
| 47 | + .filter((file) => resolve(file) !== resolve(OUT_FILE)) |
| 48 | + .sort() |
| 49 | + |
| 50 | +if (files.length === 0) fail(`no CSS files found under ${CSS_DIR}`) |
| 51 | + |
| 52 | +const concatenated = ( |
| 53 | + await Promise.all(files.map((file) => readFile(file, 'utf8'))) |
| 54 | +).join('\n') |
| 55 | + |
| 56 | +await writeFile(OUT_FILE, HEADER + concatenated) |
| 57 | + |
| 58 | +// Regression guard reused from the 2.x build: the bundle must contain no |
| 59 | +// light-dark() literals or lightningcss polyfill markers, and both |
| 60 | +// color-scheme scopes must be present (the new export has exactly one |
| 61 | +// block per scheme, hence the threshold of 1) |
| 62 | +execFileSync( |
| 63 | + process.execPath, |
| 64 | + [ |
| 65 | + fileURLToPath(new URL('assert-no-light-dark.mjs', import.meta.url)), |
| 66 | + OUT_FILE, |
| 67 | + '1', |
| 68 | + ], |
| 69 | + { stdio: 'inherit' }, |
| 70 | +) |
| 71 | + |
| 72 | +console.log( |
| 73 | + `generate-css-bundle: wrote ${OUT_FILE} from ${files.length} files (${files |
| 74 | + .map((file) => relative(CSS_DIR, file)) |
| 75 | + .join(', ')})`, |
| 76 | +) |
| 77 | + |
| 78 | +function parseArgs(argv) { |
| 79 | + const out = {} |
| 80 | + for (let i = 0; i < argv.length; i += 2) { |
| 81 | + const key = argv[i]?.replace(/^--/, '') |
| 82 | + const value = argv[i + 1] |
| 83 | + if (!key || !value) fail(`invalid arguments: ${argv.join(' ')}`) |
| 84 | + out[key] = value |
| 85 | + } |
| 86 | + return out |
| 87 | +} |
| 88 | + |
| 89 | +function fail(message) { |
| 90 | + console.error(`generate-css-bundle: ${message}`) |
| 91 | + process.exit(1) |
| 92 | +} |
0 commit comments