1 line
8.4 KiB
Plaintext
1 line
8.4 KiB
Plaintext
{"version":3,"file":"index.cjs","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,\n\t\t($0, browser, query) => browser === 'all'\n\t\t\t? '> 0%'\n\t\t: `${browser}${\n\t\t\tquery\n\t\t\t\t? /^((?:\\d*\\.)?\\d+)-$/.test(query)\n\t\t\t\t\t? ` <= ${query.slice(0, -1)}`\n\t\t\t\t: ` ${query}`\n\t\t\t: ' > 0'\n\t\t}`\n\t).toLowerCase()\n)\nconst getBrowsersListInSplitRegExp = /\\s+in\\s+/\nconst getBrowsersListAndSplitRegExp = /(?: and|, and|,)/\nconst getBrowsersListQueryRegExp = /^\\s*(\\w+)(?: ((?:(?:\\d*\\.)?\\d+-)?(?:\\d*\\.)?\\d+[+-]?))?.*$/\n"],"names":["plugin","opts","postcssPlugin","Once","root","clientBrowserList","browserslist","Object","browsers","path","source","input","file","references","nodes","slice","node","isBrowserCommentNode","rule","next","browserdata","getBrowserData","text","isNumbered","filter","isBrowserReferenceCommentNode","map","comment","browserdataIndex","parseFloat","browserslistPart","removeRule","some","clientBrowser","commentBrowser","prev","remove","length","postcss","type","isBrowserCommentNodeRegExp","test","isBrowserReferenceCommentNodeRegExp","browserDataNumbered","match","browserDataMutliRegExp","Boolean","getBrowsersList","replace","browserDataNumberedNewlineRegExp","browserDataNewlineRegExp","split","getBrowsersListInSplitRegExp","part","getBrowsersListAndSplitRegExp","part2","reduce","acc","val","concat","getBrowsersListQueryRegExp","$0","browser","query","toLowerCase"],"mappings":";;;;;;MAEMA,MAAM,GAAGC,IAAI,IAAI;AACtB,SAAO;AACNC,IAAAA,aAAa,EAAE,0BADT;;AAENC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