DTCG
Convert design tokens between four formats, in code or on the command line, with a report of what each conversion loses.
Reads and writes DTCG, Figma variables, Tokens Studio and Tailwind v4, and reports what each conversion could not carry, per token, rather than dropping it silently. The converter tool on this site is this package in your browser.
Install
npm install @hipuku/dtcgUsage
import { readFileSync } from 'node:fs'
import { convert } from '@hipuku/dtcg'
const text = readFileSync('tokens.json', 'utf8')
const result = convert([{ name: 'tokens.json', text }], 'tailwind')
result.format // the format detected in the input
result.files // the output, one or more files
result.allLosses // what could not be carried, each with the token, the reason and detailread and write are the two halves of convert, and detectFormat names a format from its
shape.
Formats
| Format | Notes |
|---|---|
| DTCG | Resolver documents, modes included |
| Figma variables | The export shape: collections and modes |
| Tokens Studio | Sets and themes |
| Tailwind v4 | @theme stylesheets |
All four are read and written.
The command line
npx dtcg tokens.json --to tailwind > theme.css
npx dtcg theme.css --from tailwind --to figma --out figma/Run it from a project with @hipuku/dtcg installed. Without a local install, npx dtcg fetches an
unrelated package of the same name.
| Option | Meaning |
|---|---|
--to <format> | dtcg, figma, tokens-studio or tailwind. Required |
--from <format> | The input format. Detected when left out |
--out <dir> | Write the output files here. Without it, a single file goes to stdout |
--rem-base <px> | Pixels per rem. Default 16 |
--mode-selector <kind> | How a Tailwind stylesheet switches modes: data-theme, class or media |
The loss report goes to stderr. Exit codes: 0 converted, 1 the input could not be read, 2 the command was wrong, 3 the output could not be written.
API
| Export | Signature |
|---|---|
FORMAT_LABEL | Record<FormatId, string> |
TOKEN_TYPES | readonly TokenType[] |
COMPOSITE_TYPES | readonly TokenType[] |
LOSS_REASONS | readonly LossReason[] |
DEFAULT_OPTIONS | WriteOptions |
ReadError | typeof ReadError |
joinPath | (path: readonly string[]) => string |
convert | (documents: readonly InputDocument[], target: FormatId, options?: WriteOptions, source?: FormatId) => Conversion |
read | (documents: readonly InputDocument[], format?: FormatId) => ReadOutcome |
write | (set: TokenSet, format: FormatId, options?: WriteOptions) => WriteResult |
detectFormat | (documents: readonly InputDocument[]) => FormatId |
MAX_INPUT_BYTES | 500000 |
indexTokens | (set: TokenSet) => Index |
resolveValue | (value: TokenValue, mode: string, set: TokenSet, index: Index) => Resolution |
propertyName | (token: Token) => { name: string; inTheme: boolean; renamed: boolean; } |
colourToCss | (value: ColorValue) => string | null |
cubicBezierToCss | (b: readonly number[]) => string |
dimensionToCss | (d: DimensionValue) => string |
durationToCss | (d: DurationValue) => string |
isColorValue | (value: unknown) => value is ColorValue |
isCubicBezier | (value: unknown) => value is [number, number, number, number] |
isDimensionValue | (value: unknown) => value is DimensionValue |
isDurationValue | (value: unknown) => value is DurationValue |
The full reference is the package README.