Style Dictionary: Transforming and Generating Tokens
Configure Style Dictionary to transform raw token files into CSS variables, JS constants, and Tailwind config.
Style Dictionary: Transforming and Generating Tokens is a free React Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Style Dictionary
Style Dictionary (created by Amazon) is a build tool that takes design token definitions in JSON format and outputs them in any target format: CSS custom properties, JavaScript ES modules, Sass variables, iOS Swift constants, Android XML resources, and any custom format you define. One source of truth, many outputs.
Installation and Initialization
Install with npm install --save-dev style-dictionary. Initialize a project structure with npx style-dictionary init, which creates a config.json (or config.js) and a tokens/ directory. The config tells Style Dictionary where to find token files and where to write the outputs.
Token JSON Format
Style Dictionary uses a nested JSON structure where the leaf nodes have a value property. For example: { "color": { "primary": { "500": { "value": "#3b82f6" } } } }. The path to the value (color, primary, 500) becomes the token name after transformation, typically formatted as color-primary-500 or --color-primary-500.
Running the Build
Run npx style-dictionary build --config config.json (or style-dictionary build if configured in package.json scripts). Style Dictionary reads all token files from the configured source, applies transforms, and writes outputs to the configured destinations. Errors in the token format are reported with the file name and path.
Defining Output Platforms
The config.json defines platforms: css (outputs a CSS file with custom properties), js (outputs a JS object), scss (outputs Sass variables). Each platform specifies the output directory, file name, format (the template), and transforms (the name and value processing functions) to apply.
Built-In Transforms
Style Dictionary includes built-in transforms: name/cti/kebab converts the nested path to color-primary-500, name/cti/camel produces colorPrimary500, size/rem converts pixel values to rem, color/hex normalizes colors to hex. Transforms are composable and applied in order.
Registering Custom Transforms
You can register custom transforms with StyleDictionary.registerTransform(). For example, a transform that converts pixel values to iOS points, or one that adds a company-specific prefix to all token names. Custom transforms give you full control over how values are processed before output.
Registering Custom Formats
Custom formats define the output file template. StyleDictionary.registerFormat() receives the dictionary of transformed tokens and returns a string — the file content. You can output tokens as TypeScript types, as a React theme object, as a Tailwind config extension, or as any other format your stack requires.
Integrating Output Into React
After building, import the CSS output in your global stylesheet: @import './tokens/tokens.css'. Import the JS output in components or the Tailwind config: import tokens from './tokens/tokens.js'. The CSS gives you custom properties for runtime theming; the JS gives you typed constants for Tailwind and JavaScript logic.
Token References and Aliases
Style Dictionary supports token references using curly braces: { "value": "{color.primary.500}" }. This creates semantic token aliases — the semantic token references a primitive token. At build time, Style Dictionary resolves references and outputs the final resolved value (or a CSS variable reference, depending on your format).
Automating with prebuild
Run Style Dictionary as a prebuild npm script so tokens are always regenerated before the application builds. Add "prebuild": "style-dictionary build" to package.json. This ensures that any change to the token JSON files is reflected in the CSS and JS outputs before the React build processes them.
Style Dictionary Transform
What is the purpose of transforms in Style Dictionary?
Lesson Recap
Style Dictionary converts token JSON into any output format: CSS variables, JS modules, Sass, iOS, Android. Token JSON uses nested objects with value leaf nodes. Transforms process names and values (camelCase, px-to-rem); formats define the output template. Token references ({color.primary.500}) enable semantic aliasing. Automate with a prebuild script so tokens are always fresh before the React build.
Frequently asked questions
Is the “Style Dictionary: Transforming and Generating Tokens” lesson free?
Yes — the full text of “Style Dictionary: Transforming and Generating Tokens” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.
What will I learn in “Style Dictionary: Transforming and Generating Tokens”?
Configure Style Dictionary to transform raw token files into CSS variables, JS constants, and Tailwind config. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start React Academy?
No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Style Dictionary: Transforming and Generating Tokens” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this React Academy lesson?
Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What Are Design Tokens and Why They Matter
- Style Dictionary: Transforming and Generating Tokens
- Tokens in Tailwind, CSS Variables, and CSS-in-JS
- Multi-Theme and Multi-Brand Token Architecture