Skip to content

Commit

Permalink
Modify commonnjs formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
taysea committed Jul 30, 2024
1 parent 9822957 commit ab56166
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions design-tokens/src/build-style-dictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,40 @@ import * as fs from 'fs';
// }),
// ).buildAllPlatforms();

const jsonToNestedValue = token => {
// is non-object value
if (!token || typeof token !== 'object') return token;
// is design token
if ('value' in token) return token.value;
// is obj
const nextObj = {};
Object.entries(token).forEach(([prop, value]) => {
nextObj[prop] = jsonToNestedValue(value);
});

return nextObj;
};

const { fileHeader } = StyleDictionary.formatHelpers;

const javascriptCommonJs = ({ dictionary, file, options, platform = {} }) => {
const { prefix } = platform;
const tokens = prefix ? { [prefix]: dictionary.tokens } : dictionary.tokens;
//
const output = `${fileHeader({ file })}module.exports = ${JSON.stringify(
jsonToNestedValue(tokens),
null,
2,
)}\n`;
// return prettified
return output;
};

StyleDictionary.registerFormat({
name: 'javascript/commonJs',
formatter: javascriptCommonJs,
});

// js transform group + custom from style-dictionary-utils
StyleDictionary.registerTransformGroup({
name: 'js/w3c',
Expand Down

0 comments on commit ab56166

Please sign in to comment.