A Node.js/TypeScript project that parses CSS files to extract design tokens and identifies which components use them. Built with css-tree
for robust CSS parsing and intelligent component name resolution with BEM hierarchy consolidation.
- Design Token Extraction: Automatically extracts CSS custom properties (design tokens) from CSS files
- Token Reference Detection: Identifies tokens that reference other tokens using
var()
functions - Component Usage Analysis: Determines which components use specific design tokens
- BEM Hierarchy Consolidation: Intelligently resolves component names from CSS selectors with support for BEM methodology
- Type Classification: Automatically categorizes tokens by type (color, dimension, shadow, font, etc.)
- JSON Output: Generates structured JSON output with comprehensive token metadata
Install the library from npm:
npm install css-token-extractor
# or
yarn add css-token-extractor
# or
pnpm add css-token-extractor
# Salesforce Lightning Design System (SLDS)
npm run extract:slds
# Carbon Design System (IBM)
npm run extract:carbon
# Stellar Design System
npm run extract:stellar
TypeScript / ESM:
import { CSSTokenExtractor } from 'css-token-extractor';
const extractor = new CSSTokenExtractor({
// Configure how components are detected and normalized
selectorPrefixes: ['slds-'],
// Order matters: longer separators first
bemSeparators: ['__', '--'],
// Exclude utilities/state classes using string or regex strings
excludeClasses: ['slds-var-', 'slds-m-', 'slds-is-', 'slds-has-', 'slds-r\\d']
});
// Extract from file
const tokens = await extractor.extractFromFile('./path/to/styles.css');
// Extract from CSS content string
const tokens = extractor.extractFromContent(cssContent);
// Save to JSON file
await extractor.saveToFile(tokens, './output/tokens.json');
// Generate statistics
const stats = extractor.generateStatistics(tokens);
CommonJS:
const { CSSTokenExtractor } = require('css-token-extractor');
The extractor generates JSON output with the following structure:
{
"--slds-g-spacing-1": {
"value": "0.25rem",
"type": "dimension",
"usedIn": ["slds-button", "slds-checkbox", "slds-card"]
},
"--sds-g-spacing-1": {
"refersTo": ["--slds-g-spacing-1"]
},
"--slds-g-color-palette-blue-50": {
"value": "#0176d3",
"type": "color"
},
"--slds-g-shadow-insetinverse-focus-1": {
"type": "shadow",
"refersTo": ["--slds-g-color-brand-base-15"],
"usedIn": ["button", "checkbox"]
}
}
value
: The actual CSS value (only present if token doesn't reference others)type
: Token type (dimension
,color
,shadow
,font
,other
)refersTo
: Array of design tokens referenced viavar()
functionsusedIn
: Array of component names that use this token
The extractor intelligently resolves component names from CSS class selectors using BEM hierarchy consolidation:
.slds-checkbox
→slds-checkbox
.slds-sub-tabs__item
→slds-sub-tabs
(BEM element consolidated to parent).slds-button--brand
→slds-button
(modifier consolidated).cds--switcher__item
→cds--switcher
(BEM element consolidated).cds--layer-one
→cds--layer
(if hyphen variants are treated as family; see config notes)
- BEM Elements (
__
): Consolidated to the base component. - BEM Modifiers (
--
or custom): Consolidated to the base component. - Hyphenated Variants: By default preserved; can be consolidated by adding
-
tobemSeparators
or by project-specific logic. - Utility/State Classes: Can be excluded using
excludeClasses
(strings or regex-like strings, e.g.,slds-r\\d
). - Specificity Sorting: Internally, class names are normalized, sorted, and de-duplicated before base selection.
- Sort order matters: we sort
selectorPrefixes
,bemSeparators
, andexcludeClasses
by length (desc) to ensure the most specific match wins. - For Carbon (
cds--
): preferselectorPrefixes: ['cds--']
andbemSeparators: ['__', '--']
so bases likecds--switcher
are preserved. - For SLDS:
selectorPrefixes: ['slds-']
,bemSeparators: ['__', '--', '_']
works well; exclude common utilities withexcludeClasses
.
Tokens are automatically classified based on their names and values:
color
: Contains color keywords, hex values, rgb/hsl functions, orlight-dark()
dimension
: Contains spacing, margin, padding keywords or rem/px/em unitsshadow
: Contains shadow keywords or box-shadow valuesfont
: Contains font-related keywords or font propertiesother
: Default classification for unrecognized patterns
src/
├── index.ts # Programmatic API
├── types.ts # TypeScript type definitions
├── css-parser.ts # CSS parsing and token extraction
├── selector-analyzer.ts # Component name resolution logic
└── token-analyzer.ts # Token analysis and output generation
# Build the project
npm run build
# Lint the code
npm run lint
# Example scripts for systems
npm run extract:slds
npm run extract:carbon
npm run extract:stellar
The extractor provides comprehensive statistics about the extracted tokens:
- Total number of tokens
- Tokens with direct values vs. references
- Token distribution by type
- Most frequently used tokens
- Component usage analysis
When run on SLDS example:
📊 Statistics:
Total tokens: 1347
Tokens with values: 462
Tokens with references: 885
Tokens by type:
color: 348
dimension: 67
other: 23
font: 23
shadow: 6
Most used tokens:
--slds-g-spacing-2: used in 72 component(s)
--slds-g-spacing-4: used in 64 component(s)
--slds-g-sizing-border-1: used in 62 component(s)
- Node.js 18+
- TypeScript 5+
- css-tree for CSS parsing
ISC (c) Naveen Ithappu
Contributions are welcome! Please see CONTRIBUTING.md for guidelines and development setup.
If you discover a security issue, please follow the steps in SECURITY.md.