GrepBoard/Manuals
605 manuals ·

Move Design Tokens from JSON into Tailwind v4 @theme

·Tested on Node.js 24 · tailwindcss 4.3.3 · @tailwindcss/cli 4.3.3·Test scope ↓·8 steps

Convert a token JSON file into an @theme block with a script, then see in the build output that a name outside Tailwind namespaces makes no utility class.

Put @theme { --brand-color: #ff5f0b; } in a Tailwind v4 project, use text-brand-color, and the build succeeds with nothing in the CSS. When you move token JSON into @theme, the variable name is the usual cause, so the converter below checks names.

[1] Install in an empty folder.

console
$ npm init -y$ npm install tailwindcss @tailwindcss/cli$ npx tailwindcss --help | head -1≈ tailwindcss v4.3.3

[2] Create tokens.json. It uses the same $value format as the Style Dictionary post. The top-level keys (color, spacing, radius, font) become the start of each variable name.

tokens.json
{  "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] Create the converter tools/tokens-to-theme.mjs. It joins the JSON path with hyphens (--color-brand-primary) and joins array values with commas. If the name does not start with a Tailwind namespace, it writes nothing and exits with code 1.

tools/tokens-to-theme.mjs
// 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] Convert.

console
$ 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] Load the result from the entry CSS, src/app.css. This is @import "tailwindcss"; split into parts. source(none) turns off automatic class detection and @source points at index.html only, so the output contains just the test classes. In a real project, @import "tailwindcss"; is enough.

src/app.css
@import "tailwindcss/theme.css" layer(theme);@import "tailwindcss/utilities.css" layer(utilities) source(none);@source "../index.html";@import "./theme.css";

[6] Put the token names in an HTML file as classes and build.

console
$ 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);  }}

Six variables landed in :root and six classes were generated. The docs say --spacing-* feeds spacing and sizing utilities in general, such as px-4 and max-h-16.

[7] Break the names on purpose. bad.json has three names outside the namespaces and one valid name.

bad.json
{  "colors": { "brand": { "$value": "#0b5fff" } },  "brand-color": { "$value": "#ff5f0b" },  "color": { "ok": { "$value": "#00aa55" } },  "gutter": { "$value": "24px" }}
console
$ node tools/tokens-to-theme.mjs bad.json src/theme.cssno Tailwind namespace: --colors-brand --brand-color --gutter$ echo $?1

Take the check out of the script, put the same names in @theme, and build <div class="bg-brand text-brand-color bg-ok p-gutter">:

console
$ 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 and p-gutter produced no class, no variable and no error message. colors (plural), brand-color (reversed order) and gutter (no prefix) all behave the same way.

[8] Two more things, checked with the same build.

  • Unused tokens are left out of the output CSS. To keep every variable, use @theme static { … }. In the test, only --color-used survived from @theme (the class was used, --color-unused was not), while --color-kept from @theme static stayed even though nothing used it.
  • :root { --color-rootonly: #444; } is not @theme: the variable is emitted as is, but bg-rootonly is not generated. Put tokens that need no utility in :root.

Run AI-generated token JSON through the script in [3] first. A token with a wrong name then fails at conversion instead of disappearing without a trace.

#design-tokens#tailwind#css