postcss-pxtransform 3.6.0-canary.9 → 3.6.1-alpha.0

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2018
3
+ Copyright (c) 2018 O2Team
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -19,3 +19,121 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
22
+
23
+ ========================================================================
24
+
25
+ MIT (react-devtools):
26
+ The following files embed [react-devtools](https://github.com/facebook/react) MIT:
27
+ `/packages/taro-plugin-react-devtools/src/backend/index.js`
28
+ See `/LICENSE` for details of the license.
29
+
30
+ ==================
31
+
32
+ MIT (vue-devtools):
33
+ The following files embed [vue-devtools](https://github.com/vuejs/devtools) MIT:
34
+ `/packages/taro-plugin-vue-devtools/src/backend/*`
35
+ See `/LICENSE` for details of the license.
36
+
37
+ ==================
38
+
39
+ MIT (vite):
40
+ The following files embed [vite](https://github.com/vitejs/vite) MIT:
41
+ `/packages/taro-webpack5-prebundle/src/prebundle/scanImport.ts`,
42
+ `/packages/taro-webpack5-prebundle/src/prebundle/bundle.ts`,
43
+ `/packages/taro-webpack5-prebundle/src/h5.ts`,
44
+ `/packages/taro-webpack5-prebundle/src/min.ts`
45
+ See `/LICENSE` for details of the license.
46
+
47
+ ==================
48
+
49
+ MIT (webpack):
50
+ The following files embed [webpack](https://github.com/webpack/webpack) MIT:
51
+ `/packages/taro-webpack5-prebundle/src/webpack/TaroContainerEntryModule.ts`,
52
+ `/packages/taro-webpack5-prebundle/src/webpack/TaroContainerPlugin.ts`,
53
+ `/packages/taro-webpack5-prebundle/src/webpack/TaroContainerReferencePlugin.ts`,
54
+ `/packages/taro-webpack5-prebundle/src/webpack/TaroModuleFederationPlugin.ts`
55
+ See `/LICENSE` for details of the license.
56
+
57
+ ==================
58
+
59
+ MIT (sizzle):
60
+ The following files embed [sizzle](https://github.com/jquery/sizzle) MIT:
61
+ `/packages/taro-extends/src/jquery/sizzle.js`
62
+ See `/LICENSE.txt` for details of the license.
63
+
64
+ ==================
65
+
66
+ MIT (zepto):
67
+ The following files embed [zepto](https://github.com/madrobby/zepto) MIT:
68
+ `/packages/taro-extends/src/jquery/zepto.js`,
69
+ `/packages/taro-extends/src/jquery/event.js`
70
+ See `/MIT-LICENSE` for details of the license.
71
+
72
+ ==================
73
+
74
+ MIT (css-to-react-native):
75
+ The following files embed [css-to-react-native](https://github.com/styled-components/css-to-react-native) MIT:
76
+ `/packages/css-to-react-native/src/css-to-react-native/*`
77
+ See `/LICENSE.md` for details of the license.
78
+
79
+ ==================
80
+
81
+ MIT (reactify-wc):
82
+ The following files embed [reactify-wc](https://github.com/BBKolton/reactify-wc) MIT:
83
+ `/packages/taro-components-library-react/src/component-lib/reactify-wc.ts`
84
+ See `/LICENSE` for details of the license.
85
+
86
+ ==================
87
+
88
+ MIT (ant-design-mobile-rn):
89
+ The following files embed [ant-design-mobile-rn](https://github.com/ant-design/ant-design-mobile-rn) MIT:
90
+ `/packages/taro-components-rn/src/components/Swiper/carousel.tsx`
91
+ See `/LICENSE` for details of the license.
92
+
93
+ ==================
94
+
95
+ MIT (react-wx-images-viewer):
96
+ The following files embed [react-wx-images-viewer](https://github.com/react-ld/react-wx-images-viewer) MIT:
97
+ `/packages/taro-h5/src/api/media/image/previewImage.ts`
98
+ See `/LICENSE` for details of the license.
99
+
100
+ ==================
101
+
102
+ MIT (webpack-contrib/css-loader):
103
+ The following files embed [webpack-contrib/css-loader](https://github.com/webpack-contrib/css-loader) MIT:
104
+ `/packages/taro-rn-style-transformer/src/utils/index.ts`
105
+ See `/LICENSE` for details of the license.
106
+
107
+ ==================
108
+
109
+ MIT (react-native):
110
+ The following files embed [react-native](https://github.com/facebook/react-native) MIT:
111
+ `/packages/taro-rn-style-transformer/src/transforms/StyleSheet/*`
112
+ See `/LICENSE` for details of the license.
113
+
114
+ ==================
115
+
116
+ MIT (myrne/performance-now):
117
+ The following files embed [myrne/performance-now](https://github.com/myrne/performance-now) MIT:
118
+ `/packages/taro-runtime/src/bom/raf.ts`
119
+ See `/LICENSE` for details of the license.
120
+
121
+ ==================
122
+
123
+ Apache (chameleon-api):
124
+ The following files embed [chameleon-api](https://github.com/chameleon-team/chameleon-api) Apache:
125
+ `/packages/taro-h5/src/api/device/clipboard.ts`
126
+
127
+ ==================
128
+
129
+ MIT (uni-app):
130
+ The following files embed [uni-app](https://github.com/dcloudio/uni-app) MIT:
131
+ `/packages/taro-components-rn/src/components/Video/index.tsx`
132
+ See `/LICENSE` for details of the license.
133
+
134
+ ==================
135
+
136
+ MIT (miniprogram-render):
137
+ The following files embed [miniprogram-render](https://github.com/Tencent/kbone) MIT:
138
+ `/packages/taro-plugin-http/src/runtime/Cookie.ts`
139
+ See `/LICENSE` for details of the license.
@@ -66,13 +66,14 @@ describe('px2rem', function () {
66
66
  expect(processed).toBe(expected)
67
67
  })
68
68
 
69
- it('7 should remain unitless if 0', function () {
70
- const expected = '.rule { font-size: 0px; font-size: 0; }'
71
- const processed = postcss(px2rem()).process(expected).css
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
  })
75
-
76
+
76
77
  it('8 should work on custom baseFontSize', function () {
77
78
  const processed = postcss(px2rem({ platform: 'h5', baseFontSize: 15 })).process(basicCSS).css
78
79
  const expected = '.rule { font-size: 0.5rem }'
@@ -640,3 +641,37 @@ describe('rpx 单位转换', () => {
640
641
  expect(processed).toBe('h1 {margin: 0 0 0.585rem;font-size: 40Px;line-height: 1.2;} .test{}')
641
642
  })
642
643
  })
644
+
645
+ describe('platform 为 rn,适配', () => {
646
+ it('{platform: \'rn\', designWidth: 750} ', () => {
647
+ const rules = 'view { width: 100px; }'
648
+ const options = {
649
+ platform: 'rn',
650
+ designWidth: 750,
651
+ deviceRatio: {
652
+ 640: 2.34 / 2,
653
+ 750: 1,
654
+ 828: 1.81 / 2
655
+ }
656
+ }
657
+ const processed = postcss(px2rem(options)).process(rules).css
658
+ expect(processed).toBe('view { width: 50px; }')
659
+ })
660
+ })
661
+
662
+ describe('platform 为 harmony,适配', () => {
663
+ it('{platform: \'harmony\', designWidth: 640} ', () => {
664
+ const rules = 'view { width: 100PX; }'
665
+ const options = {
666
+ platform: 'harmony',
667
+ designWidth: 640,
668
+ deviceRatio: {
669
+ 640: 2.34 / 2,
670
+ 750: 1,
671
+ 828: 1.81 / 2
672
+ }
673
+ }
674
+ const processed = postcss(px2rem(options)).process(rules).css
675
+ expect(processed).toBe('view { width: 100vp; }')
676
+ })
677
+ })
package/index.js CHANGED
@@ -33,6 +33,9 @@ const DEFAULT_WEAPP_OPTIONS = {
33
33
  deviceRatio
34
34
  }
35
35
 
36
+ const processed = Symbol('processed')
37
+
38
+
36
39
  let targetUnit
37
40
 
38
41
  module.exports = (options = {}) => {
@@ -40,18 +43,17 @@ module.exports = (options = {}) => {
40
43
 
41
44
  const transUnits = ['px']
42
45
  const baseFontSize = options.baseFontSize || (options.minRootSize >= 1 ? options.minRootSize : 20)
43
- const designWidth = input => typeof options.designWidth === 'function'
44
- ? options.designWidth(input)
45
- : options.designWidth
46
+ const designWidth = (input) =>
47
+ typeof options.designWidth === 'function' ? options.designWidth(input) : options.designWidth
46
48
  switch (options.platform) {
47
49
  case 'h5': {
48
- options.rootValue = input => baseFontSize / options.deviceRatio[designWidth(input)] * 2
50
+ options.rootValue = (input) => (baseFontSize / options.deviceRatio[designWidth(input)]) * 2
49
51
  targetUnit = 'rem'
50
52
  transUnits.push('rpx')
51
53
  break
52
54
  }
53
55
  case 'rn': {
54
- options.rootValue = input => 1 / options.deviceRatio[designWidth(input)] * 2
56
+ options.rootValue = (input) => (1 / options.deviceRatio[designWidth(input)]) * 2
55
57
  targetUnit = 'px'
56
58
  break
57
59
  }
@@ -61,13 +63,13 @@ module.exports = (options = {}) => {
61
63
  break
62
64
  }
63
65
  case 'harmony': {
64
- options.rootValue = input => 1 / options.deviceRatio[designWidth(input)]
66
+ options.rootValue = (input) => 1 / options.deviceRatio[designWidth(input)]
65
67
  targetUnit = 'px'
66
68
  break
67
69
  }
68
70
  default: {
69
71
  // mini-program
70
- options.rootValue = input => 1 / options.deviceRatio[designWidth(input)]
72
+ options.rootValue = (input) => 1 / options.deviceRatio[designWidth(input)]
71
73
  targetUnit = 'rpx'
72
74
  }
73
75
  }
@@ -82,117 +84,127 @@ module.exports = (options = {}) => {
82
84
 
83
85
  return {
84
86
  postcssPlugin: 'postcss-pxtransform',
85
- Comment (comment) {
86
- if (comment.text === 'postcss-pxtransform disable') {
87
- return
88
- }
87
+ prepare (result) {
88
+ const pxReplace = createPxReplace(
89
+ opts.rootValue,
90
+ opts.unitPrecision,
91
+ opts.minPixelValue,
92
+ onePxTransform
93
+ )(result.root.source.input)
89
94
 
90
- // delete code between comment in RN
91
- // 有死循环的问题
92
- if (options.platform === 'rn') {
93
- if (comment.text === 'postcss-pxtransform rn eject enable') {
94
- let next = comment.next()
95
- while (next) {
96
- if (next.text === 'postcss-pxtransform rn eject disable') {
97
- break
98
- }
99
- const temp = next.next()
100
- next.remove()
101
- next = temp
95
+ /** 是否跳过当前文件不处理 */
96
+ let skip = false
97
+
98
+ return {
99
+ // 注意:钩子在节点变动时会重新执行,Once,OnceExit只执行一次,https://github.com/NervJS/taro/issues/13238
100
+ Comment (comment) {
101
+ if (comment.text === 'postcss-pxtransform disable') {
102
+ skip = true
103
+ return
102
104
  }
103
- }
104
- }
105
105
 
106
- /* #ifdef %PLATFORM%
107
- * 平台特有样式
108
- * #endif */
109
- const wordList = comment.text.split(' ')
110
- // 指定平台保留
111
- if (wordList.indexOf('#ifdef') > -1) {
112
- // 非指定平台
113
- if (wordList.indexOf(options.platform) === -1) {
114
- let next = comment.next()
115
- while (next) {
116
- if (next.type === 'comment' && next.text.trim() === '#endif') {
117
- break
106
+ // delete code between comment in RN
107
+ // 有死循环的问题
108
+ if (options.platform === 'rn') {
109
+ if (comment.text === 'postcss-pxtransform rn eject enable') {
110
+ let next = comment.next()
111
+ while (next) {
112
+ if (next.text === 'postcss-pxtransform rn eject disable') {
113
+ break
114
+ }
115
+ const temp = next.next()
116
+ next.remove()
117
+ next = temp
118
+ }
118
119
  }
119
- const temp = next.next()
120
- next.remove()
121
- next = temp
122
120
  }
123
- }
124
- }
125
121
 
126
- /* #ifdef %PLATFORM%
127
- * 平台特有样式
128
- * #endif */
129
- // 指定平台剔除
130
- if (wordList.indexOf('#ifndef') > -1) {
131
- // 指定平台
132
- if (wordList.indexOf(options.platform) > -1) {
133
- let next = comment.next()
134
- while (next) {
135
- if (next.type === 'comment' && next.text.trim() === '#endif') {
136
- break
122
+ /* #ifdef %PLATFORM%
123
+ * 平台特有样式
124
+ * #endif */
125
+ const wordList = comment.text.split(' ')
126
+ // 指定平台保留
127
+ if (wordList.indexOf('#ifdef') > -1) {
128
+ // 非指定平台
129
+ if (wordList.indexOf(options.platform) === -1) {
130
+ let next = comment.next()
131
+ while (next) {
132
+ if (next.type === 'comment' && next.text.trim() === '#endif') {
133
+ break
134
+ }
135
+ const temp = next.next()
136
+ next.remove()
137
+ next = temp
138
+ }
137
139
  }
138
- const temp = next.next()
139
- next.remove()
140
- next = temp
141
140
  }
142
- }
143
- }
144
- },
145
141
 
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
-
166
- Once (root) {
167
- const pxReplace = createPxReplace(opts.rootValue, opts.unitPrecision, opts.minPixelValue, onePxTransform)(root.source.input)
168
-
169
- root.walkDecls(function (decl, i) {
170
- // This should be the fastest test and will remove most declarations
171
- if (decl.value.indexOf('px') === -1) return
172
-
173
- if (!satisfyPropList(decl.prop)) return
142
+ /* #ifdef %PLATFORM%
143
+ * 平台特有样式
144
+ * #endif */
145
+ // 指定平台剔除
146
+ if (wordList.indexOf('#ifndef') > -1) {
147
+ // 指定平台
148
+ if (wordList.indexOf(options.platform) > -1) {
149
+ let next = comment.next()
150
+ while (next) {
151
+ if (next.type === 'comment' && next.text.trim() === '#endif') {
152
+ break
153
+ }
154
+ const temp = next.next()
155
+ next.remove()
156
+ next = temp
157
+ }
158
+ }
159
+ }
160
+ },
161
+ Declaration (decl) {
162
+ if (skip) return
174
163
 
175
- if (blacklistedSelector(opts.selectorBlackList, decl.parent.selector)) return
164
+ if (decl[processed]) return
176
165
 
177
- const value = decl.value.replace(pxRgx, pxReplace)
166
+ // 标记当前 node 已处理
167
+ decl[processed] = true
178
168
 
179
- // if rem unit already exists, do not add or replace
180
- if (declarationExists(decl.parent, decl.prop, value)) return
169
+ if (options.platform === 'harmony') {
170
+ if (decl.value.indexOf('PX') === -1) return
171
+ const value = decl.value.replace(PXRegex, function (m, _$1, $2) {
172
+ return m.replace($2, 'vp')
173
+ })
174
+ decl.value = value
175
+ } else {
176
+ if (decl.value.indexOf('px') === -1) return
181
177
 
182
- if (opts.replace) {
183
- decl.value = value
184
- } else {
185
- decl.parent.insertAfter(i, decl.clone({ value: value }))
186
- }
187
- })
178
+ if (!satisfyPropList(decl.prop)) return
188
179
 
189
- if (opts.mediaQuery) {
190
- root.walkAtRules('media', function (rule) {
191
- if (rule.params.indexOf('px') === -1) return
192
- rule.params = rule.params.replace(pxRgx, pxReplace)
193
- })
180
+ if (blacklistedSelector(opts.selectorBlackList, decl.parent.selector)) return
181
+ const value = decl.value.replace(pxRgx, pxReplace)
182
+ // if rem unit already exists, do not add or replace
183
+ if (declarationExists(decl.parent, decl.prop, value)) return
184
+ if (opts.replace) {
185
+ decl.value = value
186
+ } else {
187
+ decl.cloneAfter({ value: value })
188
+ }
189
+ }
190
+ },
191
+ AtRule: {
192
+ media: (rule) => {
193
+ if (skip) return
194
+ if (options.platform === 'harmony') {
195
+ if (rule.params.indexOf('PX') === -1) return
196
+ const value = rule.params.replace(PXRegex, function (m, _$1, $2) {
197
+ return m.replace($2, 'vp')
198
+ })
199
+ rule.params = value
200
+ } else {
201
+ if (rule.params.indexOf('px') === -1) return
202
+ rule.params = rule.params.replace(pxRgx, pxReplace)
203
+ }
204
+ },
205
+ },
194
206
  }
195
- }
207
+ },
196
208
  }
197
209
  }
198
210
 
@@ -228,9 +240,8 @@ function createPxReplace (rootValue, unitPrecision, minPixelValue, onePxTransfor
228
240
  }
229
241
  const pixels = parseFloat($1)
230
242
  if (pixels < minPixelValue) return m
231
- const fixedVal = toFixed((pixels / rootValue(input, m, $1)), unitPrecision)
232
- // return (fixedVal === 0) ? '0' : fixedVal + targetUnit
233
- // 不带单位不支持在calc表达式中参与计算
243
+ const fixedVal = toFixed(pixels / rootValue(input, m, $1), unitPrecision)
244
+ // 不带单位不支持在calc表达式中参与计算(https://github.com/NervJS/taro/issues/12607)
234
245
  return fixedVal + targetUnit
235
246
  }
236
247
  }
@@ -239,12 +250,12 @@ function createPxReplace (rootValue, unitPrecision, minPixelValue, onePxTransfor
239
250
  function toFixed (number, precision) {
240
251
  const multiplier = Math.pow(10, precision + 1)
241
252
  const wholeNumber = Math.floor(number * multiplier)
242
- return Math.round(wholeNumber / 10) * 10 / multiplier
253
+ return (Math.round(wholeNumber / 10) * 10) / multiplier
243
254
  }
244
255
 
245
256
  function declarationExists (decls, prop, value) {
246
257
  return decls.some(function (decl) {
247
- return (decl.prop === prop && decl.value === value)
258
+ return decl.prop === prop && decl.value === value
248
259
  })
249
260
  }
250
261
 
@@ -258,7 +269,7 @@ function blacklistedSelector (blacklist, selector) {
258
269
 
259
270
  function createPropListMatcher (propList) {
260
271
  const hasWild = propList.indexOf('*') > -1
261
- const matchAll = (hasWild && propList.length === 1)
272
+ const matchAll = hasWild && propList.length === 1
262
273
  const lists = {
263
274
  exact: filterPropList.exact(propList),
264
275
  contain: filterPropList.contain(propList),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-pxtransform",
3
- "version": "3.6.0-canary.9",
3
+ "version": "3.6.1-alpha.0",
4
4
  "description": "PostCSS plugin px 转小程序 rpx及h5 rem 单位",
5
5
  "main": "index.js",
6
6
  "keywords": [