postcss-pxtransform 3.8.0-canary.0 → 4.0.0-alpha.2
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 +14 -0
- package/__tests__/index.test.js +79 -0
- package/index.js +40 -41
- package/package.json +2 -2
package/LICENSE
CHANGED
|
@@ -158,3 +158,17 @@ MIT (stencil-vue2-output-target):
|
|
|
158
158
|
The following files embed [stencil-vue2-output-target](https://github.com/diondree/stencil-vue2-output-target) MIT:
|
|
159
159
|
`/packages/taro-components-library-vue2/src/vue-component-lib/utils.ts`
|
|
160
160
|
See `/LICENSE` for details of the license.
|
|
161
|
+
|
|
162
|
+
==================
|
|
163
|
+
|
|
164
|
+
MIT (weui):
|
|
165
|
+
The following files embed [stencil-vue2-output-target](https://github.com/Tencent/weui) MIT:
|
|
166
|
+
`/packages/taro-components/src/components/*.scss`
|
|
167
|
+
See `/LICENSE.txt` for details of the license.
|
|
168
|
+
|
|
169
|
+
==================
|
|
170
|
+
|
|
171
|
+
Apache-2.0 (intersection-observer):
|
|
172
|
+
The following files embed [intersection-observer](https://github.com/GoogleChromeLabs/intersection-observer) Apache-2.0:
|
|
173
|
+
`/packages/taro-api/src/polyfill/intersection-observer.ts`
|
|
174
|
+
See `/LICENSE.txt` for details of the license.
|
package/__tests__/index.test.js
CHANGED
|
@@ -523,6 +523,17 @@ describe('platform 为 weapp', () => {
|
|
|
523
523
|
const processed = postcss(px2rem(options)).process(rules).css
|
|
524
524
|
expect(processed).toBe(expected)
|
|
525
525
|
})
|
|
526
|
+
|
|
527
|
+
it('{platform: \'weapp\', designWidth: 375} ', () => {
|
|
528
|
+
const rules = 'h1 {margin: 0 0 20px;font-size: 40px;line-height: 1.2;}'
|
|
529
|
+
const expected = 'h1 {margin: 0 0 40rpx;font-size: 80rpx;line-height: 1.2;}'
|
|
530
|
+
const options = {
|
|
531
|
+
platform: 'weapp',
|
|
532
|
+
designWidth: 375
|
|
533
|
+
}
|
|
534
|
+
const processed = postcss(px2rem(options)).process(rules).css
|
|
535
|
+
expect(processed).toBe(expected)
|
|
536
|
+
})
|
|
526
537
|
})
|
|
527
538
|
|
|
528
539
|
describe('platform 为 weapp, targetUnit 为 rem', () => {
|
|
@@ -549,6 +560,18 @@ describe('platform 为 weapp, targetUnit 为 rem', () => {
|
|
|
549
560
|
const processed = postcss(px2rem(options)).process(rules).css
|
|
550
561
|
expect(processed).toBe(expected)
|
|
551
562
|
})
|
|
563
|
+
|
|
564
|
+
it('{platform: \'weapp\', designWidth: 375, targetUnit: \'rem\'} ', () => {
|
|
565
|
+
const rules = 'h1 {margin: 0 0 20px;font-size: 40px;line-height: 1.2;}'
|
|
566
|
+
const expected = 'h1 {margin: 0 0 1rem;font-size: 2rem;line-height: 1.2;}'
|
|
567
|
+
const options = {
|
|
568
|
+
platform: 'weapp',
|
|
569
|
+
designWidth: 375,
|
|
570
|
+
targetUnit: 'rem'
|
|
571
|
+
}
|
|
572
|
+
const processed = postcss(px2rem(options)).process(rules).css
|
|
573
|
+
expect(processed).toBe(expected)
|
|
574
|
+
})
|
|
552
575
|
})
|
|
553
576
|
|
|
554
577
|
describe('platform 为 weapp, targetUnit 为 px', () => {
|
|
@@ -575,6 +598,18 @@ describe('platform 为 weapp, targetUnit 为 px', () => {
|
|
|
575
598
|
const processed = postcss(px2rem(options)).process(rules).css
|
|
576
599
|
expect(processed).toBe(expected)
|
|
577
600
|
})
|
|
601
|
+
|
|
602
|
+
it('{platform: \'weapp\', designWidth: 375, targetUnit: \'px\'} ', () => {
|
|
603
|
+
const rules = 'h1 {margin: 0 0 20px;font-size: 40px;line-height: 1.2;}'
|
|
604
|
+
const expected = 'h1 {margin: 0 0 20px;font-size: 40px;line-height: 1.2;}'
|
|
605
|
+
const options = {
|
|
606
|
+
platform: 'weapp',
|
|
607
|
+
designWidth: 375,
|
|
608
|
+
targetUnit: 'px'
|
|
609
|
+
}
|
|
610
|
+
const processed = postcss(px2rem(options)).process(rules).css
|
|
611
|
+
expect(processed).toBe(expected)
|
|
612
|
+
})
|
|
578
613
|
})
|
|
579
614
|
|
|
580
615
|
describe('platform 为 h5', () => {
|
|
@@ -599,6 +634,17 @@ describe('platform 为 h5', () => {
|
|
|
599
634
|
const processed = postcss(px2rem(options)).process(rules).css
|
|
600
635
|
expect(processed).toBe(expected)
|
|
601
636
|
})
|
|
637
|
+
|
|
638
|
+
it('{platform: \'h5\', designWidth: 375} ', () => {
|
|
639
|
+
const rules = 'h1 {margin: 0 0 20px;font-size: 40Px;line-height: 1.2;}'
|
|
640
|
+
const expected = 'h1 {margin: 0 0 1rem;font-size: 40Px;line-height: 1.2;}'
|
|
641
|
+
const options = {
|
|
642
|
+
platform: 'h5',
|
|
643
|
+
designWidth: 375
|
|
644
|
+
}
|
|
645
|
+
const processed = postcss(px2rem(options)).process(rules).css
|
|
646
|
+
expect(processed).toBe(expected)
|
|
647
|
+
})
|
|
602
648
|
})
|
|
603
649
|
|
|
604
650
|
describe('platform 为 h5, targetUnit 为 px', () => {
|
|
@@ -625,6 +671,18 @@ describe('platform 为 h5, targetUnit 为 px', () => {
|
|
|
625
671
|
const processed = postcss(px2rem(options)).process(rules).css
|
|
626
672
|
expect(processed).toBe(expected)
|
|
627
673
|
})
|
|
674
|
+
|
|
675
|
+
it('{platform: \'h5\', designWidth: 375, targetUnit: \'px\'} ', () => {
|
|
676
|
+
const rules = 'h1 {margin: 0 0 20px;font-size: 40Px;line-height: 1.2;}'
|
|
677
|
+
const expected = 'h1 {margin: 0 0 20px;font-size: 40Px;line-height: 1.2;}'
|
|
678
|
+
const options = {
|
|
679
|
+
platform: 'h5',
|
|
680
|
+
designWidth: 375,
|
|
681
|
+
targetUnit: 'px'
|
|
682
|
+
}
|
|
683
|
+
const processed = postcss(px2rem(options)).process(rules).css
|
|
684
|
+
expect(processed).toBe(expected)
|
|
685
|
+
})
|
|
628
686
|
})
|
|
629
687
|
|
|
630
688
|
describe('platform 为 h5,文件头部带注释的不转换', () => {
|
|
@@ -719,6 +777,16 @@ describe('rpx 单位转换', () => {
|
|
|
719
777
|
const processed = postcss(px2rem(options)).process(rules).css
|
|
720
778
|
expect(processed).toBe('h1 {margin: 0 0 0.585rem;font-size: 40Px;line-height: 1.2;} .test{}')
|
|
721
779
|
})
|
|
780
|
+
|
|
781
|
+
it('{platform: \'weapp\', designWidth: 375} ', () => {
|
|
782
|
+
const rules = 'h1 {margin: 0 0 20rpx;font-size: 40Px;line-height: 1.2;} .test{}'
|
|
783
|
+
const options = {
|
|
784
|
+
platform: 'weapp',
|
|
785
|
+
designWidth: 375
|
|
786
|
+
}
|
|
787
|
+
const processed = postcss(px2rem(options)).process(rules).css
|
|
788
|
+
expect(processed).toBe('h1 {margin: 0 0 20rpx;font-size: 40Px;line-height: 1.2;} .test{}')
|
|
789
|
+
})
|
|
722
790
|
})
|
|
723
791
|
|
|
724
792
|
describe('vw 单位转换', () => {
|
|
@@ -754,6 +822,17 @@ describe('vw 单位转换', () => {
|
|
|
754
822
|
const processed = postcss(px2rem(options)).process(rules).css
|
|
755
823
|
expect(processed).toBe('h1 {margin: 0 0 50vw;font-size: 40Px;line-height: 1.2;} .test{}')
|
|
756
824
|
})
|
|
825
|
+
|
|
826
|
+
it('{platform: \'h5\', designWidth: 375} ', () => {
|
|
827
|
+
const rules = 'h1 {margin: 0 0 320px;font-size: 40Px;line-height: 1.2;} .test{}'
|
|
828
|
+
const options = {
|
|
829
|
+
platform: 'h5',
|
|
830
|
+
designWidth: 375,
|
|
831
|
+
targetUnit: 'vw'
|
|
832
|
+
}
|
|
833
|
+
const processed = postcss(px2rem(options)).process(rules).css
|
|
834
|
+
expect(processed).toBe('h1 {margin: 0 0 85.33333vw;font-size: 40Px;line-height: 1.2;} .test{}')
|
|
835
|
+
})
|
|
757
836
|
})
|
|
758
837
|
|
|
759
838
|
describe('platform 为 rn,适配', () => {
|
package/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const pxRegex = require('./lib/pixel-unit-regex')
|
|
2
|
-
const PXRegex = require('./lib/pixel-upper-unit-regex')
|
|
3
2
|
const filterPropList = require('./lib/filter-prop-list')
|
|
4
3
|
|
|
5
4
|
const defaults = {
|
|
5
|
+
methods: ['platform', 'size'],
|
|
6
6
|
rootValue: 16,
|
|
7
7
|
unitPrecision: 5,
|
|
8
8
|
selectorBlackList: [],
|
|
@@ -22,6 +22,7 @@ const legacyOptions = {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
const deviceRatio = {
|
|
25
|
+
375: 2,
|
|
25
26
|
640: 2.34 / 2,
|
|
26
27
|
750: 1,
|
|
27
28
|
828: 1.81 / 2
|
|
@@ -40,7 +41,7 @@ let targetUnit
|
|
|
40
41
|
|
|
41
42
|
module.exports = (options = {}) => {
|
|
42
43
|
options = Object.assign({}, DEFAULT_WEAPP_OPTIONS, options)
|
|
43
|
-
|
|
44
|
+
const exclude = options.exclude
|
|
44
45
|
const transUnits = ['px']
|
|
45
46
|
const baseFontSize = options.baseFontSize || (options.minRootSize >= 1 ? options.minRootSize : 20)
|
|
46
47
|
const designWidth = (input) =>
|
|
@@ -50,17 +51,22 @@ module.exports = (options = {}) => {
|
|
|
50
51
|
case 'h5': {
|
|
51
52
|
targetUnit = options.targetUnit ?? 'rem'
|
|
52
53
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
54
|
+
switch (targetUnit) {
|
|
55
|
+
case 'vw':
|
|
56
|
+
case 'vmin':
|
|
57
|
+
options.rootValue = (input) => {
|
|
58
|
+
return designWidth(input) / 100
|
|
59
|
+
}
|
|
60
|
+
break
|
|
61
|
+
case 'px':
|
|
62
|
+
options.rootValue = (input) => (1 / options.deviceRatio[designWidth(input)]) * 2
|
|
63
|
+
break
|
|
64
|
+
default:
|
|
65
|
+
// rem
|
|
66
|
+
options.rootValue = (input) => {
|
|
67
|
+
return (baseFontSize / options.deviceRatio[designWidth(input)]) * 2
|
|
68
|
+
}
|
|
69
|
+
break
|
|
64
70
|
}
|
|
65
71
|
|
|
66
72
|
transUnits.push('rpx')
|
|
@@ -116,6 +122,10 @@ module.exports = (options = {}) => {
|
|
|
116
122
|
|
|
117
123
|
/** 是否跳过当前文件不处理 */
|
|
118
124
|
let skip = false
|
|
125
|
+
|
|
126
|
+
if (exclude && exclude?.(result.opts.from)) {
|
|
127
|
+
return null
|
|
128
|
+
}
|
|
119
129
|
|
|
120
130
|
return {
|
|
121
131
|
// 注意:钩子在节点变动时会重新执行,Once,OnceExit只执行一次,https://github.com/NervJS/taro/issues/13238
|
|
@@ -125,6 +135,8 @@ module.exports = (options = {}) => {
|
|
|
125
135
|
return
|
|
126
136
|
}
|
|
127
137
|
|
|
138
|
+
if (!opts.methods.includes('platform')) return
|
|
139
|
+
|
|
128
140
|
// delete code between comment in RN
|
|
129
141
|
// 有死循环的问题
|
|
130
142
|
if (options.platform === 'rn') {
|
|
@@ -182,47 +194,34 @@ module.exports = (options = {}) => {
|
|
|
182
194
|
},
|
|
183
195
|
Declaration (decl) {
|
|
184
196
|
if (skip) return
|
|
197
|
+
if (!opts.methods.includes('size')) return
|
|
185
198
|
|
|
186
199
|
if (decl[processed]) return
|
|
187
200
|
|
|
188
201
|
// 标记当前 node 已处理
|
|
189
202
|
decl[processed] = true
|
|
190
203
|
|
|
191
|
-
if (
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
204
|
+
if (decl.value.indexOf('px') === -1) return
|
|
205
|
+
|
|
206
|
+
if (!satisfyPropList(decl.prop)) return
|
|
207
|
+
|
|
208
|
+
if (blacklistedSelector(opts.selectorBlackList, decl.parent.selector)) return
|
|
209
|
+
const value = decl.value.replace(pxRgx, pxReplace)
|
|
210
|
+
// if rem unit already exists, do not add or replace
|
|
211
|
+
if (declarationExists(decl.parent, decl.prop, value)) return
|
|
212
|
+
if (opts.replace) {
|
|
196
213
|
decl.value = value
|
|
197
214
|
} else {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
if (!satisfyPropList(decl.prop)) return
|
|
201
|
-
|
|
202
|
-
if (blacklistedSelector(opts.selectorBlackList, decl.parent.selector)) return
|
|
203
|
-
const value = decl.value.replace(pxRgx, pxReplace)
|
|
204
|
-
// if rem unit already exists, do not add or replace
|
|
205
|
-
if (declarationExists(decl.parent, decl.prop, value)) return
|
|
206
|
-
if (opts.replace) {
|
|
207
|
-
decl.value = value
|
|
208
|
-
} else {
|
|
209
|
-
decl.cloneAfter({ value: value })
|
|
210
|
-
}
|
|
215
|
+
decl.cloneAfter({ value: value })
|
|
211
216
|
}
|
|
212
217
|
},
|
|
213
218
|
AtRule: {
|
|
214
219
|
media: (rule) => {
|
|
215
220
|
if (skip) return
|
|
216
|
-
if (
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
})
|
|
221
|
-
rule.params = value
|
|
222
|
-
} else {
|
|
223
|
-
if (rule.params.indexOf('px') === -1) return
|
|
224
|
-
rule.params = rule.params.replace(pxRgx, pxReplace)
|
|
225
|
-
}
|
|
221
|
+
if (!opts.methods.includes('size')) return
|
|
222
|
+
|
|
223
|
+
if (rule.params.indexOf('px') === -1) return
|
|
224
|
+
rule.params = rule.params.replace(pxRgx, pxReplace)
|
|
226
225
|
},
|
|
227
226
|
},
|
|
228
227
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-pxtransform",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-alpha.2",
|
|
4
4
|
"description": "PostCSS plugin px 转小程序 rpx及h5 rem 单位",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"keywords": [
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"bugs": {
|
|
19
19
|
"url": "https://github.com/NervJS/taro/issues"
|
|
20
20
|
},
|
|
21
|
-
"homepage": "https://github.com/NervJS/taro#readme",
|
|
21
|
+
"homepage": "https://github.com/NervJS/taro/tree/next/packages/postcss-pxtransform#readme",
|
|
22
22
|
"jest": {
|
|
23
23
|
"testEnvironment": "node",
|
|
24
24
|
"testEnvironmentOptions": {}
|