-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
34 lines (32 loc) · 1019 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
34
let postcss = require('postcss')
function convert (sizeStr, zoom = 1, units = 'px') {
// eslint-disable-next-line security/detect-non-literal-regexp
let reg = new RegExp(`(-?\\d+)(\\.\\d+)?${units}`, 'g')
return (sizeStr || '').replace(reg, (value, index, str) => {
let unit = units ? value.replace(/-?\.?\d+/g, '') : ''
return `${Number(value.replace(unit, '')) * zoom}${unit}`
})
}
module.exports = postcss.plugin('postcss-design-convert', (opts = {}) => {
opts = opts || {} //
let {
multiple = 2,
units = ['vw'],
selector,
classRule = /./,
attribute
} = opts
selector = selector || classRule
// Work with options here
return function (root, result) {
// 遍历所有的选择器
root.walkRules(selector, rule => {
// 遍历所有的属性
let fun = decl => {
decl.value = convert(decl.value, multiple, (`(${units.join('|')})`))
}
let params = attribute ? [attribute, fun] : [fun]
rule.walkDecls(...params)
})
}
})