UNPKG

807 BJavaScriptView Raw
1'use strict';
2const plugin = 'postcss-discard-empty';
3
4function 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
31function pluginCreator() {
32 return {
33 postcssPlugin: plugin,
34 OnceExit(css, { result }) {
35 discardAndReport(css, result);
36 },
37 };
38}
39
40pluginCreator.postcss = true;
41module.exports = pluginCreator;