-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
112 lines (107 loc) · 3.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const pxUnitReg = /"[^"]+"|'[^']+'|url\([^\)]+\)|(\d*\.?\d+)px/g;
function toFixed(number, precision) {
let multiplier = Math.pow(10, precision + 1);
let wholeNumber = Math.floor(number * multiplier);
return (Math.round(wholeNumber / 10) * 10) / multiplier;
}
function isExcludeFile(path, excludeFiles) {
return excludeFiles.some((rule) => path.match(rule));
}
function isExcludeSelector(selector, excludeSelectors) {
return excludeSelectors.some((rule) => {
if (typeof rule === "string") {
return selector.indexOf(rule) !== -1;
}
return selector.match(rule);
});
}
function isExcludeProperty(property, excludeProperties) {
return excludeProperties.some((rule) => {
if (typeof rule === "string") {
return property.indexOf(rule) !== -1;
}
return property.match(rule);
});
}
module.exports = (options) => {
options = options || {};
options = Object.assign(
{
targetUnit: "vw",
ignoreThreshold: 1,
viewportWidth: 375,
viewportHeight: 667,
htmlFontSize: 37.5,
unitPrecision: 5,
excludeFiles: [],
excludeSelectors: [],
excludeProperties: [],
},
options
);
return {
postcssPlugin: "postcss-px-to-relative-unit",
Once(root) {
if (isExcludeFile(root.source.input.file, options.excludeFiles)) {
return;
}
root.walkRules((rule) => {
if (isExcludeSelector(rule.selector, options.excludeSelectors)) {
return;
}
rule.walkDecls((decl) => {
if (isExcludeProperty(decl.prop, options.excludeProperties)) {
return;
}
let remValue = decl.value;
let vwValue = decl.value;
let hasChange = false;
remValue = remValue.replace(pxUnitReg, (match, pxValue) => {
if (!pxValue) {
return match;
}
let pixelValue = parseFloat(pxValue);
if (pixelValue <= options.ignoreThreshold) {
return match;
}
let remTargetValue = toFixed(
pixelValue / options.htmlFontSize,
options.unitPrecision
);
return `${remTargetValue}rem`;
});
vwValue = vwValue.replace(pxUnitReg, (match, pxValue) => {
if (!pxValue) {
return match;
}
let pixelValue = parseFloat(pxValue);
if (pixelValue <= options.ignoreThreshold) {
return match;
}
hasChange = true;
let vwTargetValue = toFixed(
(pixelValue / options.viewportWidth) * 100,
options.unitPrecision
);
return `${vwTargetValue}vw`;
});
if (!hasChange) {
return;
}
if (options.targetUnit === "vw") {
decl.value = vwValue;
} else if (options.targetUnit === "rem") {
decl.value = remValue;
} else if (options.targetUnit === "vw&rem") {
decl.value = remValue;
decl.after({
prop: decl.prop,
value: vwValue,
});
}
});
});
},
};
};
module.exports.postcss = true;