postcss-pxtransform 3.5.11 → 3.6.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/index.js +69 -51
- package/lib/pixel-upper-unit-regex.js +11 -0
- package/package.json +8 -5
package/index.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const postcss = require('postcss')
|
|
4
1
|
const pxRegex = require('./lib/pixel-unit-regex')
|
|
2
|
+
const PXRegex = require('./lib/pixel-upper-unit-regex')
|
|
5
3
|
const filterPropList = require('./lib/filter-prop-list')
|
|
6
4
|
|
|
7
5
|
const defaults = {
|
|
@@ -37,7 +35,7 @@ const DEFAULT_WEAPP_OPTIONS = {
|
|
|
37
35
|
|
|
38
36
|
let targetUnit
|
|
39
37
|
|
|
40
|
-
module.exports =
|
|
38
|
+
module.exports = (options = {}) => {
|
|
41
39
|
options = Object.assign({}, DEFAULT_WEAPP_OPTIONS, options)
|
|
42
40
|
|
|
43
41
|
const transUnits = ['px']
|
|
@@ -62,6 +60,11 @@ module.exports = postcss.plugin('postcss-pxtransform', function (options = {}) {
|
|
|
62
60
|
targetUnit = 'px'
|
|
63
61
|
break
|
|
64
62
|
}
|
|
63
|
+
case 'harmony': {
|
|
64
|
+
options.rootValue = input => 1 / options.deviceRatio[designWidth(input)]
|
|
65
|
+
targetUnit = 'px'
|
|
66
|
+
break
|
|
67
|
+
}
|
|
65
68
|
default: {
|
|
66
69
|
// mini-program
|
|
67
70
|
options.rootValue = input => 1 / options.deviceRatio[designWidth(input)]
|
|
@@ -77,26 +80,20 @@ module.exports = postcss.plugin('postcss-pxtransform', function (options = {}) {
|
|
|
77
80
|
|
|
78
81
|
const satisfyPropList = createPropListMatcher(opts.propList)
|
|
79
82
|
|
|
80
|
-
return
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (css.nodes[i].text === 'postcss-pxtransform disable') {
|
|
86
|
-
return
|
|
87
|
-
} else {
|
|
88
|
-
break
|
|
89
|
-
}
|
|
83
|
+
return {
|
|
84
|
+
postcssPlugin: 'postcss-pxtransform',
|
|
85
|
+
Comment (comment) {
|
|
86
|
+
if (comment.text === 'postcss-pxtransform disable') {
|
|
87
|
+
return
|
|
90
88
|
}
|
|
91
|
-
}
|
|
92
89
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
90
|
+
// delete code between comment in RN
|
|
91
|
+
// 有死循环的问题
|
|
92
|
+
if (options.platform === 'rn') {
|
|
96
93
|
if (comment.text === 'postcss-pxtransform rn eject enable') {
|
|
97
94
|
let next = comment.next()
|
|
98
95
|
while (next) {
|
|
99
|
-
if (next.
|
|
96
|
+
if (next.text === 'postcss-pxtransform rn eject disable') {
|
|
100
97
|
break
|
|
101
98
|
}
|
|
102
99
|
const temp = next.next()
|
|
@@ -104,13 +101,11 @@ module.exports = postcss.plugin('postcss-pxtransform', function (options = {}) {
|
|
|
104
101
|
next = temp
|
|
105
102
|
}
|
|
106
103
|
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
104
|
+
}
|
|
109
105
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
css.walkComments(comment => {
|
|
106
|
+
/* #ifdef %PLATFORM%
|
|
107
|
+
* 平台特有样式
|
|
108
|
+
* #endif */
|
|
114
109
|
const wordList = comment.text.split(' ')
|
|
115
110
|
// 指定平台保留
|
|
116
111
|
if (wordList.indexOf('#ifdef') > -1) {
|
|
@@ -127,13 +122,10 @@ module.exports = postcss.plugin('postcss-pxtransform', function (options = {}) {
|
|
|
127
122
|
}
|
|
128
123
|
}
|
|
129
124
|
}
|
|
130
|
-
})
|
|
131
125
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
css.walkComments(comment => {
|
|
136
|
-
const wordList = comment.text.split(' ')
|
|
126
|
+
/* #ifdef %PLATFORM%
|
|
127
|
+
* 平台特有样式
|
|
128
|
+
* #endif */
|
|
137
129
|
// 指定平台剔除
|
|
138
130
|
if (wordList.indexOf('#ifndef') > -1) {
|
|
139
131
|
// 指定平台
|
|
@@ -149,36 +141,60 @@ module.exports = postcss.plugin('postcss-pxtransform', function (options = {}) {
|
|
|
149
141
|
}
|
|
150
142
|
}
|
|
151
143
|
}
|
|
152
|
-
}
|
|
144
|
+
},
|
|
153
145
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
+
},
|
|
157
155
|
|
|
158
|
-
|
|
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
|
+
},
|
|
159
165
|
|
|
160
|
-
|
|
166
|
+
Once (root) {
|
|
167
|
+
const pxReplace = createPxReplace(opts.rootValue, opts.unitPrecision, opts.minPixelValue, onePxTransform)(root.source.input)
|
|
161
168
|
|
|
162
|
-
|
|
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
|
|
163
172
|
|
|
164
|
-
|
|
165
|
-
if (declarationExists(decl.parent, decl.prop, value)) return
|
|
173
|
+
if (!satisfyPropList(decl.prop)) return
|
|
166
174
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
175
|
+
if (blacklistedSelector(opts.selectorBlackList, decl.parent.selector)) return
|
|
176
|
+
|
|
177
|
+
const value = decl.value.replace(pxRgx, pxReplace)
|
|
178
|
+
|
|
179
|
+
// if rem unit already exists, do not add or replace
|
|
180
|
+
if (declarationExists(decl.parent, decl.prop, value)) return
|
|
173
181
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
182
|
+
if (opts.replace) {
|
|
183
|
+
decl.value = value
|
|
184
|
+
} else {
|
|
185
|
+
decl.parent.insertAfter(i, decl.clone({ value: value }))
|
|
186
|
+
}
|
|
178
187
|
})
|
|
188
|
+
|
|
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
|
+
})
|
|
194
|
+
}
|
|
179
195
|
}
|
|
180
196
|
}
|
|
181
|
-
}
|
|
197
|
+
}
|
|
182
198
|
|
|
183
199
|
function convertLegacyOptions (options) {
|
|
184
200
|
if (typeof options !== 'object') return
|
|
@@ -284,3 +300,5 @@ function createPropListMatcher (propList) {
|
|
|
284
300
|
)
|
|
285
301
|
}
|
|
286
302
|
}
|
|
303
|
+
|
|
304
|
+
module.exports.postcss = true
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/*eslint-disable*/
|
|
2
|
+
|
|
3
|
+
// excluding regex trick: http://www.rexegg.com/regex-best-trick.html
|
|
4
|
+
|
|
5
|
+
// Not anything inside double quotes
|
|
6
|
+
// Not anything inside single quotes
|
|
7
|
+
// Not anything inside url()
|
|
8
|
+
// Any digit followed by PX
|
|
9
|
+
// !singlequotes|!doublequotes|!url()|pixelunit
|
|
10
|
+
|
|
11
|
+
module.exports = /"[^"]+"|'[^']+'|url\([^\)]+\)|(\d*\.?\d+)(PX)/g
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-pxtransform",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0-alpha.1",
|
|
4
4
|
"description": "PostCSS plugin px 转小程序 rpx及h5 rem 单位",
|
|
5
|
+
"main": "index.js",
|
|
5
6
|
"keywords": [
|
|
6
7
|
"postcss",
|
|
7
8
|
"css",
|
|
@@ -18,13 +19,15 @@
|
|
|
18
19
|
"url": "https://github.com/NervJS/taro/issues"
|
|
19
20
|
},
|
|
20
21
|
"homepage": "https://github.com/NervJS/taro#readme",
|
|
21
|
-
"dependencies": {
|
|
22
|
-
"postcss": "^6.0.22"
|
|
23
|
-
},
|
|
24
22
|
"jest": {
|
|
25
23
|
"testEnvironment": "node"
|
|
26
24
|
},
|
|
27
|
-
"
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"postcss": "^8.4.18"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"postcss": "^8.4.18"
|
|
30
|
+
},
|
|
28
31
|
"scripts": {
|
|
29
32
|
"test": "jest"
|
|
30
33
|
}
|