디자인 토큰 JSON 을 Tailwind v4 @theme 로 옮기기
토큰 JSON 을 @theme 블록으로 바꾸는 스크립트를 만들고, 변수 이름이 네임스페이스 규칙을 어기면 유틸리티가 생기지 않는 것을 빌드 결과로 확인합니다.
Tailwind v4 에서 @theme { --brand-color: #ff5f0b; } 를 써 놓고 text-brand-color 를 쓰면 빌드는 성공하는데 CSS 에는 아무것도 나오지 않습니다. 토큰 JSON 을 @theme 로 옮길 때 변수 이름이 원인인 경우가 대부분이라, 변환 스크립트에 이름 검사를 넣습니다.
[1] 빈 폴더에 설치합니다.
$ npm init -y$ npm install tailwindcss @tailwindcss/cli$ npx tailwindcss --help | head -1≈ tailwindcss v4.3.3[2] 토큰 파일 tokens.json 을 만듭니다. 형식은 Style Dictionary 글과 같은 $value 입니다. 최상위 키(color·spacing·radius·font)가 곧 변수 이름의 앞부분이 됩니다.
{ "color": { "brand": { "primary": { "$value": "#0b5fff" }, "soft": { "$value": "#e6eeff" } }, "surface": { "$value": "#f4f5f7" } }, "spacing": { "gutter": { "$value": "24px" } }, "radius": { "card": { "$value": "12px" } }, "font": { "body": { "$value": ["Inter", "system-ui", "sans-serif"] } }}[3] 변환 스크립트 tools/tokens-to-theme.mjs 를 만듭니다. JSON 경로를 하이픈으로 이어 --color-brand-primary 꼴로 쓰고, 배열 값은 쉼표로 잇습니다. 앞부분이 Tailwind 네임스페이스 목록에 없으면 파일을 쓰지 않고 종료 코드 1 로 멈춥니다.
// usage: node tokens-to-theme.mjs tokens.json out.cssimport { readFileSync, writeFileSync } from 'node:fs'; const NAMESPACES = ['color', 'font', 'text', 'font-weight', 'tracking', 'leading', 'tab-size', 'breakpoint', 'container', 'spacing', 'radius', 'shadow', 'inset-shadow', 'drop-shadow', 'blur', 'perspective', 'zoom', 'aspect', 'ease', 'animate']; const [src, out] = process.argv.slice(2);const tree = JSON.parse(readFileSync(src, 'utf8'));const lines = [];const bad = []; function walk(node, path) { if ('$value' in node) { const name = path.join('-'); const v = Array.isArray(node.$value) ? node.$value.join(', ') : node.$value; if (!NAMESPACES.some((ns) => name.startsWith(ns + '-'))) bad.push(name); lines.push(` --${name}: ${v};`); return; } for (const [k, child] of Object.entries(node)) { if (!k.startsWith('$')) walk(child, [...path, k]); }}walk(tree, []); if (bad.length) { console.error('no Tailwind namespace: ' + bad.map((n) => '--' + n).join(' ')); process.exit(1);}writeFileSync(out, `@theme {\n${lines.join('\n')}\n}\n`);[4] 변환합니다.
$ node tools/tokens-to-theme.mjs tokens.json src/theme.css$ cat src/theme.css@theme { --color-brand-primary: #0b5fff; --color-brand-soft: #e6eeff; --color-surface: #f4f5f7; --spacing-gutter: 24px; --radius-card: 12px; --font-body: Inter, system-ui, sans-serif;}[5] 진입 CSS src/app.css 에서 불러 옵니다. 아래는 @import "tailwindcss"; 를 쪼갠 형태입니다. source(none) 으로 자동 클래스 탐색을 끄고 @source 로 index.html 만 지정해, 출력에 시험용 클래스만 남깁니다. 실제 프로젝트에서는 @import "tailwindcss"; 한 줄이면 됩니다.
@import "tailwindcss/theme.css" layer(theme);@import "tailwindcss/utilities.css" layer(utilities) source(none);@source "../index.html";@import "./theme.css";[6] 토큰 이름을 클래스로 쓴 HTML 을 두고 빌드합니다.
$ echo '<div class="bg-brand-primary text-brand-soft p-gutter rounded-card font-body bg-surface"></div>' > index.html$ npx @tailwindcss/cli -i src/app.css -o out.css≈ tailwindcss v4.3.3 Done in 66ms$ cat out.css/*! tailwindcss v4.3.3 | MIT License | https://tailwindcss.com */@layer theme { :root, :host { --color-brand-primary: #0b5fff; --color-brand-soft: #e6eeff; --color-surface: #f4f5f7; --spacing-gutter: 24px; --radius-card: 12px; --font-body: Inter, system-ui, sans-serif; }}@layer utilities { .rounded-card { border-radius: var(--radius-card); } .bg-brand-primary { background-color: var(--color-brand-primary); } .bg-surface { background-color: var(--color-surface); } .p-gutter { padding: var(--spacing-gutter); } .font-body { font-family: var(--font-body); } .text-brand-soft { color: var(--color-brand-soft); }}여섯 변수가 :root 에 들어갔고 클래스 여섯 개가 생겼습니다. 문서는 --spacing-* 가 px-4·max-h-16 같은 간격·크기 유틸리티 전반에 쓰인다고 설명합니다.
[7] 이름을 일부러 틀려 봅니다. bad.json 에 네임스페이스에 없는 이름 세 개와 맞는 이름 한 개를 둡니다.
{ "colors": { "brand": { "$value": "#0b5fff" } }, "brand-color": { "$value": "#ff5f0b" }, "color": { "ok": { "$value": "#00aa55" } }, "gutter": { "$value": "24px" }}$ node tools/tokens-to-theme.mjs bad.json src/theme.cssno Tailwind namespace: --colors-brand --brand-color --gutter$ echo $?1스크립트의 검사를 빼고 @theme 에 그대로 넣은 뒤 <div class="bg-brand text-brand-color bg-ok p-gutter"> 를 빌드하면 이렇게 됩니다.
$ npx @tailwindcss/cli -i src/app.css -o out2.css$ cat out2.css/*! tailwindcss v4.3.3 | MIT License | https://tailwindcss.com */@layer theme { :root, :host { --color-ok: #00aa55; }}@layer utilities { .bg-ok { background-color: var(--color-ok); }}bg-brand·text-brand-color·p-gutter 는 클래스도, 변수도 나오지 않았고 오류 메시지도 없었습니다. colors(복수)·brand-color(순서 반대)·gutter(앞부분 없음) 모두 같은 결과입니다.
[8] 알아 둘 것 두 가지입니다. 같은 빌드로 확인했습니다.
- 쓰지 않는 토큰은 출력 CSS 에 없습니다. 모든 변수를 남기려면
@theme static { … }을 씁니다. 시험에서@theme의--color-used·--color-unused중 클래스로 쓴used만 남았고,@theme static의--color-kept는 쓰지 않아도 남았습니다. :root { --color-rootonly: #444; }는@theme가 아니라서 변수는 그대로 나가지만bg-rootonly클래스는 생기지 않습니다. 유틸리티가 필요 없는 토큰은:root에 둡니다.
AI 가 만든 토큰 JSON 은 [3] 의 스크립트를 먼저 통과시킵니다. 이름이 틀린 토큰이 조용히 사라지는 일을 변환 단계에서 막습니다.