Skip to content

Commit 4819f6c

Browse files
committed
feat: var references for all tokens
1 parent b6312e0 commit 4819f6c

File tree

5 files changed

+55
-33
lines changed

5 files changed

+55
-33
lines changed

packages/tokens-builder/configs/css.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ module.exports = {
1717
filter: 'css-variables-light',
1818
prefix: 'kbq',
1919
options: {
20-
selector: '.kbq-light'
20+
selector: '.kbq-light',
21+
outputReferences: true
2122
}
2223
},
2324
{
@@ -26,7 +27,8 @@ module.exports = {
2627
filter: 'css-variables-dark',
2728
prefix: 'kbq',
2829
options: {
29-
selector: '.kbq-dark'
30+
selector: '.kbq-dark',
31+
outputReferences: true
3032
}
3133
},
3234
{

packages/tokens-builder/filters/css-variables.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ module.exports = (StyleDictionary) => {
1515

1616
StyleDictionary.registerFilter({
1717
name: 'css-variables-light',
18-
matcher: (prop) => prop.attributes.light && !prop.attributes.palette
18+
matcher: (prop) => prop.attributes.light
1919
});
2020

2121
StyleDictionary.registerFilter({
2222
name: 'css-variables-dark',
23-
matcher: (prop) => prop.attributes.dark && !prop.attributes.palette
23+
matcher: (prop) => prop.attributes.dark
2424
});
2525

2626
StyleDictionary.registerFilter({

packages/tokens-builder/formats/utils.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const transform = require('style-dictionary/lib/common/transforms');
2+
const getPathFromName = require('style-dictionary/lib/utils/references/getPathFromName');
3+
const createReferenceRegex = require('style-dictionary/lib/utils/references/createReferenceRegex');
24

35
const unwrapObjectTransformer = (token) => {
46
return Object.keys(token.value).map((key) => {
@@ -15,6 +17,18 @@ const unwrapObjectTransformer = (token) => {
1517
}, {});
1618
};
1719

20+
const resolvePaletteRef = (token, dictionary) => {
21+
const regex = createReferenceRegex({});
22+
23+
const findPalette = (match, variable) => {
24+
const [category, item, ...pathName] = getPathFromName(variable);
25+
const pathToSemanticPalette = dictionary.tokens[category][item].palette.original.value.replace(/[{}]/g, '');
26+
return `{${pathToSemanticPalette}.${pathName[pathName.length - 1]}}`;
27+
};
28+
token.original.value = token.original.value.replace(regex, findPalette);
29+
return token;
30+
};
31+
1832
const getMapFromObj = (object) => {
1933
const result = Object.keys(object)
2034
.map((key) => {
@@ -29,7 +43,25 @@ const getMapFromObj = (object) => {
2943
return `(\n${result}\n)`;
3044
};
3145

46+
const applyCustomTransformations = (dictionary) => {
47+
dictionary.allProperties = dictionary.allTokens = dictionary.allTokens
48+
.flatMap((token) => {
49+
if (typeof token.value === 'object' && token.type === 'font') {
50+
return unwrapObjectTransformer(token);
51+
}
52+
if (typeof token.original.value === 'string' && token.original.value.includes('palette.value')) {
53+
return resolvePaletteRef(token, dictionary);
54+
}
55+
return token;
56+
})
57+
.filter((token) => !token.attributes.palette);
58+
59+
return dictionary.allTokens;
60+
};
61+
3262
module.exports = {
3363
unwrapObjectTransformer,
34-
getMapFromObj
64+
getMapFromObj,
65+
resolvePaletteRef,
66+
applyCustomTransformations
3567
};

packages/tokens-builder/formats/variables.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { formatHelpers } = require('style-dictionary');
2-
const { unwrapObjectTransformer } = require('../formats/utils');
2+
const { applyCustomTransformations } = require('../formats/utils');
33

44
module.exports = (StyleDictionary) => {
55
StyleDictionary.registerFormat({
@@ -8,13 +8,7 @@ module.exports = (StyleDictionary) => {
88
const selector = options.selector ? options.selector : `:root`;
99
const { outputReferences } = options;
1010

11-
// apply custom transformations for tokens
12-
dictionary.allProperties = dictionary.allTokens = dictionary.allTokens.flatMap((token) => {
13-
if (typeof token.value === 'object' && token.type === 'font') {
14-
return unwrapObjectTransformer(token);
15-
}
16-
return token;
17-
});
11+
applyCustomTransformations(dictionary);
1812

1913
dictionary.allTokens.forEach((token) => {
2014
token.name = token.name.replace(/(light|dark)-/, '');

tools/build-each-component.js

+14-20
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
1+
require('../packages/tokens-builder/build');
12
const fs = require('fs/promises');
23
const path = require('path');
34
const StyleDictionary = require('style-dictionary');
45
const { formatHelpers } = require('style-dictionary');
5-
require('../packages/tokens-builder/build');
6+
const { applyCustomTransformations } = require('../packages/tokens-builder/formats/utils');
67

78
const TOKEN_FILE_EXT = 'json5';
89
const BASE_PATH = 'packages/design-tokens/web';
910

10-
// Original console.log function
11-
const originalLog = console.log;
12-
13-
console.log = (message) => {
14-
/*
15-
Light/dark token names are in the same file but under different selectors
16-
But Style-Dictionary expects 1 unique token per file, or it will throw warn
17-
So, console output is overwritten to prevent unnecessary errors message
18-
*/
19-
if (!message.includes('token collisions')) {
20-
originalLog(message);
21-
}
22-
};
23-
2411
const sdConfig = {
2512
source: [`${BASE_PATH}/properties/**/*.json5`, `${BASE_PATH}/components/**/*.json5`],
2613
platforms: {
2714
css: {
2815
buildPath: `dist/design-tokens/web/components/styles/`,
2916
transformGroup: 'kbq/css',
30-
filter: (token) =>
31-
!['light', 'dark', 'font', 'size', 'typography', 'md-typography', 'palette'].includes(
32-
token.attributes.category
33-
)
17+
filter: (token) => !['font', 'size', 'typography', 'md-typography'].includes(token.attributes.category),
18+
options: {
19+
outputReferences: true
20+
}
3421
}
3522
}
3623
};
@@ -45,9 +32,14 @@ StyleDictionary.registerFormat({
4532
formatter: function ({ dictionary, options = {} }) {
4633
const { outputReferences, selector = ':root', darkThemeSelector = '.kbq-dark' } = options;
4734

35+
const allTokens = applyCustomTransformations(dictionary);
36+
4837
dictionary.allTokens.forEach((token) => {
4938
token.name = token.name.replace(/(light|dark)-/, '');
5039
});
40+
dictionary.allTokens = dictionary.allProperties = allTokens.filter(
41+
(token) => !['light', 'dark'].includes(token.attributes.category)
42+
);
5143

5244
// formatting function expects dictionary as input, so here initialize a copy to work with different tokens
5345
const baseDictionary = { ...dictionary };
@@ -82,7 +74,9 @@ const main = async () => {
8274
destination: `${path.basename(currentValue, `.${TOKEN_FILE_EXT}`)}.css`,
8375
filter: (token) =>
8476
token.attributes.category?.includes(path.basename(currentValue, `.${TOKEN_FILE_EXT}`)) ||
85-
path.basename(currentValue, `.${TOKEN_FILE_EXT}`).includes(token.attributes.category),
77+
path.basename(currentValue, `.${TOKEN_FILE_EXT}`).includes(token.attributes.category) ||
78+
// give access to light/dark/palette tokens to resolve reference manually
79+
['light', 'dark', 'palette'].includes(token.attributes.category),
8680
format: 'kbq-css/component',
8781
prefix: 'kbq'
8882
}));

0 commit comments

Comments
 (0)