source: frontend/node_modules/postcss-browser-comments/index.mjs

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.9 KB
Line 
1import browserslist from 'browserslist';
2
3const plugin = opts => {
4 return {
5 postcssPlugin: 'postcss-browser-comments',
6
7 Once(root) {
8 // client browserslist
9 const clientBrowserList = browserslist(Object(opts).browsers || null, {
10 path: root.source && root.source.input && root.source.input.file
11 }); // root children references
12
13 const references = root.nodes.slice(0); // for each child node of the root children references
14
15 for (const 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(); // browser data
20
21 const browserdata = getBrowserData(node.text);
22
23 if (browserdata.isNumbered) {
24 rule.nodes.filter(isBrowserReferenceCommentNode).map(comment => {
25 const browserdataIndex = parseFloat(comment.text) - 1;
26 const browserslistPart = browserdata.browserslist[browserdataIndex]; // whether to remove the rule if the comment browserslist does not match the client browserslist
27
28 const removeRule = !clientBrowserList.some(clientBrowser => browserslist(browserslistPart).some(commentBrowser => commentBrowser === clientBrowser)); // conditionally remove the declaration and reference comment
29
30 if (removeRule) {
31 comment.prev().remove();
32 comment.remove();
33 }
34 }); // conditionally remove the empty rule and comment
35
36 if (!rule.nodes.length) {
37 rule.remove();
38 node.remove();
39 }
40 } else {
41 // whether to remove the rule if the comment browserslist does not match the client browserslist
42 const removeRule = !clientBrowserList.some(clientBrowser => browserslist(browserdata.browserslist).some(commentBrowser => commentBrowser === clientBrowser)); // conditionally remove the rule and comment
43
44 if (removeRule) {
45 rule.remove();
46 node.remove();
47 }
48 }
49 }
50 }
51 }
52
53 };
54};
55
56plugin.postcss = true;
57
58const isBrowserCommentNode = node => node.type === 'comment' && isBrowserCommentNodeRegExp.test(node.text) && node.next().type === 'rule';
59
60const isBrowserCommentNodeRegExp = /^\*\n * /; // returns whether a node is a browser reference comment
61
62const isBrowserReferenceCommentNode = node => node.type === 'comment' && isBrowserReferenceCommentNodeRegExp.test(node.text);
63
64const isBrowserReferenceCommentNodeRegExp = /^\d+$/; // returns browser data from comment text
65
66const getBrowserData = text => {
67 const browserDataNumbered = text.match(browserDataMutliRegExp);
68 const isNumbered = Boolean(browserDataNumbered);
69 return {
70 browserslist: isNumbered ? browserDataNumbered.map(browserslistPart => getBrowsersList(browserslistPart.replace(browserDataNumberedNewlineRegExp, '$1'))) : getBrowsersList(text.replace(browserDataNewlineRegExp, '')),
71 isNumbered
72 };
73};
74
75const browserDataMutliRegExp = /(\n \* \d+\. (?:[^\n]+|\n \* {4,})+)/g;
76const browserDataNewlineRegExp = /^\*\n \* ?|\n \*/g;
77const browserDataNumberedNewlineRegExp = /\n \* (?:( )\s*)?/g; // returns a browserlist from comment text
78
79const getBrowsersList = text => text.split(getBrowsersListInSplitRegExp).slice(1).map(part => part.split(getBrowsersListAndSplitRegExp).filter(part2 => part2)).reduce((acc, val) => acc.concat(val), []).map(part => part.replace(getBrowsersListQueryRegExp, ($0, browser, query) => browser === 'all' ? '> 0%' : `${browser}${query ? /^((?:\d*\.)?\d+)-$/.test(query) ? ` <= ${query.slice(0, -1)}` : ` ${query}` : ' > 0'}`).toLowerCase());
80
81const getBrowsersListInSplitRegExp = /\s+in\s+/;
82const getBrowsersListAndSplitRegExp = /(?: and|, and|,)/;
83const getBrowsersListQueryRegExp = /^\s*(\w+)(?: ((?:(?:\d*\.)?\d+-)?(?:\d*\.)?\d+[+-]?))?.*$/;
84
85export default plugin;
86//# sourceMappingURL=index.mjs.map
Note: See TracBrowser for help on using the repository browser.