postcss-pxtransform 3.6.0-beta.2 → 3.6.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/index.test.js +4 -3
- package/index.js +113 -108
- package/package.json +1 -1
package/__tests__/index.test.js
CHANGED
|
@@ -66,9 +66,10 @@ describe('px2rem', function () {
|
|
|
66
66
|
expect(processed).toBe(expected)
|
|
67
67
|
})
|
|
68
68
|
|
|
69
|
-
it('7
|
|
70
|
-
const
|
|
71
|
-
const
|
|
69
|
+
it('7 属性值为"0"时不处理,为"0px"时仍然单位转换', function () {
|
|
70
|
+
const rule = '.rule { font-size: 0px; font-size: 0; }'
|
|
71
|
+
const expected = '.rule { font-size: 0rpx; font-size: 0; }'
|
|
72
|
+
const processed = postcss(px2rem()).process(rule).css
|
|
72
73
|
|
|
73
74
|
expect(processed).toBe(expected)
|
|
74
75
|
})
|
package/index.js
CHANGED
|
@@ -40,18 +40,17 @@ module.exports = (options = {}) => {
|
|
|
40
40
|
|
|
41
41
|
const transUnits = ['px']
|
|
42
42
|
const baseFontSize = options.baseFontSize || (options.minRootSize >= 1 ? options.minRootSize : 20)
|
|
43
|
-
const designWidth = input =>
|
|
44
|
-
? options.designWidth(input)
|
|
45
|
-
: options.designWidth
|
|
43
|
+
const designWidth = (input) =>
|
|
44
|
+
typeof options.designWidth === 'function' ? options.designWidth(input) : options.designWidth
|
|
46
45
|
switch (options.platform) {
|
|
47
46
|
case 'h5': {
|
|
48
|
-
options.rootValue = input => baseFontSize / options.deviceRatio[designWidth(input)] * 2
|
|
47
|
+
options.rootValue = (input) => (baseFontSize / options.deviceRatio[designWidth(input)]) * 2
|
|
49
48
|
targetUnit = 'rem'
|
|
50
49
|
transUnits.push('rpx')
|
|
51
50
|
break
|
|
52
51
|
}
|
|
53
52
|
case 'rn': {
|
|
54
|
-
options.rootValue = input => 1 / options.deviceRatio[designWidth(input)] * 2
|
|
53
|
+
options.rootValue = (input) => (1 / options.deviceRatio[designWidth(input)]) * 2
|
|
55
54
|
targetUnit = 'px'
|
|
56
55
|
break
|
|
57
56
|
}
|
|
@@ -61,13 +60,13 @@ module.exports = (options = {}) => {
|
|
|
61
60
|
break
|
|
62
61
|
}
|
|
63
62
|
case 'harmony': {
|
|
64
|
-
options.rootValue = input => 1 / options.deviceRatio[designWidth(input)]
|
|
63
|
+
options.rootValue = (input) => 1 / options.deviceRatio[designWidth(input)]
|
|
65
64
|
targetUnit = 'px'
|
|
66
65
|
break
|
|
67
66
|
}
|
|
68
67
|
default: {
|
|
69
68
|
// mini-program
|
|
70
|
-
options.rootValue = input => 1 / options.deviceRatio[designWidth(input)]
|
|
69
|
+
options.rootValue = (input) => 1 / options.deviceRatio[designWidth(input)]
|
|
71
70
|
targetUnit = 'rpx'
|
|
72
71
|
}
|
|
73
72
|
}
|
|
@@ -82,117 +81,124 @@ module.exports = (options = {}) => {
|
|
|
82
81
|
|
|
83
82
|
return {
|
|
84
83
|
postcssPlugin: 'postcss-pxtransform',
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
84
|
+
prepare (result) {
|
|
85
|
+
const pxReplace = createPxReplace(
|
|
86
|
+
opts.rootValue,
|
|
87
|
+
opts.unitPrecision,
|
|
88
|
+
opts.minPixelValue,
|
|
89
|
+
onePxTransform
|
|
90
|
+
)(result.root.source.input)
|
|
89
91
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
99
|
-
const temp = next.next()
|
|
100
|
-
next.remove()
|
|
101
|
-
next = temp
|
|
92
|
+
/** 是否跳过当前文件不处理 */
|
|
93
|
+
let skip = false
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
Comment (comment) {
|
|
97
|
+
if (comment.text === 'postcss-pxtransform disable') {
|
|
98
|
+
skip = true
|
|
99
|
+
return
|
|
102
100
|
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
101
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
102
|
+
// delete code between comment in RN
|
|
103
|
+
// 有死循环的问题
|
|
104
|
+
if (options.platform === 'rn') {
|
|
105
|
+
if (comment.text === 'postcss-pxtransform rn eject enable') {
|
|
106
|
+
let next = comment.next()
|
|
107
|
+
while (next) {
|
|
108
|
+
if (next.text === 'postcss-pxtransform rn eject disable') {
|
|
109
|
+
break
|
|
110
|
+
}
|
|
111
|
+
const temp = next.next()
|
|
112
|
+
next.remove()
|
|
113
|
+
next = temp
|
|
114
|
+
}
|
|
118
115
|
}
|
|
119
|
-
const temp = next.next()
|
|
120
|
-
next.remove()
|
|
121
|
-
next = temp
|
|
122
116
|
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
117
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
118
|
+
/* #ifdef %PLATFORM%
|
|
119
|
+
* 平台特有样式
|
|
120
|
+
* #endif */
|
|
121
|
+
const wordList = comment.text.split(' ')
|
|
122
|
+
// 指定平台保留
|
|
123
|
+
if (wordList.indexOf('#ifdef') > -1) {
|
|
124
|
+
// 非指定平台
|
|
125
|
+
if (wordList.indexOf(options.platform) === -1) {
|
|
126
|
+
let next = comment.next()
|
|
127
|
+
while (next) {
|
|
128
|
+
if (next.type === 'comment' && next.text.trim() === '#endif') {
|
|
129
|
+
break
|
|
130
|
+
}
|
|
131
|
+
const temp = next.next()
|
|
132
|
+
next.remove()
|
|
133
|
+
next = temp
|
|
134
|
+
}
|
|
137
135
|
}
|
|
138
|
-
const temp = next.next()
|
|
139
|
-
next.remove()
|
|
140
|
-
next = temp
|
|
141
136
|
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
},
|
|
145
|
-
|
|
146
|
-
Declaration (decl) {
|
|
147
|
-
if (options.platform === 'harmony') {
|
|
148
|
-
if (decl.value.indexOf('PX') === -1) return
|
|
149
|
-
const value = decl.value.replace(PXRegex, function (m, _$1, $2) {
|
|
150
|
-
return m.replace($2, 'vp')
|
|
151
|
-
})
|
|
152
|
-
decl.value = value
|
|
153
|
-
}
|
|
154
|
-
},
|
|
155
|
-
|
|
156
|
-
AtRule (rule) {
|
|
157
|
-
if (options.platform === 'harmony' && rule.name === 'media') {
|
|
158
|
-
if (rule.params.indexOf('PX') === -1) return
|
|
159
|
-
const value = rule.params.replace(PXRegex, function (m, _$1, $2) {
|
|
160
|
-
return m.replace($2, 'vp')
|
|
161
|
-
})
|
|
162
|
-
rule.params = value
|
|
163
|
-
}
|
|
164
|
-
},
|
|
165
137
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
138
|
+
/* #ifdef %PLATFORM%
|
|
139
|
+
* 平台特有样式
|
|
140
|
+
* #endif */
|
|
141
|
+
// 指定平台剔除
|
|
142
|
+
if (wordList.indexOf('#ifndef') > -1) {
|
|
143
|
+
// 指定平台
|
|
144
|
+
if (wordList.indexOf(options.platform) > -1) {
|
|
145
|
+
let next = comment.next()
|
|
146
|
+
while (next) {
|
|
147
|
+
if (next.type === 'comment' && next.text.trim() === '#endif') {
|
|
148
|
+
break
|
|
149
|
+
}
|
|
150
|
+
const temp = next.next()
|
|
151
|
+
next.remove()
|
|
152
|
+
next = temp
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
Declaration (decl) {
|
|
158
|
+
if (skip) return
|
|
172
159
|
|
|
173
|
-
|
|
160
|
+
if (options.platform === 'harmony') {
|
|
161
|
+
if (decl.value.indexOf('PX') === -1) return
|
|
162
|
+
const value = decl.value.replace(PXRegex, function (m, _$1, $2) {
|
|
163
|
+
return m.replace($2, 'vp')
|
|
164
|
+
})
|
|
165
|
+
decl.value = value
|
|
166
|
+
} else {
|
|
167
|
+
if (decl.value.indexOf('px') === -1) return
|
|
174
168
|
|
|
175
|
-
|
|
169
|
+
if (!satisfyPropList(decl.prop)) return
|
|
176
170
|
|
|
177
|
-
|
|
171
|
+
if (blacklistedSelector(opts.selectorBlackList, decl.parent.selector)) return
|
|
178
172
|
|
|
179
|
-
|
|
180
|
-
|
|
173
|
+
const value = decl.value.replace(pxRgx, pxReplace)
|
|
174
|
+
// if rem unit already exists, do not add or replace
|
|
175
|
+
if (declarationExists(decl.parent, decl.prop, value)) return
|
|
181
176
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
177
|
+
if (opts.replace) {
|
|
178
|
+
decl.value = value
|
|
179
|
+
} else {
|
|
180
|
+
decl.cloneAfter({ value: value })
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
},
|
|
188
184
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
185
|
+
AtRule: {
|
|
186
|
+
media: (rule) => {
|
|
187
|
+
if (skip) return
|
|
188
|
+
if (options.platform === 'harmony') {
|
|
189
|
+
if (rule.params.indexOf('PX') === -1) return
|
|
190
|
+
const value = rule.params.replace(PXRegex, function (m, _$1, $2) {
|
|
191
|
+
return m.replace($2, 'vp')
|
|
192
|
+
})
|
|
193
|
+
rule.params = value
|
|
194
|
+
} else {
|
|
195
|
+
if (rule.params.indexOf('px') === -1) return
|
|
196
|
+
rule.params = rule.params.replace(pxRgx, pxReplace)
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
},
|
|
194
200
|
}
|
|
195
|
-
}
|
|
201
|
+
},
|
|
196
202
|
}
|
|
197
203
|
}
|
|
198
204
|
|
|
@@ -228,9 +234,8 @@ function createPxReplace (rootValue, unitPrecision, minPixelValue, onePxTransfor
|
|
|
228
234
|
}
|
|
229
235
|
const pixels = parseFloat($1)
|
|
230
236
|
if (pixels < minPixelValue) return m
|
|
231
|
-
const fixedVal = toFixed(
|
|
232
|
-
//
|
|
233
|
-
// 不带单位不支持在calc表达式中参与计算
|
|
237
|
+
const fixedVal = toFixed(pixels / rootValue(input, m, $1), unitPrecision)
|
|
238
|
+
// 不带单位不支持在calc表达式中参与计算(https://github.com/NervJS/taro/issues/12607)
|
|
234
239
|
return fixedVal + targetUnit
|
|
235
240
|
}
|
|
236
241
|
}
|
|
@@ -239,12 +244,12 @@ function createPxReplace (rootValue, unitPrecision, minPixelValue, onePxTransfor
|
|
|
239
244
|
function toFixed (number, precision) {
|
|
240
245
|
const multiplier = Math.pow(10, precision + 1)
|
|
241
246
|
const wholeNumber = Math.floor(number * multiplier)
|
|
242
|
-
return Math.round(wholeNumber / 10) * 10 / multiplier
|
|
247
|
+
return (Math.round(wholeNumber / 10) * 10) / multiplier
|
|
243
248
|
}
|
|
244
249
|
|
|
245
250
|
function declarationExists (decls, prop, value) {
|
|
246
251
|
return decls.some(function (decl) {
|
|
247
|
-
return
|
|
252
|
+
return decl.prop === prop && decl.value === value
|
|
248
253
|
})
|
|
249
254
|
}
|
|
250
255
|
|
|
@@ -258,7 +263,7 @@ function blacklistedSelector (blacklist, selector) {
|
|
|
258
263
|
|
|
259
264
|
function createPropListMatcher (propList) {
|
|
260
265
|
const hasWild = propList.indexOf('*') > -1
|
|
261
|
-
const matchAll =
|
|
266
|
+
const matchAll = hasWild && propList.length === 1
|
|
262
267
|
const lists = {
|
|
263
268
|
exact: filterPropList.exact(propList),
|
|
264
269
|
contain: filterPropList.contain(propList),
|