@@ -2,6 +2,7 @@ import * as fs from 'fs/promises';
2
2
import path from 'path' ;
3
3
import postcss from 'postcss' ;
4
4
import postcssCustomProperties from 'postcss-custom-properties' ;
5
+ import * as postCssValueParser from 'postcss-values-parser' ;
5
6
6
7
export const CacheCustomProperties = async masterCSSPath => {
7
8
const CSS_PATH = path . resolve ( masterCSSPath ) ;
@@ -18,12 +19,35 @@ export const CacheCustomProperties = async masterCSSPath => {
18
19
}
19
20
) ;
20
21
22
+ /**
23
+ * Walk through all the declarations and find the custom properties
24
+ * and their values. Store them in the fileData object.
25
+ */
21
26
postcssResult . root . walkDecls ( decl => {
22
27
if ( decl . prop . startsWith ( '--' ) ) {
23
28
fileData . customProperties [ decl . prop ] = decl . value ;
24
29
}
25
30
} ) ;
26
31
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
+
27
51
let json = JSON . stringify ( fileData ) ;
28
52
29
53
try {
0 commit comments