Preprocessors
Starting in version 4.0, you can define custom preprocessors to process the dictionary object as a whole, after it all token files have been parsed and combined into one. This is useful if you want to do more complex transformations on the dictionary as a whole, when all other ways are not powerful enough.
Preprocessors can be applied globally or per platform.
That said, preprocessing the full dictionary gives ultimate flexibility when needed.
Preprocessor structure
A preprocessor is an object with two props:
name
: the name of the preprocessorpreprocessor
a callback function that receives the dictionary and SD options as parameters, and returns the processed dictionary
Asynchronous callback functions are also supported, giving even more flexibility.
Using preprocessors
First you will need to tell Style Dictionary about your parser. You can do this in two ways:
- Using the
.registerPreprocessor
method - Inline in the configuration
.registerPreprocessor
Inline
Applying it in config
or platform-specific:
Preprocessor examples
Stripping description property recursively in the entire dictionary object:
Default preprocessors
There are two default preprocessors that are always applied and run before other custom preprocessors do:
typeDtcgDelegate
, for DTCG tokens, make sure the$type
is either already present or gets inherited from the closest ancestor that has it defined, so that the$type
is always available on the token level, for ease of useexpandObjectTokens
, a private preprocessor that will expand object-value (composite) tokens when user config has this enabled.