|
1 | 1 | import postcss from 'postcss';
|
2 | 2 |
|
3 | 3 | function hasVar(str) {
|
4 |
| - return str.includes('var('); |
| 4 | + return str.includes('var('); |
5 | 5 | }
|
6 | 6 |
|
7 | 7 | function resolveValue(value, maps) {
|
8 |
| - return hasVar(value) ? value.replace(/var\(--.*?\)/g, match => maps[match.slice(4, -1)] || match) : value; |
| 8 | + return hasVar(value) ? value.replace(/var\(--.*?\)/g, match => maps[match.slice(4, -1)] || match) : value; |
9 | 9 | }
|
10 | 10 |
|
11 | 11 | function getProperty(nodes) {
|
12 |
| - let propertys = {}; |
| 12 | + let propertys = {}; |
13 | 13 |
|
14 |
| - nodes.walkRules(rule => { |
15 |
| - if (rule.selector !== ':root') { |
16 |
| - return; |
17 |
| - } |
| 14 | + nodes.walkRules(rule => { |
| 15 | + if (rule.selector !== ':root') { |
| 16 | + return; |
| 17 | + } |
18 | 18 |
|
19 |
| - rule.each(({prop, value}) => { |
20 |
| - propertys[prop] = value; |
21 |
| - }); |
22 |
| - }); |
| 19 | + rule.each(({prop, value}) => { |
| 20 | + propertys[prop] = value; |
| 21 | + }); |
| 22 | + }); |
23 | 23 |
|
24 |
| - return propertys; |
| 24 | + return propertys; |
| 25 | +} |
| 26 | + |
| 27 | +function circularReference(maps) { |
| 28 | + return Object.keys(maps).reduce((prevMaps, property) => { |
| 29 | + prevMaps[property] = resolveValue(maps[property], maps); |
| 30 | + return prevMaps; |
| 31 | + }, maps); |
25 | 32 | }
|
26 | 33 |
|
27 | 34 | export default postcss.plugin('postcss-at-rules-variables', (options = {}) => {
|
28 |
| - options = { |
29 |
| - atRules: [...new Set(['for', 'if', 'else', 'each', 'mixin', 'custom-media', ...options.atRules || ''])], |
30 |
| - variables: options.variables || {} |
31 |
| - }; |
32 |
| - |
33 |
| - return nodes => { |
34 |
| - const maps = Object.assign(getProperty(nodes), options.variables); |
35 |
| - |
36 |
| - nodes.walkAtRules(new RegExp(options.atRules.join('|')), rules => { |
37 |
| - rules.params = resolveValue(rules.params, maps); |
38 |
| - }); |
39 |
| - }; |
| 35 | + options = { |
| 36 | + atRules: [...new Set(['for', 'if', 'else', 'each', 'mixin', 'custom-media', ...options.atRules || ''])], |
| 37 | + variables: options.variables || {} |
| 38 | + }; |
| 39 | + |
| 40 | + return nodes => { |
| 41 | + const maps = circularReference(Object.assign(getProperty(nodes), options.variables)); |
| 42 | + |
| 43 | + nodes.walkAtRules(new RegExp(options.atRules.join('|')), rules => { |
| 44 | + rules.params = resolveValue(rules.params, maps); |
| 45 | + }); |
| 46 | + }; |
40 | 47 | });
|
0 commit comments