amzn / style-dictionary

A build system for creating cross-platform styles.
https://styledictionary.com
Apache License 2.0
3.87k stars 543 forks source link

feat: add preprocessors #1043

Closed jorenbroekema closed 10 months ago

jorenbroekema commented 11 months ago

Sometimes it makes sense to process the dictionary after it has been created from merging the parsed input files together into one JS object. For example, when more complicated and broad transformations have to be done in order for things like references to work, metadata to be deleted, etc.

In the past, I've used custom parsers to parse design token files from Tokens Studio, but we ran into issues with cross-file references, which at the parser lifecycle cannot be resolved and transformed, because the parsers work on single input files one by one. I think that totally makes sense for parsers, I wouldn't change that, but instead I think it makes sense to introduce this preprocessor lifecycle hook to deal with use cases like this.

TODO:

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

jorenbroekema commented 10 months ago

@dbanksdesign this needs another review, I've rebased to the branch that has all tests fixed (and also contains some refactors and API changes that affect this PR). I also changed the API of preprocessors to be aligned with formats, transforms, fileheader etc. which just means you register it with a name property, I think this is better API design, helps with indexing and debugging preprocessors if multiple are used.

I'm also going to add a task to the v4 project board for aligning every registerWhatever API, there are some inconsistencies that I think could do with fixing. See: https://github.com/amzn/style-dictionary/issues/1049