Skip to content

Commit 56612bf

Browse files
yutingzhao1991tingzhao.ytz
andauthored
feat: Support custom convert px unit (#105)
* feat: Support custom convert px unit * chore: revert format --------- Co-authored-by: tingzhao.ytz <tingzhao.ytz@antgroup.com>
1 parent 1226490 commit 56612bf

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Default:
9696
- If value is function, you can use exclude function to return a true and the file will be ignored.
9797
- the callback will pass the file path as a parameter, it should returns a Boolean result.
9898
- `function (file) { return file.indexOf('exclude') !== -1; }`
99+
- `unit` (String) Set the default unit to convert, default is `px`.
99100

100101
### Use with gulp-postcss and autoprefixer
101102

index.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const defaults = {
1010
replace: true,
1111
mediaQuery: false,
1212
minPixelValue: 0,
13-
exclude: null
13+
exclude: null,
14+
unit: "px",
1415
};
1516

1617
const legacyOptions = {
@@ -19,7 +20,7 @@ const legacyOptions = {
1920
selector_black_list: "selectorBlackList",
2021
prop_white_list: "propList",
2122
media_query: "mediaQuery",
22-
propWhiteList: "propList"
23+
propWhiteList: "propList",
2324
};
2425

2526
function convertLegacyOptions(options) {
@@ -35,7 +36,7 @@ function convertLegacyOptions(options) {
3536
delete options["prop_white_list"];
3637
delete options.propWhiteList;
3738
}
38-
Object.keys(legacyOptions).forEach(key => {
39+
Object.keys(legacyOptions).forEach((key) => {
3940
if (Reflect.has(options, key)) {
4041
options[legacyOptions[key]] = options[key];
4142
delete options[key];
@@ -60,12 +61,12 @@ function toFixed(number, precision) {
6061
}
6162

6263
function declarationExists(decls, prop, value) {
63-
return decls.some(decl => decl.prop === prop && decl.value === value);
64+
return decls.some((decl) => decl.prop === prop && decl.value === value);
6465
}
6566

6667
function blacklistedSelector(blacklist, selector) {
6768
if (typeof selector !== "string") return;
68-
return blacklist.some(regex => {
69+
return blacklist.some((regex) => {
6970
if (typeof regex === "string") {
7071
return selector.indexOf(regex) !== -1;
7172
}
@@ -84,31 +85,31 @@ function createPropListMatcher(propList) {
8485
notExact: filterPropList.notExact(propList),
8586
notContain: filterPropList.notContain(propList),
8687
notStartWith: filterPropList.notStartWith(propList),
87-
notEndWith: filterPropList.notEndWith(propList)
88+
notEndWith: filterPropList.notEndWith(propList),
8889
};
89-
return prop => {
90+
return (prop) => {
9091
if (matchAll) return true;
9192
return (
9293
(hasWild ||
9394
lists.exact.indexOf(prop) > -1 ||
94-
lists.contain.some(function(m) {
95+
lists.contain.some(function (m) {
9596
return prop.indexOf(m) > -1;
9697
}) ||
97-
lists.startWith.some(function(m) {
98+
lists.startWith.some(function (m) {
9899
return prop.indexOf(m) === 0;
99100
}) ||
100-
lists.endWith.some(function(m) {
101+
lists.endWith.some(function (m) {
101102
return prop.indexOf(m) === prop.length - m.length;
102103
})) &&
103104
!(
104105
lists.notExact.indexOf(prop) > -1 ||
105-
lists.notContain.some(function(m) {
106+
lists.notContain.some(function (m) {
106107
return prop.indexOf(m) > -1;
107108
}) ||
108-
lists.notStartWith.some(function(m) {
109+
lists.notStartWith.some(function (m) {
109110
return prop.indexOf(m) === 0;
110111
}) ||
111-
lists.notEndWith.some(function(m) {
112+
lists.notEndWith.some(function (m) {
112113
return prop.indexOf(m) === prop.length - m.length;
113114
})
114115
)
@@ -145,20 +146,20 @@ module.exports = (options = {}) => {
145146
pxReplace = createPxReplace(
146147
rootValue,
147148
opts.unitPrecision,
148-
opts.minPixelValue
149+
opts.minPixelValue,
149150
);
150151
},
151152
Declaration(decl) {
152153
if (isExcludeFile) return;
153154

154155
if (
155-
decl.value.indexOf("px") === -1 ||
156+
decl.value.indexOf(opts.unit) === -1 ||
156157
!satisfyPropList(decl.prop) ||
157158
blacklistedSelector(opts.selectorBlackList, decl.parent.selector)
158159
)
159160
return;
160161

161-
const value = decl.value.replace(pxRegex, pxReplace);
162+
const value = decl.value.replace(pxRegex(opts.unit), pxReplace);
162163

163164
// if rem unit already exists, do not add or replace
164165
if (declarationExists(decl.parent, decl.prop, value)) return;
@@ -173,10 +174,10 @@ module.exports = (options = {}) => {
173174
if (isExcludeFile) return;
174175

175176
if (opts.mediaQuery && atRule.name === "media") {
176-
if (atRule.params.indexOf("px") === -1) return;
177-
atRule.params = atRule.params.replace(pxRegex, pxReplace);
177+
if (atRule.params.indexOf(opts.unit) === -1) return;
178+
atRule.params = atRule.params.replace(pxRegex(opts.unit), pxReplace);
178179
}
179-
}
180+
},
180181
};
181182
};
182183
module.exports.postcss = true;

lib/pixel-unit-regex.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
// Any digit followed by px
77
// !singlequotes|!doublequotes|!url()|pixelunit
88

9-
module.exports = /"[^"]+"|'[^']+'|url\([^)]+\)|var\([^)]+\)|(\d*\.?\d+)px/g;
9+
module.exports = (unit) =>
10+
new RegExp(
11+
`"[^"]+"|'[^']+'|url\\([^)]+\\)|var\\([^)]+\\)|(\\d*\\.?\\d+)${unit}`,
12+
'g',
13+
);

spec/pxtorem-spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,4 +540,18 @@ describe("exclude", function() {
540540
}).css;
541541
expect(processed).toBe(basicCSS);
542542
});
543+
544+
it("should only replace properties in the prop list with wildcard", function() {
545+
var input =
546+
"h1 { margin: 0 0 20rpx; font-size: 32rpx; line-height: 1.2; letter-spacing: 1rpx; width: 30px;}";
547+
var output =
548+
"h1 { margin: 0 0 1.25rem; font-size: 2rem; line-height: 1.2; letter-spacing: 0.0625rem; width: 30px;}";
549+
var options = {
550+
unit: "rpx",
551+
propList: ["*"]
552+
};
553+
var processed = postcss(pxtorem(options)).process(input).css;
554+
555+
expect(processed).toBe(output);
556+
});
543557
});

0 commit comments

Comments
 (0)