-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (30 loc) · 1.03 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
let postcss = require('postcss')
let pxRegExp = /\b(\d+(\.\d+)?)px\b/;
let pxGlobalRegExp = new RegExp(pxRegExp.source, 'g');
let checkScaleRegExp = /\/\* postcss-scale-px (.*?) \*\//;
module.exports = postcss.plugin('postcss-scale', (opts = { scale: 2 }) => {
// Work with options here
return (root, result) => {
// Transform CSS AST here
if (!checkScaleRegExp.exec(root.source.input.css)) {
result.root = root;
return;
}
let disableAttributes = [];
const disableAttributeString = checkScaleRegExp.exec(root.source.input.css)[1]
if (disableAttributeString) {
disableAttributes = disableAttributeString.split(',');
}
console.log('disableAttributes', disableAttributes)
root.walkDecls(function (decl) {
if (disableAttributes.indexOf(decl.prop) == -1) {
if (/px/.test(decl.value)) {
decl.value = decl.value.replace(pxGlobalRegExp, function (match, p1) {
return p1 * opts.scale + 'px'
})
}
}
});
result.root = root;
};
});