@@ -26,35 +26,39 @@ export const resolveAllCSSVariables = (root: Root) => {
26
26
const declarationsForAtRules = new Map < AtRule , Set < Declaration > > ( ) ;
27
27
const valueReplacingInformation = new Set < {
28
28
declaration : Declaration ;
29
- newValue : string ;
29
+ replacing : string ;
30
+ replacement : string ;
30
31
} > ( ) ;
31
32
32
- rule . walkDecls ( ( decl ) => {
33
- if ( / v a r \( - - [ ^ \s ) ] + \) / . test ( decl . value ) ) {
33
+ rule . walkDecls ( ( declaration ) => {
34
+ if ( / v a r \( - - [ ^ \s ) ] + \) / . test ( declaration . value ) ) {
34
35
/**
35
36
* @example ['var(--width)', 'var(--length)']
36
37
*/
37
- const variablesUsed = / v a r \( - - [ ^ \s ) ] + \) / gm. exec ( decl . value ) ;
38
+ const variablesUsed = [
39
+ ...declaration . value . matchAll ( / v a r \( - - [ ^ \s ) ] + \) / gm) ,
40
+ ] . map ( ( match ) => match . toString ( ) ) ;
41
+
38
42
root . walkDecls ( ( otherDecl ) => {
39
43
if ( / - - [ ^ \s ] + / . test ( otherDecl . prop ) ) {
40
44
const variable = `var(${ otherDecl . prop } )` ;
41
45
if (
42
46
variablesUsed ?. includes ( variable ) &&
43
- doNodesMatch ( decl . parent , otherDecl . parent )
47
+ doNodesMatch ( declaration . parent , otherDecl . parent )
44
48
) {
45
49
if (
46
50
otherDecl . parent ?. parent instanceof AtRule &&
47
- otherDecl . parent !== decl . parent
51
+ otherDecl . parent !== declaration . parent
48
52
) {
49
53
const atRule = otherDecl . parent . parent ;
50
54
51
55
const clonedDeclaration = createDeclaration ( ) ;
52
- clonedDeclaration . prop = decl . prop ;
53
- clonedDeclaration . value = decl . value . replaceAll (
56
+ clonedDeclaration . prop = declaration . prop ;
57
+ clonedDeclaration . value = declaration . value . replaceAll (
54
58
variable ,
55
59
otherDecl . value ,
56
60
) ;
57
- clonedDeclaration . important = decl . important ;
61
+ clonedDeclaration . important = declaration . important ;
58
62
59
63
const declarationForAtRule = declarationsForAtRules . get ( atRule ) ;
60
64
if ( declarationForAtRule ) {
@@ -69,17 +73,22 @@ export const resolveAllCSSVariables = (root: Root) => {
69
73
}
70
74
71
75
valueReplacingInformation . add ( {
72
- declaration : decl ,
73
- newValue : decl . value . replaceAll ( variable , otherDecl . value ) ,
76
+ declaration,
77
+ replacing : variable ,
78
+ replacement : otherDecl . value ,
74
79
} ) ;
75
80
}
76
81
}
77
82
} ) ;
78
83
}
79
84
} ) ;
80
85
81
- for ( const { declaration, newValue } of valueReplacingInformation ) {
82
- declaration . value = newValue ;
86
+ for ( const {
87
+ declaration,
88
+ replacing,
89
+ replacement,
90
+ } of valueReplacingInformation ) {
91
+ declaration . value = declaration . value . replaceAll ( replacing , replacement ) ;
83
92
}
84
93
85
94
for ( const [ atRule , declarations ] of declarationsForAtRules . entries ( ) ) {
0 commit comments