postcss-pxtransform 3.4.2 → 3.4.5

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.
Files changed (3) hide show
  1. package/README.md +17 -1
  2. package/index.js +21 -14
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -36,6 +36,22 @@ options = {
36
36
  }
37
37
  ```
38
38
 
39
+ ### 动态计算designWidth
40
+
41
+ 基础组件库一般是`375`,如果业务系统UI设计是`750`,可以如下配置后`1:1`切图,小程序、H5、RN时有效
42
+
43
+ ```js
44
+ options = {
45
+ platform: 'h5',
46
+ designWidth (input) {
47
+ if (input.file.replace(/\\+/g, '/').indexOf('@nutui/nutui-taro') > -1) {
48
+ return 375
49
+ }
50
+ return 750
51
+ },
52
+ }
53
+ ```
54
+
39
55
  ### 输入/输出
40
56
 
41
57
  默认配置下,所有的 px 都会被转换。
@@ -114,7 +130,7 @@ Type: `Object | Null`
114
130
  ### `platform` (String)(必填)
115
131
  `weapp` 或 `h5` 或 `rn`
116
132
 
117
- ### `designWidth`(Number)(必填)
133
+ ### `designWidth`(Number|Function)(必填)
118
134
  `640` 或 `750` 或 `828`
119
135
 
120
136
  ### `unitPrecision` (Number)
package/index.js CHANGED
@@ -42,25 +42,30 @@ let targetUnit
42
42
  module.exports = postcss.plugin('postcss-pxtransform', function (options = {}) {
43
43
  options = Object.assign({}, DEFAULT_WEAPP_OPTIONS, options)
44
44
 
45
+ const isFunctionDw = typeof options.designWidth === 'function'
46
+ const designWidth = input => isFunctionDw
47
+ ? options.designWidth(input)
48
+ : options.designWidth
49
+
45
50
  switch (options.platform) {
46
51
  case 'h5': {
47
- options.rootValue = baseFontSize * options.designWidth / 640
52
+ options.rootValue = input => baseFontSize * designWidth(input) / 640
48
53
  targetUnit = 'rem'
49
54
  break
50
55
  }
51
56
  case 'rn': {
52
- options.rootValue = 1 / options.deviceRatio[options.designWidth] * 2
57
+ options.rootValue = input => 1 / options.deviceRatio[designWidth(input)] * 2
53
58
  targetUnit = 'px'
54
59
  break
55
60
  }
56
61
  case 'quickapp': {
57
- options.rootValue = 1
62
+ options.rootValue = () => 1
58
63
  targetUnit = 'px'
59
64
  break
60
65
  }
61
66
  default: {
62
67
  // mini-program
63
- options.rootValue = 1 / options.deviceRatio[options.designWidth]
68
+ options.rootValue = input => 1 / options.deviceRatio[designWidth(input)]
64
69
  targetUnit = 'rpx'
65
70
  }
66
71
  }
@@ -69,12 +74,12 @@ module.exports = postcss.plugin('postcss-pxtransform', function (options = {}) {
69
74
 
70
75
  const opts = Object.assign({}, defaults, options)
71
76
  const onePxTransform = typeof options.onePxTransform === 'undefined' ? true : options.onePxTransform
72
- const pxReplace = createPxReplace(opts.rootValue, opts.unitPrecision,
73
- opts.minPixelValue, onePxTransform)
74
77
 
75
78
  const satisfyPropList = createPropListMatcher(opts.propList)
76
79
 
77
80
  return function (css) {
81
+ const pxReplace = createPxReplace(opts.rootValue, opts.unitPrecision, opts.minPixelValue, onePxTransform)(css.source.input)
82
+
78
83
  for (let i = 0; i < css.nodes.length; i++) {
79
84
  if (css.nodes[i].type === 'comment') {
80
85
  if (css.nodes[i].text === 'postcss-pxtransform disable') {
@@ -200,15 +205,17 @@ function convertLegacyOptions (options) {
200
205
  }
201
206
 
202
207
  function createPxReplace (rootValue, unitPrecision, minPixelValue, onePxTransform) {
203
- return function (m, $1) {
204
- if (!$1) return m
205
- if (!onePxTransform && parseInt($1, 10) === 1) {
206
- return m
208
+ return function (input) {
209
+ return function (m, $1) {
210
+ if (!$1) return m
211
+ if (!onePxTransform && parseInt($1, 10) === 1) {
212
+ return m
213
+ }
214
+ const pixels = parseFloat($1)
215
+ if (pixels < minPixelValue) return m
216
+ const fixedVal = toFixed((pixels / rootValue(input)), unitPrecision)
217
+ return (fixedVal === 0) ? '0' : fixedVal + targetUnit
207
218
  }
208
- const pixels = parseFloat($1)
209
- if (pixels < minPixelValue) return m
210
- const fixedVal = toFixed((pixels / rootValue), unitPrecision)
211
- return (fixedVal === 0) ? '0' : fixedVal + targetUnit
212
219
  }
213
220
  }
214
221
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-pxtransform",
3
- "version": "3.4.2",
3
+ "version": "3.4.5",
4
4
  "description": "PostCSS plugin px 转小程序 rpx及h5 rem 单位",
5
5
  "keywords": [
6
6
  "postcss",
@@ -28,5 +28,5 @@
28
28
  "testEnvironment": "node"
29
29
  },
30
30
  "main": "index.js",
31
- "gitHead": "26b329326c6a7da652e8d712ee33df01ab8d2d0e"
31
+ "gitHead": "789e873f7b9477124b44a900cd4f6a2810e4e06a"
32
32
  }