csstokens is a TypeScript CLI to extract design tokens from existing CSS, SCSS, JS, JSX, TS, and TSX codebases.
It scans frontend repositories, finds repeated colors, spacing, shadows, and CSS variables, then generates deterministic token outputs for design system cleanup and migration work.
Use it when you want to turn an existing codebase into:
raw-index.jsonwith detected values and occurrencestokens.jsontokens.csstokens.tsreport.md
- Node.js 20+
- pnpm 9+
npm install -g @protohiro/csstokens
csstokens --helpcsstokens --helpcsstokens
csstokens --profile strictcsstokens analyze ./examples/messy-uiGenerates:
./csstokens-out/raw-index.json./csstokens-out/report.md
csstokens extract ./examples/messy-ui --prefix pt
csstokens extract ./examples/messy-ui --profile strictGenerates:
./csstokens-out/raw-index.json./csstokens-out/report.md./csstokens-out/tokens.json./csstokens-out/tokens.css./csstokens-out/tokens.ts
--out <dir>default:./csstokens-out--include <glob>repeatable, default:**/*.{css,scss,tsx,ts,jsx,js}--exclude <glob>repeatable, default:**/node_modules/**,**/dist/**,**/build/**,**/.next/**--prefix <string>default:pt--format simpleonlysimpleis currently supported--profile <name>balancedorstrict--min-count <number>override minimum count threshold for final token candidates--min-file-count <number>override minimum file spread threshold for final token candidates--source-ignore <glob>repeatable, ignore matched files for ranking/tokenization--config <path>explicit config file path--dry-runanalyze and print summary without writing files
If you are working on the repository itself:
pnpm install
pnpm -r build
pnpm -r testThe CLI auto-loads csstokens.config.json or .csstokensrc.json from the analyzed repo root.
Example:
{
"profile": "strict",
"include": ["src/**/*.{css,scss,ts,tsx,js,jsx}"],
"exclude": ["**/node_modules/**", "**/dist/**"],
"sourceIgnorePatterns": ["**/*.stories.tsx", "**/generated/**"],
"thresholds": {
"minCount": 3,
"minFileCount": 2
}
}## Summary
- Files scanned: 2
- Entries: 20
- Color values: 8
- Length values: 9
- Shadow values: 2
- CSS var definitions: 3
- CSS var usages: 1
## Recommendations
- Detected 5 unique normalized colors.
- Found 3 near-duplicate color pairs for possible merge.:root {
--pt-color-blue-100: #3355ff;
--pt-color-blue-500: #3366ff;
--pt-color-blue-900: #3377ff;
--pt-radius-0: 6px;
--pt-space-0: 8px;
--pt-shadow-1: 0 1px 4px rgba(0,0,0,0.12);
}packages/coreparsing, normalization, grouping, emitterspackages/clicommand parsing, globbing, file IO, user-facing errorsexamples/messy-uifixture repository used by golden tests
- sorted file processing
- sorted index entries
- sorted token keys
- stable tie-breakers for naming/grouping
- no timestamps/randomness in generated files
- source filtering skips likely Storybook, generated, palette, and theme-dump files from token candidate ranking
- each raw index entry gets a simple
confidencescore based on frequency, file spread, and property context - spacing and radius token generation is limited to common token-like ranges to avoid one-off layout values
- near-duplicate colors are reported as mergeable clusters instead of an unbounded pair list
- final color tokens are split into
text,surface,border,primary,accent, andstatus.*candidates strictprofile emits a smaller token set with higher thresholds
- unit tests: color normalization
- unit tests: spacing/radius sorting
- golden snapshots:
tokens.json,tokens.css,report.md,raw-index.json
- regex-based parsing; no full AST support
- line/column detection is approximate in some complex TSX template cases
- limited color parser (common hex/rgb/hsl forms only)
box-shadowdetection focuses on directbox-shadow: ...declarations- typography extraction is heuristic (
font-size,font-weight,font-family) - no automatic code refactor from literals to CSS vars/tokens