-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcombine-at-rules.cjs
61 lines (53 loc) · 1.55 KB
/
combine-at-rules.cjs
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
/**
* @file
* PostCSS plugin to combine multiple at-rules into a single at-rule.
*
* This will go through each at-rule, save each as a key in an object, and then combine all of the at-rules into a single at-rule.
*/
const plugin = (options = {}) => {
const atRules = {};
const atRulesToProcess = options.atRules || [];
return {
postcssPlugin: "combine-at-rules",
AtRule(atRule) {
if (atRule.processed) {
return;
}
if (
(atRulesToProcess.length < 1 ||
atRulesToProcess.includes(atRule.name)) &&
atRule.nodes &&
atRule.nodes.length > 0
) {
// Save the at-rule for later
if (atRules[atRule.name]) {
if (atRules[atRule.name][atRule.params]) {
atRules[atRule.name][atRule.params].append(atRule.nodes);
} else {
atRules[atRule.name][atRule.params] = atRule.clone();
}
} else {
atRules[atRule.name] = {
[atRule.params]: atRule.clone(),
};
}
// Remove the at-rule from the tree
atRule.processed = true;
atRule.remove();
return;
}
},
Root(root) {
// Combine all of the at-rules into a single at-rule
// Only do this for rules found in the current file.
for (const atRuleName in atRules) {
for (const atRuleParams in atRules[atRuleName]) {
const atRule = atRules[atRuleName][atRuleParams];
root.append(atRule);
}
}
},
};
};
plugin.postcss = true;
module.exports = plugin;