postcss-pxtransform 4.0.0-beta.21 → 4.0.0-beta.22

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.
@@ -865,6 +865,6 @@ describe('platform 为 harmony,适配', () => {
865
865
  }
866
866
  }
867
867
  const processed = postcss(px2rem(options)).process(rules).css
868
- expect(processed).toBe('view { width: 100vp; }')
868
+ expect(processed).toBe('view { width: 100ch; }')
869
869
  })
870
870
  })
package/index.js CHANGED
@@ -38,6 +38,10 @@ const processed = Symbol('processed')
38
38
 
39
39
  let targetUnit
40
40
 
41
+ const SPECIAL_PIXEL = ['Px', 'PX', 'pX']
42
+ let unConvertTargetUnit
43
+ let platform
44
+
41
45
  module.exports = (options = {}) => {
42
46
  options = Object.assign({}, DEFAULT_WEAPP_OPTIONS, options)
43
47
  const exclude = options.exclude
@@ -46,6 +50,7 @@ module.exports = (options = {}) => {
46
50
  const designWidth = (input) =>
47
51
  typeof options.designWidth === 'function' ? options.designWidth(input) : options.designWidth
48
52
 
53
+ platform = options.platform
49
54
  switch (options.platform) {
50
55
  case 'h5': {
51
56
  targetUnit = options.targetUnit ?? 'rem'
@@ -84,6 +89,8 @@ module.exports = (options = {}) => {
84
89
  case 'harmony': {
85
90
  options.rootValue = (input) => 1 / options.deviceRatio[designWidth(input)]
86
91
  targetUnit = 'px'
92
+ unConvertTargetUnit = 'ch' // harmony对于大小写的PX转换成其他单位,用于rust解析
93
+ transUnits.push(...SPECIAL_PIXEL)
87
94
  break
88
95
  }
89
96
  default: {
@@ -200,12 +207,24 @@ module.exports = (options = {}) => {
200
207
  // 标记当前 node 已处理
201
208
  decl[processed] = true
202
209
 
203
- if (decl.value.indexOf('px') === -1) return
210
+ if (!/px/i.test(decl.value)) return
204
211
 
205
212
  if (!satisfyPropList(decl.prop)) return
206
213
 
207
- if (blacklistedSelector(opts.selectorBlackList, decl.parent.selector)) return
208
- const value = decl.value.replace(pxRgx, pxReplace)
214
+ const isBlacklisted = blacklistedSelector(opts.selectorBlackList, decl.parent.selector)
215
+ if (isBlacklisted && platform !== 'harmony') return
216
+ let value
217
+ if (isBlacklisted) {
218
+ // 如果是harmony平台,黑名单的样式单位做特殊处理
219
+ if (platform === 'harmony') {
220
+ value = decl.value.replace(pxRgx, (m, $1) => $1 ? $1 + unConvertTargetUnit : m)
221
+ } else {
222
+ // 如果是其他平台,黑名单的样式单位不做处理
223
+ return
224
+ }
225
+ } else {
226
+ value = decl.value.replace(pxRgx, pxReplace)
227
+ }
209
228
  // if rem unit already exists, do not add or replace
210
229
  if (declarationExists(decl.parent, decl.prop, value)) return
211
230
  if (opts.replace) {
@@ -219,7 +238,7 @@ module.exports = (options = {}) => {
219
238
  if (skip) return
220
239
  if (!opts.methods.includes('size')) return
221
240
 
222
- if (rule.params.indexOf('px') === -1) return
241
+ if (!/px/i.test(rule.params)) return
223
242
  rule.params = rule.params.replace(pxRgx, pxReplace)
224
243
  },
225
244
  },
@@ -252,14 +271,28 @@ function convertLegacyOptions (options) {
252
271
  }
253
272
 
254
273
  function createPxReplace (rootValue, unitPrecision, minPixelValue, onePxTransform) {
274
+ const specialPxRgx = pxRegex(SPECIAL_PIXEL)
255
275
  return function (input) {
256
276
  return function (m, $1) {
257
277
  if (!$1) return m
278
+
279
+ if (platform === 'harmony' && specialPxRgx.test(m)) {
280
+ // harmony对大小写的PX转换成其他单位,用于rust解析
281
+ return $1 + unConvertTargetUnit
282
+ }
283
+
258
284
  if (!onePxTransform && parseInt($1, 10) === 1) {
285
+ if (platform === 'harmony') { return $1 + unConvertTargetUnit }
259
286
  return m
260
287
  }
261
288
  const pixels = parseFloat($1)
262
- if (pixels < minPixelValue) return m
289
+ if (pixels < minPixelValue) {
290
+ if (platform === 'harmony') { return $1 + unConvertTargetUnit }
291
+ return m
292
+ }
293
+
294
+ // 转换工作,如果是harmony的话不转换
295
+ if (platform === 'harmony') { return m }
263
296
  let val = pixels / rootValue(input, m, $1)
264
297
  if (unitPrecision >= 0 && unitPrecision <= 100) {
265
298
  val = toFixed(val, unitPrecision)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-pxtransform",
3
- "version": "4.0.0-beta.21",
3
+ "version": "4.0.0-beta.22",
4
4
  "description": "PostCSS plugin px 转小程序 rpx及h5 rem 单位",
5
5
  "main": "index.js",
6
6
  "keywords": [