forked from jesstech/postcss-rem-to-pixel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
130 lines (116 loc) · 3.79 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const remRegex = require("./lib/rem-unit-regex");
const filterPropList = require("./lib/filter-prop-list");
const defaults = {
rootValue: 16,
unitPrecision: 5,
selectorBlackList: [],
propList: ["font", "font-size", "line-height", "letter-spacing"],
replace: true,
mediaQuery: false,
minRemValue: 0,
};
module.exports = (options = {}) => {
const opts = Object.assign({}, defaults, options);
const satisfyPropList = createPropListMatcher(opts.propList);
let remReplace;
return {
postcssPlugin: "postcss-rem-to-pixel-next",
Once() {
remReplace = createRemReplace(
opts.rootValue,
opts.unitPrecision,
opts.minRemValue,
);
},
Declaration(decl) {
// This should be the fastest test and will remove most declarations
if (decl.value.indexOf("rem") === -1) return;
if (!satisfyPropList(decl.prop)) return;
if (blacklistedSelector(opts.selectorBlackList, decl.parent.selector))
return;
const value = decl.value.replace(remRegex, remReplace);
// if px unit already exists, do not add or replace
if (declarationExists(decl.parent, decl.prop, value)) return;
if (opts.replace) {
decl.value = value;
} else {
decl.cloneAfter({ value: value });
}
},
AtRule(atRule) {
if (opts.mediaQuery && atRule.name === "media") {
if (atRule.params.indexOf("rem") === -1) return;
atRule.params = atRule.params.replace(remRegex, remReplace);
}
},
};
};
module.exports.postcss = true;
function createRemReplace(rootValue, unitPrecision, minRemValue) {
return function (m, $1) {
if (!$1) return m;
const rems = parseFloat($1);
if (rems < minRemValue) return m;
const fixedVal = toFixed(rems * rootValue, unitPrecision);
return fixedVal === 0 ? "0" : fixedVal + "px";
};
}
function toFixed(number, precision) {
const multiplier = Math.pow(10, precision + 1),
wholeNumber = Math.floor(number * multiplier);
return (Math.round(wholeNumber / 10) * 10) / multiplier;
}
function declarationExists(decls, prop, value) {
return decls.some(function (decl) {
return decl.prop === prop && decl.value === value;
});
}
function blacklistedSelector(blacklist, selector) {
if (typeof selector !== "string") return;
return blacklist.some(function (regex) {
if (typeof regex === "string") return selector.indexOf(regex) !== -1;
return selector.match(regex);
});
}
function createPropListMatcher(propList) {
const hasWild = propList.indexOf("*") > -1;
const matchAll = hasWild && propList.length === 1;
const lists = {
exact: filterPropList.exact(propList),
contain: filterPropList.contain(propList),
startWith: filterPropList.startWith(propList),
endWith: filterPropList.endWith(propList),
notExact: filterPropList.notExact(propList),
notContain: filterPropList.notContain(propList),
notStartWith: filterPropList.notStartWith(propList),
notEndWith: filterPropList.notEndWith(propList),
};
return function (prop) {
if (matchAll) return true;
return (
(hasWild ||
lists.exact.indexOf(prop) > -1 ||
lists.contain.some(function (m) {
return prop.indexOf(m) > -1;
}) ||
lists.startWith.some(function (m) {
return prop.indexOf(m) === 0;
}) ||
lists.endWith.some(function (m) {
return prop.indexOf(m) === prop.length - m.length;
})) &&
!(
lists.notExact.indexOf(prop) > -1 ||
lists.notContain.some(function (m) {
return prop.indexOf(m) > -1;
}) ||
lists.notStartWith.some(function (m) {
return prop.indexOf(m) === 0;
}) ||
lists.notEndWith.some(function (m) {
return prop.indexOf(m) === prop.length - m.length;
})
)
);
};
}