text: import fs from"fs";import glob from"glob";import postcss from"postcss";import selectorParser from"postcss-selector-parser";function normalizeArray(t,e){for(var r=0,n=t.length-1;n>=0;n--){var o=t[n];"."===o?t.splice(n,1):".."===o?(t.splice(n,1),r++):r&&(t.splice(n,1),r--)}if(e)for(;r--;r)t.unshift("..");return t}var splitPathRe=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,splitPath=function(t){return splitPathRe.exec(t).slice(1)};function resolve(){for(var t="",e=!1,r=arguments.length-1;r>=-1&&!e;r--){var n=r>=0?arguments[r]:"/";if("string"!=typeof n)throw new TypeError("Arguments to path.resolve must be strings");n&&(t=n+"/"+t,e="/"===n.charAt(0))}return t=normalizeArray(filter(t.split("/"),function(t){return!!t}),!e).join("/"),(e?"/":"")+t||"."}function normalize(t){var e=isAbsolute(t),r="/"===substr(t,-1);return(t=normalizeArray(filter(t.split("/"),function(t){return!!t}),!e).join("/"))||e||(t="."),t&&r&&(t+="/"),(e?"/":"")+t}function isAbsolute(t){return"/"===t.charAt(0)}function join(){return normalize(filter(Array.prototype.slice.call(arguments,0),function(t,e){if("string"!=typeof t)throw new TypeError("Arguments to path.join must be strings");return t}).join("/"))}function relative(t,e){function r(t){for(var e=0;e<t.length&&""===t[e];e++);for(var r=t.length-1;r>=0&&""===t[r];r--);return e>r?[]:t.slice(e,r-e+1)}t=resolve(t).substr(1),e=resolve(e).substr(1);for(var n=r(t.split("/")),o=r(e.split("/")),i=Math.min(n.length,o.length),a=i,s=0;s<i;s++)if(n[s]!==o[s]){a=s;break}var l=[];for(s=a;s<n.length;s++)l.push("..");return(l=l.concat(o.slice(a))).join("/")}var sep="/",delimiter=":";function dirname(t){var e=splitPath(t),r=e[0],n=e[1];return r||n?(n&&(n=n.substr(0,n.length-1)),r+n):"."}function basename(t,e){var r=splitPath(t)[2];return e&&r.substr(-1*e.length)===e&&(r=r.substr(0,r.length-e.length)),r}function extname(t){return splitPath(t)[3]}var path={extname:extname,basename:basename,dirname:dirname,sep:sep,delimiter:delimiter,relative:relative,join:join,isAbsolute:isAbsolute,normalize:normalize,resolve:resolve};function filter(t,e){if(t.filter)return t.filter(e);for(var r=[],n=0;n<t.length;n++)e(t[n],n,t)&&r.push(t[n]);return r}var substr="b"==="ab".substr(-1)?function(t,e,r){return t.substr(e,r)}:function(t,e,r){return e<0&&(e=t.length+e),t.substr(e,r)},defaultOptions={css:[],content:[],extractors:[],whitelist:[],output:void 0,stdout:!1,keyframes:!1,fontFace:!1},IGNORE_ANNOTATION="purgecss ignore",CONFIG_FILENAME="purgecss.config.js",ERROR_CONFIG_FILE_LOADING="Error loading the config file",ERROR_MISSING_CONTENT="No content provided.",ERROR_MISSING_CSS="No css provided.",ERROR_EXTRACTER_FAILED="The extractor has failed to extract the selectors.",ERROR_OPTIONS_TYPE="Error Type Options: expected an object",ERROR_OUTPUT_TYPE="Error Type option output: expected a string",ERROR_EXTRACTERS_TYPE="Error Type option extractors: expected an array",ERROR_WHITELIST_TYPE="Error Type option whitelist: expected an array",ERROR_WHITELIST_PATTERNS_TYPE="Error Type option whitelistPatterns: expected an array",ERROR_STDOUT_TYPE="Error Type option stdout: expected a boolean",CSS_WHITELIST=["*","::-webkit-scrollbar","::selection",":root","::before","::after"],SELECTOR_STANDARD_TYPES=["class","id","universal","pseudo"],_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},classCallCheck=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},createClass=function(){function t(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,r,n){return r&&t(e.prototype,r),n&&t(e,n),e}}(),toConsumableArray=function(t){if(Array.isArray(t)){for(var e=0,r=Array(t.length);e<t.length;e++)r[e]=t[e];return r}return Array.from(t)},DefaultExtractor=function(){function t(){classCallCheck(this,t)}return createClass(t,null,[{key:"extract",value:function(t){return t.match(/[A-Za-z0-9_-]+/g)||[]}}]),t}(),Purgecss=function(){function t(e){classCallCheck(this,t),this.atRules={keyframes:[],fontFace:[]},this.usedAnimations=new Set,this.usedFontFaces=new Set,this.selectorsRemoved=new Set,"string"!=typeof e&&void 0!==e||(e=this.loadConfigFile(e)),this.checkOptions(e),this.options=Object.assign(defaultOptions,e)}return createClass(t,[{key:"loadConfigFile",value:function(t){var e=void 0===t?CONFIG_FILENAME:t,r=void 0;try{var n=path.resolve(process.cwd(),e);r=require(n)}catch(t){throw new Error(ERROR_CONFIG_FILE_LOADING+t.message)}return r}},{key:"checkOptions",value:function(t){if("object"!==(void 0===t?"undefined":_typeof(t)))throw new TypeError(ERROR_OPTIONS_TYPE);if(!t.content||!t.content.length)throw new Error(ERROR_MISSING_CONTENT);if(!t.css||!t.css.length)throw new Error(ERROR_MISSING_CSS);if(t.output&&"string"!=typeof t.output)throw new TypeError(ERROR_OUTPUT_TYPE);if(t.extractors&&!Array.isArray(t.extractors))throw new TypeError(ERROR_EXTRACTERS_TYPE);if(t.whitelist&&!Array.isArray(t.whitelist))throw new TypeError(ERROR_WHITELIST_TYPE);if(t.stdout&&"boolean"!=typeof t.stdout)throw new TypeError(ERROR_STDOUT_TYPE);if(t.whitelistPatterns&&!Array.isArray(t.whitelistPatterns))throw new TypeError(ERROR_WHITELIST_PATTERNS_TYPE)}},{key:"purge",value:function(){var t=this.options,e=t.content,r=t.extractors,n=t.css,o=e.filter(function(t){return"string"==typeof t}),i=e.filter(function(t){return"object"===(void 0===t?"undefined":_typeof(t))}),a=this.extractFileSelector(o,r),s=this.extractRawSelector(i,r);return this.getCssContents(n,new Set([].concat(toConsumableArray(a),toConsumableArray(s))))}},{key:"getCssContents",value:function(t,e){var r=[];t=t.map(function(t){return"string"==typeof t?glob.sync(t):t}),t=[].concat.apply([],t);var n=!0,o=!1,i=void 0;try{for(var a,s=t[Symbol.iterator]();!(n=(a=s.next()).done);n=!0){var l=a.value,u=null,c="";"string"==typeof l?(u=l,c=this.options.stdin?u:fs.readFileSync(u,"utf8")):c=l.raw,this.root=postcss.parse(c),this.getSelectorsCss(e),this.options.keyframes&&this.removeUnusedKeyframes(),this.options.fontFace&&this.removeUnusedFontFaces(),r.push({file:u,css:this.root.toString()})}}catch(t){o=!0,i=t}finally{try{!n&&s.return&&s.return()}finally{if(o)throw i}}return r}},{key:"removeUnusedKeyframes",value:function(){var t=!0,e=!1,r=void 0;try{for(var n,o=this.atRules.keyframes[Symbol.iterator]();!(t=(n=o.next()).done);t=!0){var i=n.value,a=i.params;this.usedAnimations.has(a)||i.remove()}}catch(t){e=!0,r=t}finally{try{!t&&o.return&&o.return()}finally{if(e)throw r}}}},{key:"removeUnusedFontFaces",value:function(){var t=!0,e=!1,r=void 0;try{for(var n,o=this.atRules.fontFace[Symbol.iterator]();!(t=(n=o.next()).done);t=!0){var i=n.value,a=i.node,s=i.name;this.usedFontFaces.has(s)||a.remove()}}catch(t){e=!0,r=t}finally{try{!t&&o.return&&o.return()}finally{if(e)throw r}}}},{key:"extractRawSelector",value:function(t,e){var r=new Set,n=!0,o=!1,i=void 0;try{for(var a,s=t[Symbol.iterator]();!(n=(a=s.next()).done);n=!0){var l=a.value,u=l.raw,c=l.extension,f=this.getFileExtractor("."+c,e);r=new Set([].concat(toConsumableArray(r),toConsumableArray(this.extractSelectors(u,f))))}}catch(t){o=!0,i=t}finally{try{!n&&s.return&&s.return()}finally{if(o)throw i}}return r}},{key:"extractFileSelector",value:function(t,e){var r=new Set,n=!0,o=!1,i=void 0;try{for(var a,s=t[Symbol.iterator]();!(n=(a=s.next()).done);n=!0){var l=a.value,u=[];fs.existsSync(l)?u.push(l):u=glob.sync(l);var c=!0,f=!1,y=void 0;try{for(var h,p=u[Symbol.iterator]();!(c=(h=p.next()).done);c=!0){var v=h.value,d=fs.readFileSync(v,"utf8"),m=this.getFileExtractor(v,e);r=new Set([].concat(toConsumableArray(r),toConsumableArray(this.extractSelectors(d,m))))}}catch(t){f=!0,y=t}finally{try{!c&&p.return&&p.return()}finally{if(f)throw y}}}}catch(t){o=!0,i=t}finally{try{!n&&s.return&&s.return()}finally{if(o)throw i}}return r}},{key:"getFileExtractor",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];return e.length?e.find(function(e){return e.extensions.find(function(e){return t.endsWith(e)})}).extractor:DefaultExtractor}},{key:"extractSelectors",value:function(t,e){var r=new Set,n=e.extract(t);if(null===n)throw new Error(ERROR_EXTRACTER_FAILED);return n.forEach(function(t){r.add(t)}),r.delete(""),r}},{key:"getSelectorsCss",value:function(t){var e=this;this.root.walk(function(r){return"rule"===r.type?e.evaluateRule(r,t):"atrule"===r.type?e.evaluateAtRule(r):void 0})}},{key:"evaluateRule",value:function(t,e){var r=this,n=t.prev();if(!this.isIgnoreAnnotation(n)){var o=!0;if(t.selector=selectorParser(function(t){t.walk(function(t){var n=[];if("selector"===t.type){if(t.parent&&":not"===t.parent.value&&"pseudo"===t.parent.type)return;var i=!0,a=!1,s=void 0;try{for(var l,u=t.nodes[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var c=l.value,f=c.type,y=c.value;SELECTOR_STANDARD_TYPES.includes(f)&&void 0!==y&&!1===/^\d/g.test(y)?n.push(y):"tag"!==f||/[+]|(even)|(odd)|^from$|^to$|^\d/.test(y)||n.push(y)}}catch(t){a=!0,s=t}finally{try{!i&&u.return&&u.return()}finally{if(a)throw s}}(o=r.shouldKeepSelector(e,n))||t.remove()}})}).processSync(t.selector),o){var i=!0,a=!1,s=void 0;try{for(var l,u=t.nodes[Symbol.iterator]();!(i=(l=u.next()).done);i=!0){var c=l.value,f=c.prop,y=c.value;if(this.options.keyframes&&("animation"===f||"animation-name"===f)){var h=!0,p=!1,v=void 0;try{for(var d,m=y.split(" ")[Symbol.iterator]();!(h=(d=m.next()).done);h=!0){var E=d.value;this.usedAnimations.add(E)}}catch(t){p=!0,v=t}finally{try{!h&&m.return&&m.return()}finally{if(p)throw v}}}this.options.fontFace&&"font-family"===f&&this.usedFontFaces.add(y)}}catch(t){a=!0,s=t}finally{try{!i&&u.return&&u.return()}finally{if(a)throw s}}}var R=t.parent;t.selector||t.remove(),this.isRuleEmpty(R)&&R.remove()}}},{key:"evaluateAtRule",value:function(t){if(this.options.keyframes&&t.name.endsWith("keyframes"))this.atRules.keyframes.push(t);else if(this.options.fontFace&&"font-face"===t.name){var e=!0,r=!1,n=void 0;try{for(var o,i=t.nodes[Symbol.iterator]();!(e=(o=i.next()).done);e=!0){var a=o.value,s=a.prop,l=a.value;"font-family"===s&&this.atRules.fontFace.push({name:l,node:t})}}catch(t){r=!0,n=t}finally{try{!e&&i.return&&i.return()}finally{if(r)throw n}}}else;}},{key:"isIgnoreAnnotation",value:function(t){return!(!t||"comment"!==t.type)&&t.text.includes(IGNORE_ANNOTATION)}},{key:"isRuleEmpty",value:function(t){return!!("decl"===t.type&&!t.value||"rule"===t.type&&!t.selector||t.nodes&&!t.nodes.length||"atrule"===t.type&&(!t.nodes&&!t.params||!t.params&&!t.nodes.length))}},{key:"shouldKeepSelector",value:function(t,e){var r=!0,n=!1,o=void 0;try{for(var i,a=e[Symbol.iterator]();!(r=(i=a.next()).done);r=!0){var s=i.value.replace(/\\/g,"");if(!s.startsWith(":")){if(this.isSelectorWhitelistedChildren(s))return!0;if(!(t.has(s)||CSS_WHITELIST.includes(s)||this.isSelectorWhitelisted(s)))return!1}}}catch(t){n=!0,o=t}finally{try{!r&&a.return&&a.return()}finally{if(n)throw o}}return!0}},{key:"isSelectorWhitelisted",value:function(t){return!!(CSS_WHITELIST.includes(t)||this.options.whitelist&&this.options.whitelist.some(function(e){return e===t})||this.options.whitelistPatterns&&this.options.whitelistPatterns.some(function(e){return e.test(t)}))}},{key:"isSelectorWhitelistedChildren",value:function(t){return!(!this.options.whitelistPatternsChildren||!this.options.whitelistPatternsChildren.some(function(e){return e.test(t)}))}}]),t}();export default Purgecss;
  