Skip to content

Commit 2493fd6

Browse files
committed
chore: add single-file multi-theme example
1 parent d66998b commit 2493fd6

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,75 @@ run();
455455

456456
You can find a variation of this example [here](https://github.com/tokens-studio/lion-example). It outputs a CSS file for every theme combination _for every component_, e.g. `button-business-blue.css`, `date-picker-business-blue.css` and so on. This caters to use cases where component-level tokens as required, e.g. when implementing Web Components.
457457

458+
#### Single-file tokens example
459+
460+
> This is not recommended because it's a pretty complex workaround.
461+
> The best method is to just migrate to multi-file tokens export.
462+
463+
The same full example as above but assuming single token-file export.
464+
465+
What it does is, take your single `"tokens.json"` file and turn it into multi-file by
466+
persisting the sets as separate files in a `"tokens"` folder and then creating the SD config accordingly.
467+
468+
```js
469+
import { register, permutateThemes } from '@tokens-studio/sd-transforms';
470+
import StyleDictionary from 'style-dictionary';
471+
import { readFile, writeFile, mkdir } from 'fs/promises';
472+
import { dirname } from 'node:path/posix';
473+
474+
register(StyleDictionary, {
475+
/* options here if needed */
476+
});
477+
478+
async function run() {
479+
const tokens = JSON.parse(await readFile('tokens.json', 'utf-8'));
480+
const { $themes, ...sets } = tokens;
481+
482+
const persistSet = async ([setName, setTokens]) => {
483+
const fileName = `tokens/${setName}.json`;
484+
const dirName = dirname(fileName);
485+
try {
486+
await mkdir(dirName, { recursive: true });
487+
} catch (e) {
488+
// do nothing, dir already exists
489+
}
490+
await writeFile(fileName, JSON.stringify(setTokens, null, 2), 'utf-8');
491+
};
492+
493+
// persist sets as multi file in tokens folder
494+
await Promise.all(Object.entries(sets).map(persistSet));
495+
496+
const themes = permutateThemes($themes, { separator: '_' });
497+
const configs = Object.entries(themes).map(([name, tokensets]) => ({
498+
source: Object.keys(sets)
499+
.filter(setName => tokensets.includes(setName))
500+
.map(setName => `tokens/${setName}.json`),
501+
preprocessors: ['tokens-studio'], // <-- since 0.16.0 this must be explicit
502+
platforms: {
503+
css: {
504+
transformGroup: 'tokens-studio',
505+
transforms: ['name/kebab'],
506+
files: [
507+
{
508+
destination: `vars-${name}.css`,
509+
format: 'css/variables',
510+
},
511+
],
512+
},
513+
},
514+
}));
515+
516+
async function cleanAndBuild(cfg) {
517+
const sd = new StyleDictionary(cfg);
518+
await sd.cleanAllPlatforms(); // optionally, cleanup files first..
519+
await sd.buildAllPlatforms();
520+
}
521+
await Promise.all(configs.map(cleanAndBuild));
522+
}
523+
524+
run();
525+
```
526+
458527
## Transforms
459528

460529
### ts/descriptionToComment

0 commit comments

Comments
 (0)