AgentSkillsCN

style-dictionary

在使用 Style Dictionary 将设计令牌转化为特定平台的输出时使用。涵盖配置、转换、格式、DTCG 支持,以及多平台构建。 适用场景:Style Dictionary 配置、令牌转换、特定平台的令牌输出(CSS、SCSS、iOS、Android、Compose)、自定义格式、DTCG 到平台的流水线。 切勿用于:创作令牌文件(使用 Design Tokens)、Figma 变量管理(使用 Figma)、组件文档(使用 Storybook)。

SKILL.md
--- frontmatter
name: style-dictionary
description: |
    Use when transforming design tokens into platform-specific outputs using Style Dictionary. Covers configuration, transforms, formats, DTCG support, and multi-platform builds.
    USE FOR: Style Dictionary configuration, token transforms, platform-specific token output (CSS, SCSS, iOS, Android, Compose), custom formats, DTCG-to-platform pipelines
    DO NOT USE FOR: authoring token files (use design-tokens), Figma variable management (use figma), component documentation (use storybook)
license: MIT
metadata:
  displayName: "Style Dictionary"
  author: "Tyler-R-Kendrick"
compatibility: claude, copilot, cursor

Style Dictionary

Overview

Style Dictionary is the standard build system for transforming design tokens into platform-specific outputs — CSS custom properties, SCSS variables, iOS/Android constants, Kotlin Compose themes, and more. Version 4 adds native W3C DTCG support, ESM configuration, async transforms, and browser compatibility.

How It Works

code
Token source files (.tokens.json)
        │
        ▼
  ┌──────────┐
  │  Parse    │  Deep-merge all source files
  └────┬─────┘
       ▼
  ┌──────────┐
  │ Transform │  Name, value, and attribute transforms per platform
  └────┬─────┘
       ▼
  ┌──────────┐
  │  Format   │  Serialize into platform file format
  └────┬─────┘
       ▼
  Platform outputs (CSS, SCSS, JSON, Swift, Kotlin, etc.)

Configuration

js
// style-dictionary.config.mjs
import StyleDictionary from "style-dictionary";

const sd = new StyleDictionary({
  source: ["tokens/**/*.tokens.json"],
  platforms: {
    css: {
      transformGroup: "css",
      buildPath: "build/css/",
      files: [
        {
          destination: "variables.css",
          format: "css/variables",
        },
      ],
    },
    scss: {
      transformGroup: "scss",
      buildPath: "build/scss/",
      files: [
        {
          destination: "_variables.scss",
          format: "scss/variables",
        },
      ],
    },
    ios: {
      transformGroup: "ios-swift",
      buildPath: "build/ios/",
      files: [
        {
          destination: "DesignTokens.swift",
          format: "ios-swift/class.swift",
          className: "DesignTokens",
        },
      ],
    },
    android: {
      transformGroup: "android",
      buildPath: "build/android/",
      files: [
        {
          destination: "tokens.xml",
          format: "android/resources",
        },
      ],
    },
  },
});

await sd.buildAllPlatforms();

Build Commands

bash
# Build all platforms
npx style-dictionary build --config style-dictionary.config.mjs

# Build a single platform
npx style-dictionary build --config style-dictionary.config.mjs --platform css

Transforms

Transforms modify tokens for a specific platform. Three types:

TypeWhat It ChangesExample
attributeAdds metadata to token.attributesattribute/cti — adds category/type/item
nameChanges the token namename/kebabcolorPrimarycolor-primary
valueChanges the token valuecolor/css#3b82f6rgb(59, 130, 246)

Transform Groups

Built-in groups bundle common transforms:

GroupPlatformsTransforms Applied
cssWeb (CSS)name/kebab, color/css, size/px
scssWeb (SCSS)name/kebab, color/css, size/px
lessWeb (Less)name/camel, color/hex, size/px
jsJavaScriptname/camel, color/hex
ios-swiftiOSname/camel, color/UIColorSwift
androidAndroidname/snake, color/hex8android, size/dp
composeJetpack Composename/camel, color/composeColor

Formats

Formats serialize transformed tokens into output files:

FormatOutput
css/variables:root { --color-primary: #3b82f6; }
scss/variables$color-primary: #3b82f6;
less/variables@color-primary: #3b82f6;
javascript/es6export const ColorPrimary = "#3b82f6";
typescript/es6-declarationsType declarations for JS tokens
json/flat{ "color-primary": "#3b82f6" }
ios-swift/class.swiftSwift struct with static properties
android/resourcesAndroid XML <resources>

Custom Transform

js
import StyleDictionary from "style-dictionary";

StyleDictionary.registerTransform({
  name: "size/pxToRem",
  type: "value",
  filter: (token) => token.$type === "dimension",
  transform: (token) => {
    const val = parseFloat(token.value);
    return `${val / 16}rem`;
  },
});

Custom Format

js
StyleDictionary.registerFormat({
  name: "css/tailwind",
  format: ({ dictionary }) => {
    const tokens = dictionary.allTokens
      .map((t) => `  "${t.name}": "${t.value}"`)
      .join(",\n");
    return `module.exports = {\n${tokens}\n};\n`;
  },
});

Filtering Tokens

Filter which tokens appear in a specific output file:

js
{
  destination: "colors.css",
  format: "css/variables",
  filter: (token) => token.$type === "color",
}

DTCG Support (v4)

Style Dictionary v4 natively reads W3C DTCG tokens ($value, $type, $description):

json
{
  "color": {
    "$type": "color",
    "primary": { "$value": "#3b82f6" },
    "secondary": { "$value": "{color.primary}" }
  }
}

SD resolves $type inheritance from parent groups and resolves {alias} references automatically.

Token References in Output

js
// Preserve references in CSS output
{
  destination: "variables.css",
  format: "css/variables",
  options: {
    outputReferences: true,  // --color-secondary: var(--color-primary)
  },
}

Multi-Theme Builds

Build separate files per theme by pointing to different source sets:

js
const themes = ["light", "dark"];

const configs = themes.map((theme) => ({
  source: [`tokens/global/**/*.tokens.json`, `tokens/themes/${theme}/**/*.tokens.json`],
  platforms: {
    css: {
      transformGroup: "css",
      buildPath: `build/css/${theme}/`,
      files: [{ destination: "variables.css", format: "css/variables" }],
    },
  },
}));

for (const config of configs) {
  const sd = new StyleDictionary(config);
  await sd.buildAllPlatforms();
}

Best Practices

  • Use DTCG-format tokens ($value, $type) as input — Style Dictionary v4 supports them natively.
  • Use outputReferences: true in CSS/SCSS to preserve the alias chain as var() references — this enables runtime theming.
  • Use filter on files to split outputs by token type (colors, typography, spacing) for tree-shaking.
  • Register custom transforms for project-specific needs (px → rem, color space conversions).
  • Run Style Dictionary in CI to ensure token → platform output is always in sync.
  • Use the CTI naming convention (color.brand.primary) for predictable transform behavior.
  • Pin to Style Dictionary v4+ for ESM, async transforms, and DTCG support.