postcss-pxtransform 3.5.0-beta.1 → 3.5.0-beta.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.
@@ -619,3 +619,24 @@ describe('platform 为 h5,指定平台 h5 平台剔除', () => {
619
619
  expect(processed).toBe('/* #ifndef h5 *//* #endif */ .test{}')
620
620
  })
621
621
  })
622
+
623
+ describe('rpx 单位转换', () => {
624
+ it('{platform: \'weapp\', designWidth: 640} ', () => {
625
+ const rules = 'h1 {margin: 0 0 20rpx;font-size: 40Px;line-height: 1.2;} .test{}'
626
+ const options = {
627
+ platform: 'weapp',
628
+ designWidth: 640
629
+ }
630
+ const processed = postcss(pxtorem(options)).process(rules).css
631
+ expect(processed).toBe('h1 {margin: 0 0 20rpx;font-size: 40Px;line-height: 1.2;} .test{}')
632
+ })
633
+ it('{platform: \'h5\', designWidth: 640} ', () => {
634
+ const rules = 'h1 {margin: 0 0 20rpx;font-size: 40Px;line-height: 1.2;} .test{}'
635
+ const options = {
636
+ platform: 'h5',
637
+ designWidth: 640
638
+ }
639
+ const processed = postcss(pxtorem(options)).process(rules).css
640
+ expect(processed).toBe('h1 {margin: 0 0 0.5rem;font-size: 40Px;line-height: 1.2;} .test{}')
641
+ })
642
+ })
package/index.js CHANGED
@@ -42,15 +42,15 @@ 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
45
+ const transUnits = ['px']
46
+ const designWidth = input => typeof options.designWidth === 'function'
47
47
  ? options.designWidth(input)
48
48
  : options.designWidth
49
-
50
49
  switch (options.platform) {
51
50
  case 'h5': {
52
51
  options.rootValue = input => baseFontSize * designWidth(input) / 640
53
52
  targetUnit = 'rem'
53
+ transUnits.push('rpx')
54
54
  break
55
55
  }
56
56
  case 'rn': {
@@ -74,6 +74,7 @@ module.exports = postcss.plugin('postcss-pxtransform', function (options = {}) {
74
74
 
75
75
  const opts = Object.assign({}, defaults, options)
76
76
  const onePxTransform = typeof options.onePxTransform === 'undefined' ? true : options.onePxTransform
77
+ const pxRgx = pxRegex(transUnits)
77
78
 
78
79
  const satisfyPropList = createPropListMatcher(opts.propList)
79
80
 
@@ -157,10 +158,9 @@ module.exports = postcss.plugin('postcss-pxtransform', function (options = {}) {
157
158
 
158
159
  if (!satisfyPropList(decl.prop)) return
159
160
 
160
- if (blacklistedSelector(opts.selectorBlackList,
161
- decl.parent.selector)) return
161
+ if (blacklistedSelector(opts.selectorBlackList, decl.parent.selector)) return
162
162
 
163
- const value = decl.value.replace(pxRegex, pxReplace)
163
+ const value = decl.value.replace(pxRgx, pxReplace)
164
164
 
165
165
  // if rem unit already exists, do not add or replace
166
166
  if (declarationExists(decl.parent, decl.prop, value)) return
@@ -175,7 +175,7 @@ module.exports = postcss.plugin('postcss-pxtransform', function (options = {}) {
175
175
  if (opts.mediaQuery) {
176
176
  css.walkAtRules('media', function (rule) {
177
177
  if (rule.params.indexOf('px') === -1) return
178
- rule.params = rule.params.replace(pxRegex, pxReplace)
178
+ rule.params = rule.params.replace(pxRgx, pxReplace)
179
179
  })
180
180
  }
181
181
  }
@@ -8,4 +8,4 @@
8
8
  // Any digit followed by px
9
9
  // !singlequotes|!doublequotes|!url()|pixelunit
10
10
 
11
- module.exports = /"[^"]+"|'[^']+'|url\([^\)]+\)|(\d*\.?\d+)px/g
11
+ module.exports = (units = ['px']) => new RegExp(`"[^"]+"|'[^']+'|url\\([^\\)]+\\)|(\\d*\\.?\\d+)(${units.join('|')})`, 'g')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-pxtransform",
3
- "version": "3.5.0-beta.1",
3
+ "version": "3.5.0-beta.2",
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": "ba9d945ccebb86f06edb7f845110526d92dc7b00"
31
+ "gitHead": "c85adaa45959e6cafc15e6531f4735a7dfa2aab0"
32
32
  }