Format output aggregation #1319
Replies: 1 comment 3 replies
-
https://styledictionary.com/reference/api/#formatallplatforms this should come in handy import StyleDictionary from 'style-dictionary';
StyleDictionary.registerFormat({
name: 'js-memory',
format: ({ dictionary }) => dictionary.allTokens,
})
const sd = new StyleDictionary({
platforms: {
css: {
transformGroup: 'css',
files: [{
format: 'js-memory' // use this one for all your platforms
}]
}
}
});
/**
* this is the magic
* Output will look something like:
* {
* css: [
* {
* output: [{
* name: 'token-1',
* value: '...'
* }]
* }
* ]
* }
*
*/
const platformOutputs = await sd.formatAllPlatforms();
const tokens = [];
Object.entries(platformOutputs).forEach(([platKey, files], index) => {
files[0].output.forEach(outputToken => {
// first time encountering this token, construct it in the array
if (index === 0) {
tokens.push({
name: outputToken.name,
platforms: {}
});
}
const token = tokens.find(t => t.name === outputToken.name);
token.platforms[platKey] = {
name: convertNameToCSSProp(token.name),
value: outputToken.value,
}
})
}); I wrote this code from scratch here inside the Github reply so no guarantees this runs/compiles but this is the rough approach to it:
You will still need some kind of utils to convert the token name to the naming conventions (e.g. CSS custom props, SCSS props) of your choosing though. And every platform in your config must use the same name transform, judging by your post that would be FYI it might be easier to use a "name"-keyed object / Map() instead of a name-keyed array, would be easier to access, less need for |
Beta Was this translation helpful? Give feedback.
-
Hi all!
I'm curious if anyone has found an easy way to aggregate all platform outputs into a single object. For documentation purposes, I'd love to be able to construct an object similar to the one below to quickly iterate over and display in a filterable table:
Apologies if I'm completely overlooking something in the docs.
Beta Was this translation helpful? Give feedback.
All reactions