-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (52 loc) · 1.89 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
const postcss = require('postcss')
module.exports = postcss.plugin('postcss-unit', opts => {
opts = opts || {};
const defaultOpts = {
from: 'px',
to: 'rpx',
convert: num => num,
precision: 3,
minValue: 0,
mediaQuery: false,
propWhiteList: [],
selectorBlackList: []
};
if (!Array.isArray(opts)) {
opts = [opts];
}
opts.forEach((opt, index) => {
opts[index] = Object.assign({}, defaultOpts, opt);
opts[index].__regexp__ = new RegExp(`(\\d*\\.?\\d+)${opts[index].from}` , 'ig');
});
const replace = (value, opt) => {
return value.replace(opt.__regexp__, (match, num) => {
if (!num || num <= opt.minValue) return match;
let targetNum = opt.convert(+num);
// if is decimal, apply precision
if (~targetNum.toString().indexOf('.') && opt.precision >= 0) {
targetNum = targetNum.toFixed(opt.precision);
}
return targetNum + opt.to;
});
};
const checkProp = (prop, propWhiteList) => {
return !propWhiteList.length || propWhiteList.some(item => !!prop.match(item));
}
const checkSelector = (selector, selectorBlackList) => {
return selectorBlackList.some(item => !!selector.match(item));
}
return (root, result) => {
opts.forEach(opt => {
root.walkDecls(decl => {
if (!~decl.value.indexOf(opt.from) || !checkProp(decl.prop, opt.propWhiteList) || checkSelector(decl.parent.selector, opt.selectorBlackList)) return;
decl.value = replace(decl.value, opt);
});
if (opt.mediaQuery) {
root.walkAtRules(rule => {
if (!~rule.params.indexOf(opt.from)) return;
rule.params = replace(rule.params, opt);
});
}
});
}
})