-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
36 lines (34 loc) · 1.04 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
const { getRulesMatcher, getReset, createResetRule } = require("./lib");
function contains(array, item) {
return array.indexOf(item) !== -1;
}
module.exports = (opts = {}) => {
opts.rulesMatcher = opts.rulesMatcher || "bem";
opts.reset = opts.reset || "initial";
const rulesMatcher = getRulesMatcher(opts.rulesMatcher);
const reset = getReset(opts.reset);
return {
postcssPlugin: "postcss-autoreset",
prepare() {
return {
OnceExit(root) {
const matchedSelectors = [];
root.walkRules(rule => {
const { selector } = rule;
if (/^(-(webkit|moz|ms|o)-)?keyframes$/.test(rule.parent.name)) {
return;
}
if (!contains(matchedSelectors, selector) && rulesMatcher(rule)) {
matchedSelectors.push(selector);
}
});
if (!matchedSelectors.length) {
return;
}
root.prepend(createResetRule(matchedSelectors, reset));
},
};
},
};
};
module.exports.postcss = true;