-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
33 lines (30 loc) · 885 Bytes
/
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
var postcss = require('postcss');
var assign = require('object-assign');
module.exports = postcss.plugin('postcss-px2units', function (opts) {
opts = opts || {};
opts = assign({
divisor: 1,
multiple: 1,
decimalPlaces: 2,
targetUnits: 'rpx',
comment: 'no'
}, opts);
function repalcePx(str) {
if (!str) {
return '';
}
return str.replace(/\b(\d+(\.\d+)?)px\b/ig, function (match, x) {
var size = x * opts.multiple / opts.divisor;
return size % 1 === 0 ? size + opts.targetUnits : size.toFixed(opts.decimalPlaces) + opts.targetUnits;
});
}
return function (root) {
root.walkDecls(function (decl) {
if (decl && decl.next() && decl.next().type === 'comment' && decl.next().text === opts.comment) {
decl.next().remove();
} else {
decl.value = repalcePx(decl.value);
}
});
};
});