|
| 1 | +# Design System Export — Consumers & Round-Trip Contract |
| 2 | + |
| 3 | +`/export-design-system` turns a project's `design-tokens.lock.json` + components |
| 4 | +into a publishable pnpm workspace (see the `export-design-system` skill for the |
| 5 | +full output layout). This document defines **what a downstream project imports** |
| 6 | +and the **round-trip guarantee** that makes the export a reliable interchange. |
| 7 | + |
| 8 | +## The interchange contract |
| 9 | + |
| 10 | +The exported `@scope/design-tokens` package ships four token artifacts. Two are |
| 11 | +lossless reconstructions of the source lockfile; two are derived, ergonomic |
| 12 | +_views_: |
| 13 | + |
| 14 | +| Artifact | Contents | Lossless? | Use it for | |
| 15 | +|----------|----------|-----------|------------| |
| 16 | +| `tokens.json` | verbatim JSON snapshot of `design-tokens.lock.json` | **Yes** | machine interchange, re-import, custom tooling | |
| 17 | +| `tokens.ts` | `export const tokens = <lockfile> as const` | **Yes** | typed, tree-shakeable access in TS/JS consumers | |
| 18 | +| `tokens.css` | flattened `--color-* / --space-* / --radius-*` custom properties | No | drop-in theming for web UIs | |
| 19 | +| `tailwind-preset.ts` | `{ colors, spacing, borderRadius, fontFamily }` subset | No | extend a consumer's Tailwind config | |
| 20 | + |
| 21 | +**`tokens.json` is the canonical interchange.** It is a byte-for-byte snapshot of |
| 22 | +the lockfile, so any consumer that reads it sees exactly the tokens the design |
| 23 | +system was locked to. `tokens.css` and `tailwind-preset.ts` are one-way |
| 24 | +projections — they intentionally drop structure (nested color scales, typography |
| 25 | +families, `textContent`, `rgb`/`tailwind` metadata) and therefore **cannot** be |
| 26 | +reversed back into a lockfile. |
| 27 | + |
| 28 | +## Round-trip guarantee |
| 29 | + |
| 30 | +`import-design-tokens` is the inverse of the exporter and the reference consumer. |
| 31 | +It reads the lossless `tokens.json` and reconstructs an identical |
| 32 | +`design-tokens.lock.json`: |
| 33 | + |
| 34 | +``` |
| 35 | +lock ──/export-design-system──▶ @scope/design-tokens ──import-design-tokens──▶ lock′ |
| 36 | + lock′ ≡ lock |
| 37 | +``` |
| 38 | + |
| 39 | +```bash |
| 40 | +# Reconstruct a lockfile from an exported workspace |
| 41 | +./scripts/import-design-tokens.sh --from dist/design-system --out design-tokens.lock.json |
| 42 | + |
| 43 | +# From a published/installed package, or a raw tokens.json path |
| 44 | +./scripts/import-design-tokens.sh --from node_modules/@acme/design-tokens/dist/tokens.json --out - |
| 45 | + |
| 46 | +# Verify a round-trip in CI (exit 0 when identical, 1 when it drifts) |
| 47 | +./scripts/import-design-tokens.sh --from dist/design-system --verify src/styles/design-tokens.lock.json |
| 48 | +``` |
| 49 | + |
| 50 | +The `--verify` mode is the executable form of the guarantee: it fails loudly if an |
| 51 | +export ever stops being a faithful representation of its source tokens. The |
| 52 | +automated proof lives in `scripts/__tests__/design-system-roundtrip.test.js` |
| 53 | +(tokens → export → reimport → deep-equal). |
| 54 | + |
| 55 | +`--from` accepts an export root, a `design-tokens` package directory, or a |
| 56 | +`tokens.json` file directly. It probes, in order: |
| 57 | +`packages/design-tokens/src/tokens.json` → `.../dist/tokens.json` → |
| 58 | +`src/tokens.json` → `dist/tokens.json` → `tokens.json`. |
| 59 | + |
| 60 | +## Consumers |
| 61 | + |
| 62 | +### Aurelius (self-consumer) |
| 63 | + |
| 64 | +Aurelius re-imports its own export to move a locked design system between |
| 65 | +projects, or to validate that a hand-edited token package still round-trips. This |
| 66 | +is the `import-design-tokens` path above. |
| 67 | + |
| 68 | +### Flavian — WordPress / FSE `theme.json` |
| 69 | + |
| 70 | +[Flavian](https://github.com/PMDevSolutions/Flavian) is a Claude Code-integrated |
| 71 | +WordPress framework. A block theme's design surface is `theme.json`, so the |
| 72 | +consumer installs the tokens package and maps `tokens.json` into |
| 73 | +`theme.json.settings`: |
| 74 | + |
| 75 | +| Lockfile section | `theme.json` target | |
| 76 | +|------------------|---------------------| |
| 77 | +| `colors.semantic.<k>.hex` (fallback `colors.primitives`) | `settings.color.palette[]` — `{ slug, color, name }` | |
| 78 | +| `typography.families.<k>` (`value` + `fallback`) | `settings.typography.fontFamilies[]` — `{ slug, fontFamily, name }` | |
| 79 | +| `spacing.scale.<k>.px` | `settings.spacing.spacingSizes[]` — `{ slug, size: "<px>px", name }` | |
| 80 | +| `borderRadius.<k>.px` | `settings.custom.radius.<k>` → emits `--wp--custom--radius--<k>` | |
| 81 | + |
| 82 | +Example adapter (run in the theme's build step): |
| 83 | + |
| 84 | +```js |
| 85 | +// scripts/tokens-to-theme-json.mjs |
| 86 | +import { readFileSync, writeFileSync } from "node:fs"; |
| 87 | +// From the installed package: @acme/design-tokens/tokens.json |
| 88 | +import tokens from "@acme/design-tokens/tokens.json" with { type: "json" }; |
| 89 | + |
| 90 | +const palette = Object.entries(tokens.colors?.semantic ?? {}).map(([slug, v]) => ({ |
| 91 | + slug, |
| 92 | + color: v.hex, |
| 93 | + name: slug.replace(/(^|-)\w/g, (m) => m.toUpperCase().replace("-", " ")), |
| 94 | +})); |
| 95 | + |
| 96 | +const fontFamilies = Object.entries(tokens.typography?.families ?? {}).map(([slug, v]) => ({ |
| 97 | + slug, |
| 98 | + fontFamily: [v.value, v.fallback].filter(Boolean).join(", "), |
| 99 | + name: slug, |
| 100 | +})); |
| 101 | + |
| 102 | +const spacingSizes = Object.entries(tokens.spacing?.scale ?? {}).map(([slug, v]) => ({ |
| 103 | + slug, |
| 104 | + size: `${v.px}px`, |
| 105 | + name: slug, |
| 106 | +})); |
| 107 | + |
| 108 | +const radius = Object.fromEntries( |
| 109 | + Object.entries(tokens.borderRadius ?? {}).map(([k, v]) => [k, `${v.px}px`]), |
| 110 | +); |
| 111 | + |
| 112 | +const themeJson = { |
| 113 | + version: 3, |
| 114 | + settings: { |
| 115 | + color: { palette }, |
| 116 | + typography: { fontFamilies }, |
| 117 | + spacing: { spacingSizes }, |
| 118 | + custom: { radius }, |
| 119 | + }, |
| 120 | +}; |
| 121 | + |
| 122 | +writeFileSync("theme.json", JSON.stringify(themeJson, null, 2) + "\n"); |
| 123 | +``` |
| 124 | +
|
| 125 | +Bumping the tokens package version (via Changesets) and re-running the adapter is |
| 126 | +all that is needed to propagate a token change into the theme. |
| 127 | +
|
| 128 | +### Nerva — Hono / Cloudflare Workers backend |
| 129 | +
|
| 130 | +[Nerva](https://github.com/PMDevSolutions/Nerva) is a Claude Code-integrated API |
| 131 | +framework (Hono, Cloudflare Workers, Drizzle ORM). Backends need tokens for |
| 132 | +server-rendered surfaces — transactional emails, PDFs, or a design-tokens API — |
| 133 | +where CSS is not available. The consumer imports the **typed** tokens, which are |
| 134 | +tree-shakeable and require no runtime CSS: |
| 135 | +
|
| 136 | +```ts |
| 137 | +import { Hono } from "hono"; |
| 138 | +import { tokens, type Tokens } from "@acme/design-tokens"; // typed, from tokens.ts |
| 139 | + |
| 140 | +const app = new Hono(); |
| 141 | + |
| 142 | +// Serve the canonical tokens to any client (mobile app, docs site, design tool) |
| 143 | +app.get("/design-tokens", (c) => c.json(tokens)); |
| 144 | + |
| 145 | +// Or use tokens directly when rendering an email template server-side |
| 146 | +function emailButton(label: string) { |
| 147 | + const primary = tokens.colors.semantic.primary.hex; |
| 148 | + const radius = tokens.borderRadius.md.px; |
| 149 | + return `<a style="background:${primary};border-radius:${radius}px;color:#fff">${label}</a>`; |
| 150 | +} |
| 151 | + |
| 152 | +export default app; |
| 153 | +``` |
| 154 | +
|
| 155 | +Because `@acme/design-tokens` is framework-agnostic and side-effect free (apart |
| 156 | +from the optional `tokens.css`), it bundles cleanly into a Workers runtime. |
| 157 | +
|
| 158 | +## Style Dictionary |
| 159 | +
|
| 160 | +`tokens.json` is a plain nested map, so a consumer that standardizes on |
| 161 | +[Style Dictionary](https://styledictionary.com/) can register it as a custom |
| 162 | +source and run its own transforms. Aurelius does **not** require a Style |
| 163 | +Dictionary build step — it ships ready-to-use CSS variables, a typed TS export, |
| 164 | +and a Tailwind preset directly — but nothing prevents layering Style Dictionary on |
| 165 | +top of the lossless `tokens.json`. |
| 166 | +
|
| 167 | +## Reference |
| 168 | +
|
| 169 | +- Exporter: `scripts/export-design-system.js` (skill: `export-design-system`, command: `/export-design-system`) |
| 170 | +- Re-importer: `scripts/import-design-tokens.js` / `scripts/import-design-tokens.sh` |
| 171 | +- Round-trip test: `scripts/__tests__/design-system-roundtrip.test.js` |
| 172 | +- Lockfile source of truth: `design-token-lock` skill → `design-tokens.lock.json` |
0 commit comments