|
|
|
@ -79,52 +79,32 @@ export function rootVariables(rules) { |
|
|
|
|
function reduceVariables(rules, options) { |
|
|
|
|
const defaultVariables = rootVariables(rules); |
|
|
|
|
const CSS_VAR = /(^var\((?<key>.+)\)|(?<val>.+))/; |
|
|
|
|
const reduce = (rule) => |
|
|
|
|
Object.assign( |
|
|
|
|
rule, |
|
|
|
|
rule.declarations |
|
|
|
|
? { |
|
|
|
|
declarations: rule.declarations.map((declaration) => { |
|
|
|
|
let { key, val } = CSS_VAR.exec(declaration.value).groups; |
|
|
|
|
// only one level of variable referencing is supported
|
|
|
|
|
key = |
|
|
|
|
CSS_VAR.exec(options[key] || defaultVariables[key]).groups |
|
|
|
|
.key || key; |
|
|
|
|
return Object.assign(declaration, { |
|
|
|
|
value: key |
|
|
|
|
? options[key] || defaultVariables[key] |
|
|
|
|
: declaration.value, |
|
|
|
|
}); |
|
|
|
|
}), |
|
|
|
|
} |
|
|
|
|
: {} |
|
|
|
|
); |
|
|
|
|
return rules |
|
|
|
|
.map((rule) => |
|
|
|
|
Object.assign( |
|
|
|
|
rule, |
|
|
|
|
rule.declarations |
|
|
|
|
? { |
|
|
|
|
declarations: rule.declarations.map((declaration) => { |
|
|
|
|
let { key, val } = CSS_VAR.exec(declaration.value).groups; |
|
|
|
|
// only one level of variable referencing is supported
|
|
|
|
|
key = |
|
|
|
|
CSS_VAR.exec(options[key] || defaultVariables[key]).groups |
|
|
|
|
.key || key; |
|
|
|
|
return Object.assign(declaration, { |
|
|
|
|
value: key |
|
|
|
|
? options[key] || defaultVariables[key] |
|
|
|
|
: declaration.value, |
|
|
|
|
}); |
|
|
|
|
}), |
|
|
|
|
} |
|
|
|
|
: {} |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
|
.map(reduce) |
|
|
|
|
.map((rule) => { |
|
|
|
|
if (!rule.media) return rule; |
|
|
|
|
return Object.assign(rule, { |
|
|
|
|
rules: rule.rules.map((rule) => |
|
|
|
|
Object.assign( |
|
|
|
|
rule, |
|
|
|
|
rule.declarations |
|
|
|
|
? { |
|
|
|
|
declarations: rule.declarations.map((declaration) => { |
|
|
|
|
let { key, val } = CSS_VAR.exec(declaration.value).groups; |
|
|
|
|
// only one level of variable referencing is supported
|
|
|
|
|
key = |
|
|
|
|
CSS_VAR.exec(options[key] || defaultVariables[key]).groups |
|
|
|
|
.key || key; |
|
|
|
|
return Object.assign(declaration, { |
|
|
|
|
value: key |
|
|
|
|
? options[key] || defaultVariables[key] |
|
|
|
|
: declaration.value, |
|
|
|
|
}); |
|
|
|
|
}), |
|
|
|
|
} |
|
|
|
|
: {} |
|
|
|
|
) |
|
|
|
|
), |
|
|
|
|
rules: rule.rules.map(reduce), |
|
|
|
|
}); |
|
|
|
|
}) |
|
|
|
|
.filter( |
|
|
|
|