-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
64 lines (54 loc) · 1.61 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
61
62
63
64
var postcss = require('postcss')
var pxtorem = require('postcss-pxtorem')
const PLATFORM = {
WEAPP: 'weapp',
H5: 'h5'
}
const DEVICE_RATIO = {
'640': 2.34 / 2,
'750': 1,
'828': 1.81 / 2
}
const DEFAULT_H5_OPTIONS = {
platform: 'h5',
designWidth: 750,
rootValue: 40,
propList: ['*'] // enable all properties
}
const DEFAULT_WEAPP_OPTIONS = {
platform: 'weapp',
designWidth: 750,
rootValue: 1,
propList: ['*'] // enable all properties
}
module.exports = postcss.plugin('postcss-pxtransform', function (opts) {
opts = opts || DEFAULT_WEAPP_OPTIONS
return function (root, result) {
switch (opts.platform) {
case PLATFORM.WEAPP:
return dealWithWeapp({root, result, opts})
case PLATFORM.H5:
return dealWithH5({root, result, opts})
}
}
})
function dealWithWeapp ({root, opts, result}) {
opts = Object.assign({}, DEFAULT_WEAPP_OPTIONS, opts)
var css = postcss(pxtorem(opts)).process(root.toResult()).css
var cssRoot = postcss.parse(css)
root.nodes = cssRoot.nodes
root.walkDecls(function (decl) {
let value = decl.value
value = value.replace(/([0-9.]+)rem/ig, function (match, size) {
return Math.ceil(size / DEVICE_RATIO[opts.designWidth] *
10000) / 10000 + 'rpx'
})
decl.value = value
})
}
function dealWithH5 ({root, result, opts}) {
opts = Object.assign({}, DEFAULT_H5_OPTIONS, opts)
var css = postcss(pxtorem(opts)).process(root.toResult()).css
var cssRoot = postcss.parse(css)
root.nodes = cssRoot.nodes
}