Configuration
Style dictionaries are configuration driven. Your configuration lets Style Dictionary know:
- Where to find your design tokens
- How to transform and format them to generate output files
Here is an example configuration:
Configuration file formats
Style Dictionary supports configuration files in these file formats:
- JSON
- JSONC
- JSON5
- Javascript (ES Modules, default export)
Here is an example using an ES module for configuration:
Some interesting things you can do in a JS file that you cannot do in a JSON file:
- Add custom transforms, formats, filters, actions, preprocessors and parsers
- Programmatically generate your configuration
Using configuration files
By default, the Style Dictionary CLI looks for a config.json
or config.js
file in the root of your package.
You can also specify a custom location when you use the CLI with the --config
parameter.
Using in Node
You can also use Style Dictionary as an npm module and further customize how Style Dictionary is run, for example running Style Dictionary multiple times with different configurations. To do this you would create a Javascript file that imports the Style Dictionary npm module and calls the .extend
and .buildAllPlatforms
functions.
You would then change your npm script or CLI command to run that file with Node:
Properties
Property | Type | Description |
---|---|---|
log | Log | Configure logging behavior to either reduce/silence logs or to make them more verbose for debugging purposes. |
source | string[] | An array of file path globs to design token files. Style Dictionary will do a deep merge of all of the token files, allowing you to organize your files however you want. |
include | string[] | An array of file path globs to design token files that contain default styles. Style Dictionary uses this as a base collection of design tokens. The tokens found using the “source” attribute will overwrite tokens found using include. |
tokens | Object | The tokens object is a way to include inline design tokens as opposed to using the source and include arrays. |
expand | ExpandConfig | Configures whether and how composite (object-value) tokens will be expanded into separate tokens. false by default. Supports either boolean , ExpandFilter function or an Object containing a typesMap property and optionally an include OR exclude property. |
platforms | Record<string, Platform> | An object containing platform config objects that describe how the Style Dictionary should build for that platform. You can add any arbitrary attributes on this object that will get passed to formats and actions (more on these in a bit). This is useful for things like build paths, name prefixes, variable names, etc. |
hooks | Hooks object | Object that contains all configured custom hooks: preprocessors . Note: parsers , transforms , transformGroups , formats , fileHeaders , filters , actions will be moved under property this later. Can be used to define hooks inline as an alternative to using register<Hook> methods. |
parsers | string[] | Names of custom file parsers to run on input files |
preprocessors | string[] | Which preprocessors (by name) to run on the full token dictionary, before any transforms run, can be registered using .registerPreprocessor . You can also configure this on the platform config level if you need to run it on the dictionary only for specific platforms. |
transform | Record<string, Transform> | Custom transforms you can include inline rather than using .registerTransform . The keys in this object will be the transform’s name, the value should be an object with type |
format | Record<string, Format> | Custom formats you can include inline in the configuration rather than using .registerFormat . The keys in this object will be for format’s name and value should be the format function. |
usesDtcg | boolean | Whether the tokens are using DTCG Format or not. Usually you won’t need to configure this, as style-dictionary will auto-detect this format. |
Log
Log configuration object to configure the logging behavior of Style Dictionary.
Platform
A platform is a build target that tells Style Dictionary how to properly transform and format your design tokens for output to a specific platform. You can have as many platforms as you need and you can name them anything, there are no restrictions.
Property | Type | Description |
---|---|---|
transforms | string[] | An array of transform keys to be performed on the design tokens. These will transform the tokens in a non-destructive way, allowing each platform to transform the tokens. Transforms to apply sequentially to all tokens. Can be a built-in one or you can create your own. |
transformGroup | string | A string that maps to an array of transforms. This makes it easier to reference transforms by grouping them together. Can be combined with transforms. |
buildPath | string | Base path to build the files, must end with a trailing slash. |
expand | ExpandConfig | Configures whether and how composite (object-value) tokens will be expanded into separate tokens. false by default. Supports either boolean , ExpandFilter function or an Object containing a typesMap property and optionally an include OR exclude property. |
preprocessors | string[] | Which preprocessors (by name) to run on the full token dictionary when building for this particular platform, before any transforms run, can be registered using .registerPreprocessor . You can also configure this on the global config. |
options | Object | Options that apply to all files in the platform, for example outputReferences and showFileHeader |
prefix | string | A string that prefix the name of the design tokens. |
files | File[] | Files to be generated for this platform. |
actions | string[] | Actions to be performed after the files are built for that platform. Actions can be any arbitrary code you want to run like copying files, generating assets, etc. You can use pre-defined actions or create custom actions. |
File
A File configuration object represents a single output file. The options
object on the file configuration will take precedence over the options
object defined at the platform level. Apart from the options listed below, any other options can be added, which can then be used inside custom formats.
Property | Type | Description |
---|---|---|
destination | string | Location to build the file, will be appended to the buildPath. |
format | string | Format used to generate the file. Can be a built-in one or you can create your own via registerFormat. |
filter | string | function | Object | A function, string or object used to filter the tokens that will be included in the file. If a function is provided, each design token will be passed to the function and the result (true or false) will determine whether the design token is included. If an object is provided, each design token will be matched against the object using a partial deep comparison. If a match is found, the design token is included. If a string is passed, is considered a custom filter registered via registerFilter |
options | Object | A set of extra options associated with the file. Includes showFileHeader and outputReferences . |
options.showFileHeader | boolean | If the generated file should have a comment at the top about being generated. The default fileHeader comment has “Do not edit + Timestamp”. By default is “true”. |
options.fileHeader | string |function | A custom fileHeader that can be either a name of a registered file header (string) or an inline fileHeader function. |
options.outputReferences | boolean | OutputReferencesFunction | If the file should keep token references. By default this is “false”. Also allows passing a function to conditionally output references on a per token basis. |
Expand
You can configure whether and how composite (object-value) tokens will be expanded into separate tokens. By default, this functionality is disabled and for formats such as CSS where object values are not supported, you’d be relying on either a custom value transform to turn such token values into strings, or writing a custom format to format object values into CSS compatible values.
Expand usage
Below are examples of how the expand
property can be used.
The value of expand can be multiple things:
boolean
,false
by default, when set totrue
, any object-value (composite) design token will be expanded into multiple tokens, one for each property.- a function of type
ExpandFilter
, e.g.(token, options, platform) => true
, must return aboolean
, whentrue
will expand that individual token, arguments:token
: the design token of which the value is an object (composite)options
: the StyleDictionary config optionsplatform
: this is only passed when expand is used on the platform level, contains the platform specific config options
- An object:
- Empty, which is equivalent of passing
true
- Containing just a
typesMap
, which is also equivalent of passingtrue
, except you’re also passing thetypesMap
- Also containing an
include
orexclude
property which can be either an array of composite types or anExpandFilter
function, to conditionally expand or negate expand of individual tokens
- Empty, which is equivalent of passing
Global vs Platform
You can enable the expanding of tokens both on a global level and on a platform level.
Whether configured on platform or global level, the token expansion will happen immediately after user-configured preprocessors and before transform hooks.
That said, platform expand happens only when calling (get/export/format/build)Platform
methods for the specific platform, whereas global expand happens on StyleDictionary instantiation already.
Refer to the lifecycle hooks diagram for a better overview.
When expanding globally, token metadata properties that are added by Style Dictionary such as name
, filePath
, path
, attributes
etc. are not present yet.
The advantage of global expand however, is having the expanded tokens (sd.tokens
prop) available before doing any exporting to platforms.
If you configure it on the platform level, the metadata mentioned earlier is available and can be used to conditionally expand tokens.
It also allows you to expand tokens for some platforms but not for others.
The downside there is needing to configure it for every platform separately.
Type Mapping
While our expand utility comes with a typesMap
out of the box that aligns with the Design Token Community Group spec to convert composite subtype properties to defined DTCG types, you can also pass a custom typesMap
that will allow you to extend or override it.
A typesMap
allows you to configure how object-value (composite) properties in the original token value should be mapped to the newly expanded individual tokens.
For example:
Here, according to the DTCG spec, you would probably want to map the "width"
property to type "dimension"
and "style"
property to type "strokeStyle"
.
"width"
is more of a general property where we always want to map it to "dimension"
but border "style"
is more specific to the border composite type, therefore this typesMap
makes sense:
Resulting in the following expanded output:
Example
DTCG Type Map
Below is the standard DTCG type map that the expand utility comes out of the box with: