|
1 | 1 | import browserslist from 'browserslist';
|
2 |
| -import postcss from 'postcss'; |
3 |
| - |
4 |
| -export default postcss.plugin('postcss-browser-comments', opts => root => { |
5 |
| - // client browserslist |
6 |
| - const clientBrowserList = browserslist( |
7 |
| - Object(opts).browsers || null, |
8 |
| - { path: root.source && root.source.input && root.source.input.file } |
9 |
| - ); |
10 |
| - |
11 |
| - // root children references |
12 |
| - const references = root.nodes.slice(0); |
13 |
| - |
14 |
| - // for each child node of the root children references |
15 |
| - for (let node of references) { |
16 |
| - // if the node is a comment browser comment node |
17 |
| - if (isBrowserCommentNode(node)) { |
18 |
| - // rule following the browser comment |
19 |
| - const rule = node.next(); |
20 |
| - |
21 |
| - // browser data |
22 |
| - const browserdata = getBrowserData(node.text); |
23 |
| - |
24 |
| - if (browserdata.isNumbered) { |
25 |
| - rule.nodes.filter(isBrowserReferenceCommentNode).map( |
26 |
| - comment => { |
27 |
| - const browserdataIndex = parseFloat(comment.text) - 1; |
28 |
| - const browserslistPart = browserdata.browserslist[browserdataIndex]; |
29 | 2 |
|
| 3 | +const plugin = opts => { |
| 4 | + return { |
| 5 | + postcssPlugin: 'postcss-browser-comments', |
| 6 | + Once(root) { |
| 7 | + // client browserslist |
| 8 | + const clientBrowserList = browserslist( |
| 9 | + Object(opts).browsers || null, |
| 10 | + { path: root.source && root.source.input && root.source.input.file } |
| 11 | + ); |
| 12 | + |
| 13 | + // root children references |
| 14 | + const references = root.nodes.slice(0); |
| 15 | + |
| 16 | + // for each child node of the root children references |
| 17 | + for (let node of references) { |
| 18 | + // if the node is a comment browser comment node |
| 19 | + if (isBrowserCommentNode(node)) { |
| 20 | + // rule following the browser comment |
| 21 | + const rule = node.next(); |
| 22 | + |
| 23 | + // browser data |
| 24 | + const browserdata = getBrowserData(node.text); |
| 25 | + |
| 26 | + if (browserdata.isNumbered) { |
| 27 | + rule.nodes.filter(isBrowserReferenceCommentNode).map( |
| 28 | + comment => { |
| 29 | + const browserdataIndex = parseFloat(comment.text) - 1; |
| 30 | + const browserslistPart = browserdata.browserslist[browserdataIndex]; |
| 31 | + |
| 32 | + // whether to remove the rule if the comment browserslist does not match the client browserslist |
| 33 | + const removeRule = !clientBrowserList.some( |
| 34 | + clientBrowser => browserslist(browserslistPart).some( |
| 35 | + commentBrowser => commentBrowser === clientBrowser |
| 36 | + ) |
| 37 | + ); |
| 38 | + |
| 39 | + // conditionally remove the declaration and reference comment |
| 40 | + if (removeRule) { |
| 41 | + comment.prev().remove(); |
| 42 | + comment.remove(); |
| 43 | + } |
| 44 | + } |
| 45 | + ); |
| 46 | + |
| 47 | + // conditionally remove the empty rule and comment |
| 48 | + if (!rule.nodes.length) { |
| 49 | + rule.remove(); |
| 50 | + node.remove(); |
| 51 | + } |
| 52 | + } else { |
30 | 53 | // whether to remove the rule if the comment browserslist does not match the client browserslist
|
31 | 54 | const removeRule = !clientBrowserList.some(
|
32 |
| - clientBrowser => browserslist(browserslistPart).some( |
| 55 | + clientBrowser => browserslist(browserdata.browserslist).some( |
33 | 56 | commentBrowser => commentBrowser === clientBrowser
|
34 | 57 | )
|
35 | 58 | );
|
36 | 59 |
|
37 |
| - // conditionally remove the declaration and reference comment |
| 60 | + // conditionally remove the rule and comment |
38 | 61 | if (removeRule) {
|
39 |
| - comment.prev().remove(); |
40 |
| - comment.remove(); |
| 62 | + rule.remove(); |
| 63 | + node.remove(); |
41 | 64 | }
|
42 | 65 | }
|
43 |
| - ); |
44 |
| - |
45 |
| - // conditionally remove the empty rule and comment |
46 |
| - if (!rule.nodes.length) { |
47 |
| - rule.remove(); |
48 |
| - node.remove(); |
49 |
| - } |
50 |
| - } else { |
51 |
| - // whether to remove the rule if the comment browserslist does not match the client browserslist |
52 |
| - const removeRule = !clientBrowserList.some( |
53 |
| - clientBrowser => browserslist(browserdata.browserslist).some( |
54 |
| - commentBrowser => commentBrowser === clientBrowser |
55 |
| - ) |
56 |
| - ); |
57 |
| - |
58 |
| - // conditionally remove the rule and comment |
59 |
| - if (removeRule) { |
60 |
| - rule.remove(); |
61 |
| - node.remove(); |
62 | 66 | }
|
63 | 67 | }
|
64 | 68 | }
|
65 | 69 | }
|
66 |
| -}); |
| 70 | +}; |
| 71 | + |
| 72 | +plugin.postcss = true; |
| 73 | + |
| 74 | +export default plugin; |
67 | 75 |
|
68 | 76 | // returns whether a node is a browser comment
|
69 | 77 | const isBrowserCommentNode = node => node.type === 'comment' && isBrowserCommentNodeRegExp.test(node.text) && node.next().type === 'rule';
|
|
0 commit comments