-
Notifications
You must be signed in to change notification settings - Fork 571
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4542f4
commit a12d5b0
Showing
13 changed files
with
258 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# 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. | ||
|
||
It should be clear that using this feature should be a last resort. Using custom parsers to parse per file or using transforms to do transformations on a per token basis, | ||
gives more granular control and reduces the risks of making mistakes. That said, preprocessing the full dictionary gives ultimate flexibility when needed. | ||
|
||
--- | ||
|
||
## Preprocessor structure | ||
|
||
A preprocessor is simply a callback function that receives the dictionary as a parameter, and returns the processed dictionary. | ||
|
||
```javascript | ||
const myPreprocessor = (dictionary) => { | ||
delete dictionary.thirdPartyMetadata; | ||
return dictionary; | ||
}; | ||
``` | ||
|
||
Asynchronous callback functions are also supported, giving even more flexibility. | ||
|
||
```javascript | ||
const myPreprocessor = async (dictionary) => { | ||
const propsToDelete = await someAPICall(); | ||
|
||
propsToDelete.forEach((propName) => { | ||
delete dictionary[propName]; | ||
}); | ||
|
||
return dictionary; | ||
}; | ||
``` | ||
|
||
--- | ||
|
||
## Using parsers | ||
|
||
First you will need to tell Style Dictionary about your parser. You can do this in two ways: | ||
|
||
1. Using the `.registerPreprocessor` method | ||
1. Inline in the [configuration](config.md) | ||
|
||
### .registerPreprocessor | ||
|
||
```javascript | ||
import StyleDictionary from 'style-dictionary'; | ||
|
||
StyleDictionary.registerPreprocessor(myPreprocessor); | ||
``` | ||
|
||
### Inline | ||
|
||
```javascript | ||
export default { | ||
preprocessors: [myPreprocessor], | ||
// ... the rest of the configuration | ||
}; | ||
``` | ||
|
||
--- | ||
|
||
## Preprocessor examples | ||
|
||
> Coming soon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with | ||
* the License. A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @typedef {import('../../types/Preprocessor').Preprocessor} Preprocessor | ||
* Adds a custom preprocessor to preprocess the parsed dictionary, before transforming individual tokens. | ||
* @static | ||
* @memberof module:style-dictionary | ||
* @param {Preprocessor} preprocessor - Function to preprocess the dictionary. The function should return a plain Javascript object. | ||
* @returns {module:style-dictionary} | ||
* @example | ||
* ```js | ||
* StyleDictionary.registerPreprocessor((dictionary) => { | ||
* return dictionary; | ||
* }); | ||
* ``` | ||
*/ | ||
export default function registerPreprocessor(preprocessor) { | ||
this.preprocessors.push(preprocessor); | ||
return this; | ||
} |
Oops, something went wrong.