Skip to Content
Getting startedFoundationsComponentsPackagesTools

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.

@hipuku/dtcg0.1.1Source on GitHub

Install

npm install @hipuku/dtcg

Usage

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 detail

read and write are the two halves of convert, and detectFormat names a format from its shape.

Formats

FormatNotes
DTCGResolver documents, modes included
Figma variablesThe export shape: collections and modes
Tokens StudioSets 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.

OptionMeaning
--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

ExportSignature
FORMAT_LABELRecord<FormatId, string>
TOKEN_TYPESreadonly TokenType[]
COMPOSITE_TYPESreadonly TokenType[]
LOSS_REASONSreadonly LossReason[]
DEFAULT_OPTIONSWriteOptions
ReadErrortypeof 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_BYTES500000
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.

On this page