Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/tokens/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@types/tinycolor2": "^1.4.6",
"@typescript-eslint/eslint-plugin": "^8.18.1",
"@typescript-eslint/parser": "^8.18.1",
"chalk": "^5.4.1",
"eslint": "^8.57.1",
"fs-extra": "^11.2.0",
"lodash-es": "^4.17.21",
Expand Down
21 changes: 14 additions & 7 deletions packages/tokens/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import StyleDictionary from 'style-dictionary';
import type { DesignToken, PlatformConfig } from 'style-dictionary/types';

import tinycolor from 'tinycolor2';
import chalk from 'chalk';

import fs from 'fs-extra';
import path from 'path';
Expand Down Expand Up @@ -36,27 +37,33 @@ const distFolder = path.resolve(__dirname, '../dist');
for (const mode of modes) {
StyleDictionary.registerPreprocessor({
name: `replace-value-for-mode-${mode}`,
preprocessor: (dictionary, _options) => {
preprocessor: (dictionary, options) => {
// we get the `buildPath` from the `PlatformConfig` option
const buildPath = (options as any).buildPath;
// recursively traverse token objects and replace the `$value` with the corresponding colocated `$modes` theme value
// note: the `slice` is always an object (a token or a parent group)
function replaceModes(slice: DesignToken) {
function replaceModes(slice: DesignToken, tokenPath: string[]) {
if (slice.$modes) {
if (mode in slice.$modes) {
// extra validation to catch instances where the `default` mode value is different from the `$value`
if (mode === 'default' && slice.$modes[mode] !== slice.$value) {
console.warn(`โš ๏ธ ${chalk.yellow.bold('WARNING')} - Found themed 'default' token '{${path.join('.')}}' with value different than '$value' (\`${slice.$modes[mode]}\` instead of the expected \`${slice.$value}\`) - BuildPath: ${buildPath} - File: ${slice.filePath}`);
}
slice.$value = slice.$modes[mode];
} else {
// TODO! decide if we want to throw here (and test if it works, by removing a value from one of the test files) - see: https://hashicorp.atlassian.net/browse/HDS-5668
console.error(`โŒ ERROR - Found themed token without '${mode}' value:`, JSON.stringify(slice, null, 2));
// we want to interrupt the execution of the script if one of the expected modes is missing
throw new Error(`โŒ ${chalk.red.bold('ERROR')} - Found themed token '{${path.join('.')}}' without '${mode}' value - BuildPath: ${buildPath} - File: ${slice.filePath} - Path: ${path.join('.')} - ${JSON.stringify(slice, null, 2)}`);
}
} else {
Object.values(slice).forEach((value) => {
Object.entries(slice).forEach(([key, value]) => {
if (typeof value === 'object') {
replaceModes(value);
replaceModes(value, [...tokenPath, key]);
}
});
}
return slice;
}
return replaceModes(dictionary);
return replaceModes(dictionary, []);
},
});
}
Expand Down
72 changes: 13 additions & 59 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.