1 | 'use strict';
|
2 | const plugin = 'postcss-discard-empty';
|
3 |
|
4 | function discardAndReport(css, result) {
|
5 | function discardEmpty(node) {
|
6 | const { type, nodes: sub, params } = node;
|
7 |
|
8 | if (sub) {
|
9 | node.each(discardEmpty);
|
10 | }
|
11 |
|
12 | if (
|
13 | (type === 'decl' && !node.value) ||
|
14 | (type === 'rule' && !node.selector) ||
|
15 | (sub && !sub.length) ||
|
16 | (type === 'atrule' && ((!sub && !params) || (!params && !sub.length)))
|
17 | ) {
|
18 | node.remove();
|
19 |
|
20 | result.messages.push({
|
21 | type: 'removal',
|
22 | plugin,
|
23 | node,
|
24 | });
|
25 | }
|
26 | }
|
27 |
|
28 | css.each(discardEmpty);
|
29 | }
|
30 |
|
31 | function pluginCreator() {
|
32 | return {
|
33 | postcssPlugin: plugin,
|
34 | OnceExit(css, { result }) {
|
35 | discardAndReport(css, result);
|
36 | },
|
37 | };
|
38 | }
|
39 |
|
40 | pluginCreator.postcss = true;
|
41 | module.exports = pluginCreator;
|