postcss-pxtransform 4.0.0-beta.9 → 4.0.0-beta.90

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
@@ -36,9 +36,12 @@ const DEFAULT_WEAPP_OPTIONS = {
36
36
 
37
37
  const processed = Symbol('processed')
38
38
 
39
-
40
39
  let targetUnit
41
40
 
41
+ const SPECIAL_PIXEL = ['Px', 'PX', 'pX']
42
+ let unConvertTargetUnit
43
+ let platform
44
+
42
45
  module.exports = (options = {}) => {
43
46
  options = Object.assign({}, DEFAULT_WEAPP_OPTIONS, options)
44
47
  const exclude = options.exclude
@@ -47,6 +50,7 @@ module.exports = (options = {}) => {
47
50
  const designWidth = (input) =>
48
51
  typeof options.designWidth === 'function' ? options.designWidth(input) : options.designWidth
49
52
 
53
+ platform = options.platform
50
54
  switch (options.platform) {
51
55
  case 'h5': {
52
56
  targetUnit = options.targetUnit ?? 'rem'
@@ -85,6 +89,8 @@ module.exports = (options = {}) => {
85
89
  case 'harmony': {
86
90
  options.rootValue = (input) => 1 / options.deviceRatio[designWidth(input)]
87
91
  targetUnit = 'px'
92
+ unConvertTargetUnit = 'ch' // harmony对于大小写的PX转换成其他单位,用于rust解析
93
+ transUnits.push(...SPECIAL_PIXEL)
88
94
  break
89
95
  }
90
96
  default: {
@@ -122,7 +128,7 @@ module.exports = (options = {}) => {
122
128
 
123
129
  /** 是否跳过当前文件不处理 */
124
130
  let skip = false
125
-
131
+
126
132
  if (exclude && exclude?.(result.opts.from)) {
127
133
  return null
128
134
  }
@@ -201,12 +207,24 @@ module.exports = (options = {}) => {
201
207
  // 标记当前 node 已处理
202
208
  decl[processed] = true
203
209
 
204
- if (decl.value.indexOf('px') === -1) return
210
+ if (!/px/i.test(decl.value)) return
205
211
 
206
212
  if (!satisfyPropList(decl.prop)) return
207
213
 
208
- if (blacklistedSelector(opts.selectorBlackList, decl.parent.selector)) return
209
- 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
+ }
210
228
  // if rem unit already exists, do not add or replace
211
229
  if (declarationExists(decl.parent, decl.prop, value)) return
212
230
  if (opts.replace) {
@@ -220,7 +238,7 @@ module.exports = (options = {}) => {
220
238
  if (skip) return
221
239
  if (!opts.methods.includes('size')) return
222
240
 
223
- if (rule.params.indexOf('px') === -1) return
241
+ if (!/px/i.test(rule.params)) return
224
242
  rule.params = rule.params.replace(pxRgx, pxReplace)
225
243
  },
226
244
  },
@@ -253,14 +271,28 @@ function convertLegacyOptions (options) {
253
271
  }
254
272
 
255
273
  function createPxReplace (rootValue, unitPrecision, minPixelValue, onePxTransform) {
274
+ const specialPxRgx = pxRegex(SPECIAL_PIXEL)
256
275
  return function (input) {
257
276
  return function (m, $1) {
258
277
  if (!$1) return m
278
+
279
+ if (platform === 'harmony' && specialPxRgx.test(m)) {
280
+ // harmony对大小写的PX转换成其他单位,用于rust解析
281
+ return $1 + unConvertTargetUnit
282
+ }
283
+
259
284
  if (!onePxTransform && parseInt($1, 10) === 1) {
285
+ if (platform === 'harmony') { return $1 + unConvertTargetUnit }
260
286
  return m
261
287
  }
262
288
  const pixels = parseFloat($1)
263
- 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 }
264
296
  let val = pixels / rootValue(input, m, $1)
265
297
  if (unitPrecision >= 0 && unitPrecision <= 100) {
266
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.9",
3
+ "version": "4.0.0-beta.90",
4
4
  "description": "PostCSS plugin px 转小程序 rpx及h5 rem 单位",
5
5
  "main": "index.js",
6
6
  "keywords": [