postcss-scoper
Version:
Parse CSS and add scoped selector
2 lines (1 loc) • 1.43 kB
JavaScript
;var e=require("postcss"),r=require("css-selector-parser"),t=new r.CssSelectorParser,c=(t.registerSelectorPseudos("has"),t.registerNestingOperators(">","+","~"),t.registerAttrEqualityMods("^","$","*","~"),t.enableSubstitutes(),function(e){if("string"!=typeof e)throw new TypeError("@postcss-scoper: Invalid selector: ".concat(e,", scope options should be type of string."));var r=e.trim();if(""===r)throw new TypeError("@postcss-scoper: Invalid selector: ".concat(e,", scope options should not be empty."));if(s(r))return r;throw new TypeError("@postcss-scoper: Invalid selector: ".concat(e,", scope options should fit css selector rule"))}),s=function(e){try{t.parse(e)}catch(e){return!1}return!0},o=["keyframes","-webkit-keyframes","-moz-keyframes"],i=function(r){return o.some(function(e){return r.startsWith(e)})},n=[":root"],u=function(r){return n.some(function(e){return r.startsWith(e)})},a=function(r,e){return e.find(function(e){return r.startsWith(e)})},p=function(e,r,t){return e.replace(r,t)},l=Symbol("processed"),f=function(n){return function(e){var r=Object.assign({},{scope:"",overwrites:[]},n),t=r.scope,s=r.overwrites,o=c(t);e.walkAtRules(function(e){i(e.name)&&e.walkRules(function(e){e[l]=!0})}),e.walkRules(function(e){e[l]||(e.selectors=e.selectors.map(function(e){if(u(e))return e;var r=a(e,s);return r?p(e,r,o):"".concat(o," ").concat(e)}))})}},w=e.plugin("postcss-scoper",f);module.exports=w;