Skip to content

Commit d4a200b

Browse files
committed
fix: store the final value of any custom properties
this is used to display the final value in storybook as well as in the dist css files
1 parent c9660d8 commit d4a200b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

packages/uui-css/scripts/cache-custom-properties.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as fs from 'fs/promises';
22
import path from 'path';
33
import postcss from 'postcss';
44
import postcssCustomProperties from 'postcss-custom-properties';
5+
import * as postCssValueParser from 'postcss-values-parser';
56

67
export const CacheCustomProperties = async masterCSSPath => {
78
const CSS_PATH = path.resolve(masterCSSPath);
@@ -18,12 +19,35 @@ export const CacheCustomProperties = async masterCSSPath => {
1819
}
1920
);
2021

22+
/**
23+
* Walk through all the declarations and find the custom properties
24+
* and their values. Store them in the fileData object.
25+
*/
2126
postcssResult.root.walkDecls(decl => {
2227
if (decl.prop.startsWith('--')) {
2328
fileData.customProperties[decl.prop] = decl.value;
2429
}
2530
});
2631

32+
/**
33+
* Walk through all the custom properties and find the ones that
34+
* have a single var() value. Replace the value with the value of
35+
* the var() it references.
36+
*/
37+
for (const key in fileData.customProperties) {
38+
const valueNode = postCssValueParser.parse(
39+
fileData.customProperties[key]
40+
);
41+
const onlyVars = valueNode.nodes.filter(node => node.isVar);
42+
if (onlyVars.length === 1) {
43+
const keyToFind = onlyVars[0].params
44+
.trim()
45+
.substring(1, onlyVars[0].params.length - 1);
46+
47+
fileData.customProperties[key] = fileData.customProperties[keyToFind];
48+
}
49+
}
50+
2751
let json = JSON.stringify(fileData);
2852

2953
try {

0 commit comments

Comments
 (0)