Skip to content

Dispersa API

Dispersa provides standalone functions instead of a class:

import {
build,
buildOrThrow,
buildPermutation,
resolveTokens,
lint,
resolveAllPermutations,
generateTypes,
} from 'dispersa'

Runs a full build. Never throws — returns a BuildResult with success and optional errors.

build(config: BuildConfig): Promise<BuildResult>
import { build, css } from 'dispersa'
const result = await build({
resolver: './tokens.resolver.json',
buildPath: './dist',
outputs: [css({ name: 'tokens', file: 'tokens.css' })],
})
if (result.success) {
for (const output of result.outputs) {
console.log(`${output.name}: ${output.path}`)
}
} else {
for (const err of result.errors ?? []) {
console.error(`${err.code}: ${err.message}`)
}
}

Same as build, but throws on failure instead of returning errors.

buildOrThrow(config: BuildConfig): Promise<BuildResult>
import { buildOrThrow, css } from 'dispersa'
await buildOrThrow({
resolver: './tokens.resolver.json',
outputs: [css({ name: 'tokens', file: 'tokens.css' })],
})

Builds a single permutation with the given modifier inputs. Useful for generating one variant at a time.

buildPermutation(config: BuildConfig, modifierInputs?: ModifierInputs): Promise<BuildResult>
import { buildPermutation, css } from 'dispersa'
const result = await buildPermutation(
{
resolver: './tokens.resolver.json',
outputs: [css({ name: 'dark', file: 'dark.css' })],
},
{ theme: 'dark', density: 'compact' },
)

Resolves tokens for a resolver without rendering. Returns the resolved token map for the given modifier inputs.

resolveTokens(
resolver: string | ResolverDocument,
modifierInputs?: ModifierInputs,
): Promise<Record<string, ResolvedToken>>
import { resolveTokens } from 'dispersa'
const tokens = await resolveTokens('./tokens.resolver.json', {
mode: 'light',
})
console.log(tokens['color.background.default'])

Runs lint rules on resolved tokens. Returns a LintResult with issues and counts.

lint(options: LintOptions): Promise<LintResult>

LintOptions accepts:

PropertyTypeDescription
resolverstring | ResolverDocumentResolver file path or inline object
modifierInputsRecord<string, string>Modifier values (e.g., { theme: 'dark' })
validationValidationOptionsValidation settings
pluginsRecord<string, LintPlugin>Lint plugins to load
rulesRecord<string, RuleConfig>Rule configurations
failOnErrorbooleanThrow on lint errors (default: true)
import { lint } from 'dispersa'
import { recommendedConfig } from 'dispersa/lint'
const result = await lint({
resolver: './tokens.resolver.json',
...recommendedConfig,
})
console.log(`Found ${result.errorCount} errors, ${result.warningCount} warnings`)

Resolves tokens for every permutation defined in the resolver. Returns an array of { tokens, modifierInputs }.

resolveAllPermutations(
resolver: string | ResolverDocument,
): Promise<{ tokens: Record<string, ResolvedToken>; modifierInputs: ModifierInputs }[]>
import { resolveAllPermutations } from 'dispersa'
const permutations = await resolveAllPermutations('./tokens.resolver.json')
for (const { tokens, modifierInputs } of permutations) {
console.log('Permutation:', modifierInputs)
console.log('Tokens:', Object.keys(tokens).length)
}

Generates TypeScript types for a resolved token map and writes to a file.

generateTypes(
tokens: Record<string, ResolvedToken>,
fileName: string,
options?: { moduleName?: string },
): Promise<void>
import { resolveTokens, generateTypes } from 'dispersa'
const tokens = await resolveTokens('./tokens.resolver.json')
await generateTypes(tokens, './dist/tokens.d.ts', {
moduleName: 'tokens',
})

Configuration passed to build, buildOrThrow, and buildPermutation:

OptionTypeDescription
resolverstring | ResolverDocumentResolver file path or inline document
buildPathstringOutput directory
outputsOutputConfig[]Required. Output configurations
validation{ mode?: 'error' | 'warn' | 'off' }Validation mode
filtersFilter[]Global filters applied before any output
transformsTransform[]Global transforms applied before any output
preprocessorsPreprocessor[]Preprocessors applied to raw tokens
permutationsModifierInputs[]Explicit permutation list
lintLintBuildConfigLint configuration
hooksLifecycleHooksonBuildStart, onBuildEnd

Returned by build, buildOrThrow, and buildPermutation:

PropertyTypeDescription
successbooleanWhether the build completed without errors
outputs{ name: string; path?: string; content: string }[]Per-output results
errorsBuildError[]Present when success is false
lintResultLintResultLint results when linting is enabled
PropertyTypeDescription
messagestringHuman-readable error message
codeErrorCodeError category
pathstringFile path when relevant
tokenPathstringToken path when relevant
severity'error' | 'warning'Severity level
suggestionsstring[]Suggested fixes (e.g. similar token names)

'TOKEN_REFERENCE' | 'CIRCULAR_REFERENCE' | 'VALIDATION' | 'FILE_OPERATION' | 'CONFIGURATION' | 'BASE_PERMUTATION' | 'MODIFIER' | 'UNKNOWN'