You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
8.4 KiB

2 years ago
{"version":3,"file":"index.mjs","sources":["index.js"],"sourcesContent":["import browserslist from 'browserslist'\n\nconst plugin = opts => {\n\treturn {\n\t\tpostcssPlugin: 'postcss-browser-comments',\n\t\tOnce(root) {\n\t\t\t// client browserslist\n\t\t\tconst clientBrowserList = browserslist(\n\t\t\t\tObject(opts).browsers || null,\n\t\t\t\t{ path: root.source && root.source.input && root.source.input.file }\n\t\t\t)\n\n\t\t\t// root children references\n\t\t\tconst references = root.nodes.slice(0)\n\n\t\t\t// for each child node of the root children references\n\t\t\tfor (const node of references) {\n\t\t\t\t// if the node is a comment browser comment node\n\t\t\t\tif (isBrowserCommentNode(node)) {\n\t\t\t\t\t// rule following the browser comment\n\t\t\t\t\tconst rule = node.next()\n\n\t\t\t\t\t// browser data\n\t\t\t\t\tconst browserdata = getBrowserData(node.text)\n\n\t\t\t\t\tif (browserdata.isNumbered) {\n\t\t\t\t\t\trule.nodes.filter(isBrowserReferenceCommentNode).map(\n\t\t\t\t\t\t\tcomment => {\n\t\t\t\t\t\t\t\tconst browserdataIndex = parseFloat(comment.text) - 1\n\t\t\t\t\t\t\t\tconst browserslistPart = browserdata.browserslist[browserdataIndex]\n\n\t\t\t\t\t\t\t\t// whether to remove the rule if the comment browserslist does not match the client browserslist\n\t\t\t\t\t\t\t\tconst removeRule = !clientBrowserList.some(\n\t\t\t\t\t\t\t\t\tclientBrowser => browserslist(browserslistPart).some(\n\t\t\t\t\t\t\t\t\t\tcommentBrowser => commentBrowser === clientBrowser\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t)\n\n\t\t\t\t\t\t\t\t// conditionally remove the declaration and reference comment\n\t\t\t\t\t\t\t\tif (removeRule) {\n\t\t\t\t\t\t\t\t\tcomment.prev().remove()\n\t\t\t\t\t\t\t\t\tcomment.remove()\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t)\n\n\t\t\t\t\t\t// conditionally remove the empty rule and comment\n\t\t\t\t\t\tif (!rule.nodes.length) {\n\t\t\t\t\t\t\trule.remove()\n\t\t\t\t\t\t\tnode.remove()\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// whether to remove the rule if the comment browserslist does not match the client browserslist\n\t\t\t\t\t\tconst removeRule = !clientBrowserList.some(\n\t\t\t\t\t\t\tclientBrowser => browserslist(browserdata.browserslist).some(\n\t\t\t\t\t\t\t\tcommentBrowser => commentBrowser === clientBrowser\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t)\n\n\t\t\t\t\t\t// conditionally remove the rule and comment\n\t\t\t\t\t\tif (removeRule) {\n\t\t\t\t\t\t\trule.remove()\n\t\t\t\t\t\t\tnode.remove()\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nplugin.postcss = true\n\nexport default plugin\n\n// returns whether a node is a browser comment\nconst isBrowserCommentNode = node => node.type === 'comment' && isBrowserCommentNodeRegExp.test(node.text) && node.next().type === 'rule'\nconst isBrowserCommentNodeRegExp = /^\\*\\n * /\n\n// returns whether a node is a browser reference comment\nconst isBrowserReferenceCommentNode = node => node.type === 'comment' && isBrowserReferenceCommentNodeRegExp.test(node.text)\nconst isBrowserReferenceCommentNodeRegExp = /^\\d+$/\n\n// returns browser data from comment text\nconst getBrowserData = text => {\n\tconst browserDataNumbered = text.match(browserDataMutliRegExp)\n\tconst isNumbered = Boolean(browserDataNumbered)\n\n\treturn {\n\t\tbrowserslist: isNumbered\n\t\t\t? browserDataNumbered.map(\n\t\t\t\tbrowserslistPart => getBrowsersList(browserslistPart.replace(browserDataNumberedNewlineRegExp, '$1'))\n\t\t\t)\n\t\t: getBrowsersList(\n\t\t\ttext.replace(browserDataNewlineRegExp, '')\n\t\t),\n\t\tisNumbered\n\t}\n}\nconst browserDataMutliRegExp = /(\\n \\* \\d+\\. (?:[^\\n]+|\\n \\* {4,})+)/g\nconst browserDataNewlineRegExp = /^\\*\\n \\* ?|\\n \\*/g\nconst browserDataNumberedNewlineRegExp = /\\n \\* (?:( )\\s*)?/g\n\n// returns a browserlist from comment text\nconst getBrowsersList = text => text.split(getBrowsersListInSplitRegExp).slice(1).map(\n\tpart => part.split(getBrowsersListAndSplitRegExp).filter(part2 => part2)\n).reduce(\n\t(acc, val) => acc.concat(val), [])\n.map(\n\tpart => part.replace(\n\t\tgetBrowsersListQueryRegExp,\