-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
51 lines (45 loc) · 1.33 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
const postcss = require("postcss");
const IMPORTANT = '!important'
module.exports = postcss.plugin("postcss-remove-declaration", function(options) {
options = options || {};
const { remove } = options;
return css => {
css.walkRules(rule => {
let toRemove = remove[removeLineBreaks(rule.selector)];
if (toRemove) {
if (typeof toRemove === "string") {
if (toRemove === "*") {
rule.remove();
return;
} else {
toRemove = [toRemove];
}
}
if (Array.isArray(toRemove)) {
rule.walkDecls(decl => {
if (toRemove.includes(decl.prop)) {
decl.remove();
}
});
} else if (typeof toRemove === "object") {
rule.walkDecls(decl => {
if (decl.prop in toRemove) {
let value = toRemove[decl.prop]
const hasImportant = value.endsWith(IMPORTANT)
if (hasImportant) {
value = value.slice(0, -IMPORTANT.length).trim()
}
if (decl.value === value) {
if (decl.important && !hasImportant) return
decl.remove()
}
}
});
}
}
});
};
});
function removeLineBreaks(string) {
return string.replace(/(\r\n|\n|\r)/gm, "");
}