Skip to content
All notes

Design systems as invariants: building a token pipeline from scratch

How I keep colours, typography and spacing in one place, generate code for different platforms and check the rules automatically.

When a backend, website and mobile app evolve together, colours, spacing and rules can start to differ. AI agents make changes faster, which also makes it harder to keep track of everything by hand.

I want to describe the rules once and check them automatically. I use design tokens: named values for colours, sizes and other interface settings. The rules that should remain true after a change are the invariants.

This article walks through the design layer of my NestJS, Nuxt and Flutter template. The code examples explain the pipeline. Their colours are examples, not this website’s current palette.

The mistakes I want to catch

A person or an agent can put a colour directly in a component, or change spacing in one app and forget another. Each edit is small. In a large change, it becomes hard to notice them all.

I try to turn those agreements into checks:

A design system needs rules that code can check. For example: components take colours from tokens, every component has a story and test, and generated files are not edited by hand.

That does not replace review. It lets tools catch some repetitive mistakes before review begins.

The path from design to a screen

I store tokens in a shared format and generate code for each platform. Components use that code, and pages use the components:

One source — the whole interface

From design to code

Emitters translate the shared format into native platform code.

From tokens to interface

Screens compose primitives from a shared library.

Library checksLinters · audit · CI drift

Here is each step in turn.

Step 1. Keep tokens in a shared format

I use W3C Design Tokens (DTCG). It describes a value with $value and its type with $type. The format is not tied to Nuxt or Flutter:

JSON
// specs/design/tokens/color.json
"accent": {
  "light": { "$value": "#635bff", "$type": "color" },
  "dark":  { "$value": "#7b75ff", "$type": "color" }
}

Two things matter to me here.

  1. Describe a value in one place. Colours, typography, spacing, radii, shadows and animation settings live in token files. Other code references them or is generated from them.
  2. Name the role. accent is an accent; surface.raised is a raised surface. When the accent’s value changes, components keep using the same name.

The JSON can be written by hand or exported from a design tool. The template documents imports from Claude Design, Figma and Penpot. The important part is getting the data into one format before generation.

Sources and instructions: specs/design and specs/design/tokens.

Step 2. Generate code for each platform

Shared JSON is not enough: a browser needs CSS variables, while Flutter needs Dart values. I use emitters, small programs that translate tokens into code for the target platform.

For example, the CSS emitter walks the tokens, groups them by theme and changes camelCase names to kebab-case:

TypeScript
// simplified from packages/design-tokens/src/emit-scss.ts
function themedBlock(theme, tokens) {
  const selector = theme === 'light' ? `:root, [data-theme='light']` : `[data-theme='${theme}']`;
  const lines = [`${selector} {`];
  for (const [key, leaf] of Object.entries(tokens.color.brand)) {
    const value = (leaf[theme] ?? leaf.light).$value; // fall back to light
    lines.push(`  --brand-${kebab(key)}: ${value};`);
  }
  lines.push('}');
  return lines.join('\n');
}

The output is ordinary CSS variables. Light is the default theme, and dark overrides the values:

CSS
:root,
[data-theme='light'] {
  --brand-accent: #635bff;
}
[data-theme='dark'],
.dark {
  --brand-accent: #7b75ff;
}

One build writes the result for every consumer: the website, Storybook and the mobile library. Several themes can be generated in the same pass:

TypeScript
// simplified from packages/design-tokens/src/build.ts
const targets = [
  { file: 'apps/web/app/assets/css/tokens.generated.css', contents: emitScss(tokens) },
  { file: 'packages/ui/src/tokens.generated.css', contents: emitScss(tokens) },
  { file: 'apps/web/app/design-tokens.generated.ts', contents: emitTypescript(tokens) },
  { file: 'packages/ui/src/design-tokens.generated.ts', contents: emitTypescript(tokens) },
  { file: 'packages/ui_flutter/lib/src/theme/tokens.g.dart', contents: emitDart(tokens) },
];

Here is a token in the browser:

CSS
/* CSS / SCSS (browser) */
--brand-accent: #635bff;

And its representation in Flutter:

Dart
// Flutter / Dart (mobile runtime)
static const Color brandAccent = Color(0xFF635BFF);

If I need another platform, I can add an emitter for it. The source tokens stay shared.

Generation code: packages/design-tokens. Flutter usage: packages/ui_flutter.

Step 3. Build components on tokens

I keep the base components in my own library. That lets me check their styles and control what the apps consume.

A component includes markup, a story to inspect its variants, a test and an export:

packages/ui/src/components/AppButton/
  • AppButton.vueMarkup and token styles
  • AppButton.stories.tsShowcase of every variant
  • AppButton.spec.tsRendering, a11y and variants
  • index.ts

Styles use var(--*), and class names use BEM with a prefix. Here is the button example:

SCSS
.app-button {
  height: var(--space-10);
  padding: 0 var(--space-4);
  border-radius: var(--radius-lg);
  font-size: var(--text-md);
  transition: background var(--dur-base) var(--ease);

  &--primary {
    // BEM modifier
    background: var(--brand-primary);
    color: var(--brand-primary-fg);
  }
}

A page can request color="primary". The actual colour stays in the tokens. When the palette changes, the component’s interface remains the same.

For complex elements such as calendars and dialogs, I can use an existing library. I expose it through my own wrapper component. The app works with my library, while the dependency’s details stay inside it.

Component sources: packages/ui.

Step 4. Check the rules automatically

An instruction alone is not enough to enforce a rule every time. I add a check that stops the build when the rule is broken.

The linter finds values written directly in styles. For example, it rejects a hex colour where a token reference is required:

JavaScript
// simplified from stylelint.config.mjs
rules: {
  'declaration-no-important': true,        // no !important
  'color-no-hex': true,                    // no #635bff — only var(--brand-*)
  'color-named': 'never',                  // no red / blue
  // plus: z-index only via var(--z-*), durations via var(--dur-*),
  //       spacing ≥3px via var(--space-*); classes — BEM with an app-/health-/brand- prefix
}

Write #F26A1F instead of a token and the check reports an error. The rule applies to both human and agent code.

The audit checks the component’s files. A missing story or test makes it fail:

TypeScript
// the gist of packages/ui/scripts/audit-components.ts
// every component folder must contain:
//   <Name>.vue, <Name>.stories.ts, <Name>.spec.ts, index.ts
// otherwise process.exit(1) — "component audit failed".

The audit runs in the ui-quality CI job, helping catch a missing file before changes merge.

A separate check compares generated code. The template already does this for API clients: it runs spec:codegen, then uses git diff --exit-code to compare the result with the committed files. The equivalent check for tokens is still to be added. For now, tokens need to be regenerated and generated files must not be edited by hand.

I want to distinguish a written rule from an enabled check. Having an instruction does not guarantee that an error will be caught.

Linter configuration: stylelint.config.mjs.

Step 5. Carry a change from token to page

A typical change goes like this:

  1. Update a value in specs/design/tokens/*.json.
  2. Run pnpm design:build to update generated files.
  3. Update the component, story and test if needed.
  4. Use the component on a page. The page keeps the data, props and navigation.

If a colour does not update, I first check where the component gets it. It might contain a literal value instead of a token reference. Changing the shared token to work around that mistake would not fix the cause.

What still needs care

  • Generation is a real step. Generated files are committed. Forget to rebuild them and the source and output differ. An automated check for token drift is still planned.
  • Renaming a token affects the apps. I treat it as a contract change and record the decision in an ADR, an architecture decision record.
  • Owning a library takes work. Components, stories and tests need maintenance. Installing an existing kit is usually quicker at the start.
  • Dependencies remain. Wrapping a complex widget does not remove the dependency itself.
  • Strict rules slow down a quick sketch. I accept that cost for more predictable changes.

What I want to improve next

The next steps ask the same question: what else can I stop keeping in my head?

  • Import tokens from a design tool through DTCG.
  • Regenerate tokens and check for differences in CI, as the template already does for API clients.
  • Support references between tokens, such as {group.token}, to reduce repeated values.
  • Automate token renaming across the codebase.
  • Check that every role has a value in every theme, and add visual component snapshots.

What I get from this approach

A colour or spacing value changes in one source, then reaches platform code and components. Linters and audits help check that the rules are followed. Some checks are already enabled and others still need to be added. I try to make that boundary clear.

The value for me is fewer repeated edits and fewer things to remember with every change. That helps whether I write the code myself or work with an agent.

Explore the template repository. The token format is documented at designtokens.org.

Next in the series, I want to look at shared API contracts: how OpenAPI and client generation help keep the backend and apps in sync.