postcss-pxtransform 3.7.0-beta.1 → 3.7.0-beta.4

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.
@@ -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
@@ -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
@@ -50,17 +51,22 @@ module.exports = (options = {}) => {
50
51
  case 'h5': {
51
52
  targetUnit = options.targetUnit ?? 'rem'
52
53
 
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
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')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-pxtransform",
3
- "version": "3.7.0-beta.1",
3
+ "version": "3.7.0-beta.4",
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": {}