postcss-pxtransform 3.6.5 → 3.7.0-alpha.1
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/__tests__/index.test.js +89 -0
- package/index.js +23 -7
- package/package.json +1 -1
package/__tests__/index.test.js
CHANGED
|
@@ -525,6 +525,58 @@ describe('platform 为 weapp', () => {
|
|
|
525
525
|
})
|
|
526
526
|
})
|
|
527
527
|
|
|
528
|
+
describe('platform 为 weapp, targetUnit 为 rem', () => {
|
|
529
|
+
it('{platform: \'weapp\', designWidth: 750, targetUnit: \'rem\'} ', () => {
|
|
530
|
+
const rules = 'h1 {margin: 0 0 20px;font-size: 40Px;line-height: 1.2;}'
|
|
531
|
+
const expected = 'h1 {margin: 0 0 0.5rem;font-size: 40Px;line-height: 1.2;}'
|
|
532
|
+
const options = {
|
|
533
|
+
platform: 'weapp',
|
|
534
|
+
designWidth: 750,
|
|
535
|
+
targetUnit: 'rem'
|
|
536
|
+
}
|
|
537
|
+
const processed = postcss(px2rem(options)).process(rules).css
|
|
538
|
+
expect(processed).toBe(expected)
|
|
539
|
+
})
|
|
540
|
+
|
|
541
|
+
it('{platform: \'weapp\', designWidth: 640, targetUnit: \'rem\'} ', () => {
|
|
542
|
+
const rules = 'h1 {margin: 0 0 20px;font-size: 40px;line-height: 1.2;}'
|
|
543
|
+
const expected = 'h1 {margin: 0 0 0.585rem;font-size: 1.17rem;line-height: 1.2;}'
|
|
544
|
+
const options = {
|
|
545
|
+
platform: 'weapp',
|
|
546
|
+
designWidth: 640,
|
|
547
|
+
targetUnit: 'rem'
|
|
548
|
+
}
|
|
549
|
+
const processed = postcss(px2rem(options)).process(rules).css
|
|
550
|
+
expect(processed).toBe(expected)
|
|
551
|
+
})
|
|
552
|
+
})
|
|
553
|
+
|
|
554
|
+
describe('platform 为 weapp, targetUnit 为 px', () => {
|
|
555
|
+
it('{platform: \'weapp\', designWidth: 750, targetUnit: \'px\'} ', () => {
|
|
556
|
+
const rules = 'h1 {margin: 0 0 20px;font-size: 40Px;line-height: 1.2;}'
|
|
557
|
+
const expected = 'h1 {margin: 0 0 10px;font-size: 40Px;line-height: 1.2;}'
|
|
558
|
+
const options = {
|
|
559
|
+
platform: 'weapp',
|
|
560
|
+
designWidth: 750,
|
|
561
|
+
targetUnit: 'px'
|
|
562
|
+
}
|
|
563
|
+
const processed = postcss(px2rem(options)).process(rules).css
|
|
564
|
+
expect(processed).toBe(expected)
|
|
565
|
+
})
|
|
566
|
+
|
|
567
|
+
it('{platform: \'weapp\', designWidth: 640, targetUnit: \'px\'} ', () => {
|
|
568
|
+
const rules = 'h1 {margin: 0 0 20px;font-size: 40px;line-height: 1.2;}'
|
|
569
|
+
const expected = 'h1 {margin: 0 0 11.7px;font-size: 23.4px;line-height: 1.2;}'
|
|
570
|
+
const options = {
|
|
571
|
+
platform: 'weapp',
|
|
572
|
+
designWidth: 640,
|
|
573
|
+
targetUnit: 'px'
|
|
574
|
+
}
|
|
575
|
+
const processed = postcss(px2rem(options)).process(rules).css
|
|
576
|
+
expect(processed).toBe(expected)
|
|
577
|
+
})
|
|
578
|
+
})
|
|
579
|
+
|
|
528
580
|
describe('platform 为 h5', () => {
|
|
529
581
|
it('{platform: \'h5\', designWidth: 750} ', () => {
|
|
530
582
|
const rules = 'h1 {margin: 0 0 20px;font-size: 40px;line-height: 1.2;}'
|
|
@@ -549,6 +601,32 @@ describe('platform 为 h5', () => {
|
|
|
549
601
|
})
|
|
550
602
|
})
|
|
551
603
|
|
|
604
|
+
describe('platform 为 h5, targetUnit 为 px', () => {
|
|
605
|
+
it('{platform: \'h5\', designWidth: 750, targetUnit: \'px\'} ', () => {
|
|
606
|
+
const rules = 'h1 {margin: 0 0 20px;font-size: 40px;line-height: 1.2;}'
|
|
607
|
+
const expected = 'h1 {margin: 0 0 10px;font-size: 20px;line-height: 1.2;}'
|
|
608
|
+
const options = {
|
|
609
|
+
platform: 'h5',
|
|
610
|
+
designWidth: 750,
|
|
611
|
+
targetUnit: 'px'
|
|
612
|
+
}
|
|
613
|
+
const processed = postcss(px2rem(options)).process(rules).css
|
|
614
|
+
expect(processed).toBe(expected)
|
|
615
|
+
})
|
|
616
|
+
|
|
617
|
+
it('{platform: \'h5\', designWidth: 640, targetUnit: \'px\'} ', () => {
|
|
618
|
+
const rules = 'h1 {margin: 0 0 20px;font-size: 40Px;line-height: 1.2;}'
|
|
619
|
+
const expected = 'h1 {margin: 0 0 11.7px;font-size: 40Px;line-height: 1.2;}'
|
|
620
|
+
const options = {
|
|
621
|
+
platform: 'h5',
|
|
622
|
+
designWidth: 640,
|
|
623
|
+
targetUnit: 'px'
|
|
624
|
+
}
|
|
625
|
+
const processed = postcss(px2rem(options)).process(rules).css
|
|
626
|
+
expect(processed).toBe(expected)
|
|
627
|
+
})
|
|
628
|
+
})
|
|
629
|
+
|
|
552
630
|
describe('platform 为 h5,文件头部带注释的不转换', () => {
|
|
553
631
|
it('{platform: \'h5\', designWidth: 640} ', () => {
|
|
554
632
|
const rules = '/*postcss-pxtransform disable*/ h1 {margin: 0 0 20px;font-size: 40Px;line-height: 1.2;}'
|
|
@@ -665,6 +743,17 @@ describe('vw 单位转换', () => {
|
|
|
665
743
|
const processed = postcss(px2rem(options)).process(rules).css
|
|
666
744
|
expect(processed).toBe('h1 {margin: 0 0 50vw;font-size: 40Px;line-height: 1.2;} .test{}')
|
|
667
745
|
})
|
|
746
|
+
|
|
747
|
+
it('{platform: \'h5\', designWidth: 640} ', () => {
|
|
748
|
+
const rules = 'h1 {margin: 0 0 320px;font-size: 40Px;line-height: 1.2;} .test{}'
|
|
749
|
+
const options = {
|
|
750
|
+
platform: 'h5',
|
|
751
|
+
designWidth: 640,
|
|
752
|
+
targetUnit: 'vw'
|
|
753
|
+
}
|
|
754
|
+
const processed = postcss(px2rem(options)).process(rules).css
|
|
755
|
+
expect(processed).toBe('h1 {margin: 0 0 50vw;font-size: 40Px;line-height: 1.2;} .test{}')
|
|
756
|
+
})
|
|
668
757
|
})
|
|
669
758
|
|
|
670
759
|
describe('platform 为 rn,适配', () => {
|
package/index.js
CHANGED
|
@@ -42,19 +42,27 @@ module.exports = (options = {}) => {
|
|
|
42
42
|
options = Object.assign({}, DEFAULT_WEAPP_OPTIONS, options)
|
|
43
43
|
|
|
44
44
|
const transUnits = ['px']
|
|
45
|
-
|
|
45
|
+
const baseFontSize = options.baseFontSize || (options.minRootSize >= 1 ? options.minRootSize : 20)
|
|
46
46
|
const designWidth = (input) =>
|
|
47
47
|
typeof options.designWidth === 'function' ? options.designWidth(input) : options.designWidth
|
|
48
48
|
|
|
49
49
|
switch (options.platform) {
|
|
50
50
|
case 'h5': {
|
|
51
51
|
targetUnit = options.targetUnit ?? 'rem'
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
|
|
53
|
+
if (targetUnit === 'vw') {
|
|
54
|
+
options.rootValue = (input) => {
|
|
55
|
+
return designWidth(input) / 100
|
|
56
|
+
}
|
|
57
|
+
} else if (targetUnit === 'px') {
|
|
58
|
+
options.rootValue = (input) => (1 / options.deviceRatio[designWidth(input)]) * 2
|
|
59
|
+
} else {
|
|
60
|
+
// rem
|
|
61
|
+
options.rootValue = (input) => {
|
|
62
|
+
return (baseFontSize / options.deviceRatio[designWidth(input)]) * 2
|
|
55
63
|
}
|
|
56
|
-
return (baseFontSize / options.deviceRatio[designWidth(input)]) * 2
|
|
57
64
|
}
|
|
65
|
+
|
|
58
66
|
transUnits.push('rpx')
|
|
59
67
|
break
|
|
60
68
|
}
|
|
@@ -75,8 +83,16 @@ module.exports = (options = {}) => {
|
|
|
75
83
|
}
|
|
76
84
|
default: {
|
|
77
85
|
// mini-program
|
|
78
|
-
|
|
79
|
-
|
|
86
|
+
targetUnit = options.targetUnit ?? 'rpx'
|
|
87
|
+
|
|
88
|
+
if (targetUnit === 'rem') {
|
|
89
|
+
options.rootValue = (input) => (baseFontSize / options.deviceRatio[designWidth(input)]) * 2
|
|
90
|
+
} else if (targetUnit === 'px') {
|
|
91
|
+
options.rootValue = (input) => (1 / options.deviceRatio[designWidth(input)]) * 2
|
|
92
|
+
} else {
|
|
93
|
+
// rpx
|
|
94
|
+
options.rootValue = (input) => 1 / options.deviceRatio[designWidth(input)]
|
|
95
|
+
}
|
|
80
96
|
}
|
|
81
97
|
}
|
|
82
98
|
|