Skip to content

Commit 76e097b

Browse files
committed
feat: add declarationByWalk option, close issue #209
1 parent d140432 commit 76e097b

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/index.js

+27-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,29 @@ function circularReference(maps) {
1313
}, maps);
1414
}
1515

16+
function getProperty(nodes) {
17+
let propertys = {};
18+
19+
nodes.walkRules(rule => {
20+
if (rule.selector !== ':root') {
21+
return;
22+
}
23+
24+
rule.each(({type, prop: property, value}) => {
25+
if (type === 'decl') {
26+
propertys[property] = value;
27+
}
28+
});
29+
});
30+
31+
return propertys;
32+
}
33+
1634
module.exports = (options = {}) => {
1735
options = {
1836
atRules: [...new Set(['for', 'if', 'else', 'each', 'mixin', 'custom-media', ...options.atRules || ''])],
19-
variables: {...options.variables}
37+
variables: {...options.variables},
38+
declarationByWalk: options.declarationByWalk ?? false
2039
};
2140

2241
return {
@@ -29,8 +48,13 @@ module.exports = (options = {}) => {
2948
variables[node.prop] = node.value;
3049
}
3150
},
32-
Once() {
33-
variables = circularReference(Object.assign(variables, options.variables));
51+
Once(root) {
52+
let declarativeVariables = variables;
53+
if (options.declarationByWalk) {
54+
declarativeVariables = getProperty(root);
55+
}
56+
57+
variables = circularReference(Object.assign(declarativeVariables, options.variables));
3458
},
3559
AtRule(rule) {
3660
if (options.atRules.includes(rule.name)) {

0 commit comments

Comments
 (0)