-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (47 loc) · 1.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
const postcss = require('postcss');
const rfc = require('reduce-function-call');
const resolveValue = (value, map) => {
return rfc(value, 'var', body => {
let variable = map[body];
if (!variable) {
return value;
}
// Strip outer parentheses
if (variable.indexOf('(') === 0) {
variable = variable.replace(/^\(|\)$/g, '');
}
const decls = postcss.list.comma(variable);
// Convert to object
const result = decls.reduce((obj, decl) => {
const arr = decl.split(/[:]/).map(str => str.trim());
return Object.assign({}, obj, {
[arr[0]]: arr[1]
});
}, {});
// Remove undefined values
const props = Object.keys(result).filter(n => n);
const values = Object.keys(result).map(n => result[n]).filter(n => n);
return values.length ? `(${props}), (${values})` : `(${props})`;
});
};
const processRule = (params, map) => {
const values = params.split(/\s+in\s+/).map(str => postcss.list.comma(str))[1];
params = values.reduce((acc, value) => acc.replace(value, resolveValue(value, map)), params);
return params;
};
module.exports = postcss.plugin('postcss-each-variables', () => {
return css => {
const map = [];
css.walkRules(rule => {
if (rule.selector === ':root') {
rule.each(decl => {
map[decl.prop] = decl.value;
});
}
});
css.walkAtRules('each', rule => {
rule.params = processRule(rule.params, map);
});
};
});