Comparing version 0.20.1 to 0.21.0
@@ -14,2 +14,3 @@ // @flow | ||
whitelistPatterns?: Array<RegExp>, | ||
whitelistPatternsChildren?: Array<RegExp>, | ||
output?: string, | ||
@@ -16,0 +17,0 @@ stdout?: boolean, |
@@ -43,2 +43,3 @@ // Type definitions for Purgecss 0.14.0 | ||
whitelistPatterns?: Array<RegExp> | ||
whitelistPatternsChildren?: Array<RegExp> | ||
output?: string | ||
@@ -45,0 +46,0 @@ stdout?: boolean |
@@ -1,1 +0,1 @@ | ||
import fs from"fs";import glob from"glob";import postcss from"postcss";import selectorParser from"postcss-selector-parser";function normalizeArray(e,t){for(var r=0,n=e.length-1;n>=0;n--){var o=e[n];"."===o?e.splice(n,1):".."===o?(e.splice(n,1),r++):r&&(e.splice(n,1),r--)}if(t)for(;r--;r)e.unshift("..");return e}var splitPathRe=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,splitPath=function(e){return splitPathRe.exec(e).slice(1)};function resolve(){for(var e="",t=!1,r=arguments.length-1;r>=-1&&!t;r--){var n=r>=0?arguments[r]:"/";if("string"!=typeof n)throw new TypeError("Arguments to path.resolve must be strings");n&&(e=n+"/"+e,t="/"===n.charAt(0))}return e=normalizeArray(filter(e.split("/"),function(e){return!!e}),!t).join("/"),(t?"/":"")+e||"."}function normalize(e){var t=isAbsolute(e),r="/"===substr(e,-1);return(e=normalizeArray(filter(e.split("/"),function(e){return!!e}),!t).join("/"))||t||(e="."),e&&r&&(e+="/"),(t?"/":"")+e}function isAbsolute(e){return"/"===e.charAt(0)}function join(){return normalize(filter(Array.prototype.slice.call(arguments,0),function(e,t){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e}).join("/"))}function relative(e,t){function r(e){for(var t=0;t<e.length&&""===e[t];t++);for(var r=e.length-1;r>=0&&""===e[r];r--);return t>r?[]:e.slice(t,r-t+1)}e=resolve(e).substr(1),t=resolve(t).substr(1);for(var n=r(e.split("/")),o=r(t.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(e){var t=splitPath(e),r=t[0],n=t[1];return r||n?(n&&(n=n.substr(0,n.length-1)),r+n):"."}function basename(e,t){var r=splitPath(e)[2];return t&&r.substr(-1*t.length)===t&&(r=r.substr(0,r.length-t.length)),r}function extname(e){return splitPath(e)[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(e,t){if(e.filter)return e.filter(t);for(var r=[],n=0;n<e.length;n++)t(e[n],n,e)&&r.push(e[n]);return r}var substr="b"==="ab".substr(-1)?function(e,t,r){return e.substr(t,r)}:function(e,t,r){return t<0&&(t=e.length+t),e.substr(t,r)},defaultOptions={css:[],content:[],extractors:[],whitelist:[],output:void 0,stdout:!1,info:!1,rejected:!1,legacy:!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",ERROR_INFO_TYPE="Error Type option info: expected a boolean",ERROR_REJECTED_TYPE="Error Type option rejected: 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(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},classCallCheck=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},createClass=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),toConsumableArray=function(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)},DefaultExtractor=function(){function e(){classCallCheck(this,e)}return createClass(e,null,[{key:"extract",value:function(e){return e.match(/[A-Za-z0-9_-]+/g)}}]),e}(),LegacyExtractor=function(){function e(){classCallCheck(this,e)}return createClass(e,null,[{key:"extract",value:function(e){return e.split(/[^a-z]/g)}}]),e}(),Purgecss=function(){function e(t){classCallCheck(this,e),this.atRules={keyframes:[],fontFace:[]},this.usedAnimations=new Set,this.usedFontFaces=new Set,this.selectorsRemoved=new Set,"string"!=typeof t&&void 0!==t||(t=this.loadConfigFile(t)),this.checkOptions(t),this.options=Object.assign(defaultOptions,t)}return createClass(e,[{key:"loadConfigFile",value:function(e){var t=void 0===e?CONFIG_FILENAME:e,r=void 0;try{var n=path.resolve(process.cwd(),t);r=require(n)}catch(e){throw new Error(ERROR_CONFIG_FILE_LOADING+e.message)}return r}},{key:"checkOptions",value:function(e){if("object"!==(void 0===e?"undefined":_typeof(e)))throw new TypeError(ERROR_OPTIONS_TYPE);if(!e.content||!e.content.length)throw new Error(ERROR_MISSING_CONTENT);if(!e.css||!e.css.length)throw new Error(ERROR_MISSING_CSS);if(e.output&&"string"!=typeof e.output)throw new TypeError(ERROR_OUTPUT_TYPE);if(e.extractors&&!Array.isArray(e.extractors))throw new TypeError(ERROR_EXTRACTERS_TYPE);if(e.whitelist&&!Array.isArray(e.whitelist))throw new TypeError(ERROR_WHITELIST_TYPE);if(e.stdout&&"boolean"!=typeof e.stdout)throw new TypeError(ERROR_STDOUT_TYPE);if(e.info&&"boolean"!=typeof e.info)throw new TypeError(ERROR_INFO_TYPE);if(e.rejected&&"boolean"!=typeof e.rejected)throw new TypeError(ERROR_REJECTED_TYPE);if(e.whitelistPatterns&&!Array.isArray(e.whitelistPatterns))throw new TypeError(ERROR_WHITELIST_PATTERNS_TYPE)}},{key:"purge",value:function(){var e=this.options,t=e.content,r=e.extractors,n=e.css,o=t.filter(function(e){return"string"==typeof e}),i=t.filter(function(e){return"object"===(void 0===e?"undefined":_typeof(e))}),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(e,t){var r=[],n=!0,o=!1,i=void 0;try{for(var a,s=e[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(t),this.options.keyframes&&this.removeUnusedKeyframes(),this.options.fontFace&&this.removeUnusedFontFaces(),r.push({file:u,css:this.root.toString()})}}catch(e){o=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(o)throw i}}return r}},{key:"removeUnusedKeyframes",value:function(){var e=!0,t=!1,r=void 0;try{for(var n,o=this.atRules.keyframes[Symbol.iterator]();!(e=(n=o.next()).done);e=!0){var i=n.value,a=i.params;this.usedAnimations.has(a)||i.remove()}}catch(e){t=!0,r=e}finally{try{!e&&o.return&&o.return()}finally{if(t)throw r}}}},{key:"removeUnusedFontFaces",value:function(){var e=!0,t=!1,r=void 0;try{for(var n,o=this.atRules.fontFace[Symbol.iterator]();!(e=(n=o.next()).done);e=!0){var i=n.value,a=i.node,s=i.name;this.usedFontFaces.has(s)||a.remove()}}catch(e){t=!0,r=e}finally{try{!e&&o.return&&o.return()}finally{if(t)throw r}}}},{key:"extractRawSelector",value:function(e,t){var r=new Set,n=!0,o=!1,i=void 0;try{for(var a,s=e[Symbol.iterator]();!(n=(a=s.next()).done);n=!0){var l=a.value,u=l.raw,c=l.extension,f=this.getFileExtractor("."+c,t);r=new Set([].concat(toConsumableArray(r),toConsumableArray(this.extractSelectors(u,f))))}}catch(e){o=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(o)throw i}}return r}},{key:"extractFileSelector",value:function(e,t){var r=new Set,n=!0,o=!1,i=void 0;try{for(var a,s=e[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"),E=this.getFileExtractor(v,t);r=new Set([].concat(toConsumableArray(r),toConsumableArray(this.extractSelectors(d,E))))}}catch(e){f=!0,y=e}finally{try{!c&&p.return&&p.return()}finally{if(f)throw y}}}}catch(e){o=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(o)throw i}}return r}},{key:"getFileExtractor",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];return t.length?t.find(function(t){return t.extensions.find(function(t){return e.endsWith(t)})}).extractor:!0===this.options.legacy?LegacyExtractor:DefaultExtractor}},{key:"extractSelectors",value:function(e,t){var r=new Set,n=t.extract(e);if(null===n)throw new Error(ERROR_EXTRACTER_FAILED);return n.forEach(function(e){r.add(e)}),r.delete(""),r}},{key:"getSelectorsCss",value:function(e){var t=this;this.root.walk(function(r){return"rule"===r.type?t.evaluateRule(r,e):"atrule"===r.type?t.evaluateAtRule(r):void 0})}},{key:"evaluateRule",value:function(e,t){var r=this,n=e.prev();if(!this.isIgnoreAnnotation(n)){var o=!0;if(e.selector=selectorParser(function(e){e.walk(function(e){var n=[];if("selector"===e.type){if(e.parent&&":not"===e.parent.value&&"pseudo"===e.parent.type)return;var i=!0,a=!1,s=void 0;try{for(var l,u=e.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?n.push(y):"tag"!==f||/[+]|(even)|(odd)|^from$|^to$|^\d/.test(y)||n.push(y)}}catch(e){a=!0,s=e}finally{try{!i&&u.return&&u.return()}finally{if(a)throw s}}(o=r.shouldKeepSelector(t,n))||e.remove()}})}).processSync(e.selector),o){var i=!0,a=!1,s=void 0;try{for(var l,u=e.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,E=y.split(" ")[Symbol.iterator]();!(h=(d=E.next()).done);h=!0){var R=d.value;this.usedAnimations.add(R)}}catch(e){p=!0,v=e}finally{try{!h&&E.return&&E.return()}finally{if(p)throw v}}}this.options.fontFace&&"font-family"===f&&this.usedFontFaces.add(y)}}catch(e){a=!0,s=e}finally{try{!i&&u.return&&u.return()}finally{if(a)throw s}}}var m=e.parent;e.selector||e.remove(),this.isRuleEmpty(m)&&m.remove()}}},{key:"evaluateAtRule",value:function(e){if(this.options.keyframes&&e.name.endsWith("keyframes"))this.atRules.keyframes.push(e);else if(this.options.fontFace&&"font-face"===e.name){var t=!0,r=!1,n=void 0;try{for(var o,i=e.nodes[Symbol.iterator]();!(t=(o=i.next()).done);t=!0){var a=o.value,s=a.prop,l=a.value;"font-family"===s&&this.atRules.fontFace.push({name:l,node:e})}}catch(e){r=!0,n=e}finally{try{!t&&i.return&&i.return()}finally{if(r)throw n}}}else;}},{key:"isIgnoreAnnotation",value:function(e){return!(!e||"comment"!==e.type)&&e.text.includes(IGNORE_ANNOTATION)}},{key:"isRuleEmpty",value:function(e){return!!("decl"===e.type&&!e.value||"rule"===e.type&&!e.selector||e.nodes&&!e.nodes.length||"atrule"===e.type&&(!e.nodes&&!e.params||!e.params&&!e.nodes.length))}},{key:"shouldKeepSelector",value:function(e,t){var r=!0,n=!1,o=void 0;try{for(var i,a=t[Symbol.iterator]();!(r=(i=a.next()).done);r=!0){var s=i.value;if(this.options.legacy){var l=s.split(/[^a-z]/g),u=!1,c=!0,f=!1,y=void 0;try{for(var h,p=l[Symbol.iterator]();!(c=(h=p.next()).done);c=!0){var v=h.value;if(v){if(!e.has(v))break;u=!0}}}catch(e){f=!0,y=e}finally{try{!c&&p.return&&p.return()}finally{if(f)throw y}}if(u)return!0;if(e.has(s)||CSS_WHITELIST.includes(s)||this.isSelectorWhitelisted(s))return!0}else{var d=s.replace(/\\/g,"");if(d.startsWith(":"))continue;if(!(e.has(d)||CSS_WHITELIST.includes(d)||this.isSelectorWhitelisted(d)))return!1}}}catch(e){n=!0,o=e}finally{try{!r&&a.return&&a.return()}finally{if(n)throw o}}return!this.options.legacy}},{key:"isSelectorWhitelisted",value:function(e){return!!(CSS_WHITELIST.includes(e)||this.options.whitelist&&this.options.whitelist.some(function(t){return t===e})||this.options.whitelistPatterns&&this.options.whitelistPatterns.some(function(t){return t.test(e)}))}}]),e}();export default Purgecss; | ||
import fs from"fs";import glob from"glob";import postcss from"postcss";import selectorParser from"postcss-selector-parser";function normalizeArray(e,t){for(var r=0,n=e.length-1;n>=0;n--){var o=e[n];"."===o?e.splice(n,1):".."===o?(e.splice(n,1),r++):r&&(e.splice(n,1),r--)}if(t)for(;r--;r)e.unshift("..");return e}var splitPathRe=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,splitPath=function(e){return splitPathRe.exec(e).slice(1)};function resolve(){for(var e="",t=!1,r=arguments.length-1;r>=-1&&!t;r--){var n=r>=0?arguments[r]:"/";if("string"!=typeof n)throw new TypeError("Arguments to path.resolve must be strings");n&&(e=n+"/"+e,t="/"===n.charAt(0))}return e=normalizeArray(filter(e.split("/"),function(e){return!!e}),!t).join("/"),(t?"/":"")+e||"."}function normalize(e){var t=isAbsolute(e),r="/"===substr(e,-1);return(e=normalizeArray(filter(e.split("/"),function(e){return!!e}),!t).join("/"))||t||(e="."),e&&r&&(e+="/"),(t?"/":"")+e}function isAbsolute(e){return"/"===e.charAt(0)}function join(){return normalize(filter(Array.prototype.slice.call(arguments,0),function(e,t){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e}).join("/"))}function relative(e,t){function r(e){for(var t=0;t<e.length&&""===e[t];t++);for(var r=e.length-1;r>=0&&""===e[r];r--);return t>r?[]:e.slice(t,r-t+1)}e=resolve(e).substr(1),t=resolve(t).substr(1);for(var n=r(e.split("/")),o=r(t.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(e){var t=splitPath(e),r=t[0],n=t[1];return r||n?(n&&(n=n.substr(0,n.length-1)),r+n):"."}function basename(e,t){var r=splitPath(e)[2];return t&&r.substr(-1*t.length)===t&&(r=r.substr(0,r.length-t.length)),r}function extname(e){return splitPath(e)[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(e,t){if(e.filter)return e.filter(t);for(var r=[],n=0;n<e.length;n++)t(e[n],n,e)&&r.push(e[n]);return r}var substr="b"==="ab".substr(-1)?function(e,t,r){return e.substr(t,r)}:function(e,t,r){return t<0&&(t=e.length+t),e.substr(t,r)},defaultOptions={css:[],content:[],extractors:[],whitelist:[],output:void 0,stdout:!1,info:!1,rejected:!1,legacy:!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",ERROR_INFO_TYPE="Error Type option info: expected a boolean",ERROR_REJECTED_TYPE="Error Type option rejected: 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(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},classCallCheck=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},createClass=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),toConsumableArray=function(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)},DefaultExtractor=function(){function e(){classCallCheck(this,e)}return createClass(e,null,[{key:"extract",value:function(e){return e.match(/[A-Za-z0-9_-]+/g)}}]),e}(),LegacyExtractor=function(){function e(){classCallCheck(this,e)}return createClass(e,null,[{key:"extract",value:function(e){return e.split(/[^a-z]/g)}}]),e}(),Purgecss=function(){function e(t){classCallCheck(this,e),this.atRules={keyframes:[],fontFace:[]},this.usedAnimations=new Set,this.usedFontFaces=new Set,this.selectorsRemoved=new Set,"string"!=typeof t&&void 0!==t||(t=this.loadConfigFile(t)),this.checkOptions(t),this.options=Object.assign(defaultOptions,t)}return createClass(e,[{key:"loadConfigFile",value:function(e){var t=void 0===e?CONFIG_FILENAME:e,r=void 0;try{var n=path.resolve(process.cwd(),t);r=require(n)}catch(e){throw new Error(ERROR_CONFIG_FILE_LOADING+e.message)}return r}},{key:"checkOptions",value:function(e){if("object"!==(void 0===e?"undefined":_typeof(e)))throw new TypeError(ERROR_OPTIONS_TYPE);if(!e.content||!e.content.length)throw new Error(ERROR_MISSING_CONTENT);if(!e.css||!e.css.length)throw new Error(ERROR_MISSING_CSS);if(e.output&&"string"!=typeof e.output)throw new TypeError(ERROR_OUTPUT_TYPE);if(e.extractors&&!Array.isArray(e.extractors))throw new TypeError(ERROR_EXTRACTERS_TYPE);if(e.whitelist&&!Array.isArray(e.whitelist))throw new TypeError(ERROR_WHITELIST_TYPE);if(e.stdout&&"boolean"!=typeof e.stdout)throw new TypeError(ERROR_STDOUT_TYPE);if(e.info&&"boolean"!=typeof e.info)throw new TypeError(ERROR_INFO_TYPE);if(e.rejected&&"boolean"!=typeof e.rejected)throw new TypeError(ERROR_REJECTED_TYPE);if(e.whitelistPatterns&&!Array.isArray(e.whitelistPatterns))throw new TypeError(ERROR_WHITELIST_PATTERNS_TYPE)}},{key:"purge",value:function(){var e=this.options,t=e.content,r=e.extractors,n=e.css,o=t.filter(function(e){return"string"==typeof e}),i=t.filter(function(e){return"object"===(void 0===e?"undefined":_typeof(e))}),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(e,t){var r=[],n=!0,o=!1,i=void 0;try{for(var a,s=e[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(t),this.options.keyframes&&this.removeUnusedKeyframes(),this.options.fontFace&&this.removeUnusedFontFaces(),r.push({file:u,css:this.root.toString()})}}catch(e){o=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(o)throw i}}return r}},{key:"removeUnusedKeyframes",value:function(){var e=!0,t=!1,r=void 0;try{for(var n,o=this.atRules.keyframes[Symbol.iterator]();!(e=(n=o.next()).done);e=!0){var i=n.value,a=i.params;this.usedAnimations.has(a)||i.remove()}}catch(e){t=!0,r=e}finally{try{!e&&o.return&&o.return()}finally{if(t)throw r}}}},{key:"removeUnusedFontFaces",value:function(){var e=!0,t=!1,r=void 0;try{for(var n,o=this.atRules.fontFace[Symbol.iterator]();!(e=(n=o.next()).done);e=!0){var i=n.value,a=i.node,s=i.name;this.usedFontFaces.has(s)||a.remove()}}catch(e){t=!0,r=e}finally{try{!e&&o.return&&o.return()}finally{if(t)throw r}}}},{key:"extractRawSelector",value:function(e,t){var r=new Set,n=!0,o=!1,i=void 0;try{for(var a,s=e[Symbol.iterator]();!(n=(a=s.next()).done);n=!0){var l=a.value,u=l.raw,c=l.extension,f=this.getFileExtractor("."+c,t);r=new Set([].concat(toConsumableArray(r),toConsumableArray(this.extractSelectors(u,f))))}}catch(e){o=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(o)throw i}}return r}},{key:"extractFileSelector",value:function(e,t){var r=new Set,n=!0,o=!1,i=void 0;try{for(var a,s=e[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"),E=this.getFileExtractor(v,t);r=new Set([].concat(toConsumableArray(r),toConsumableArray(this.extractSelectors(d,E))))}}catch(e){f=!0,y=e}finally{try{!c&&p.return&&p.return()}finally{if(f)throw y}}}}catch(e){o=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(o)throw i}}return r}},{key:"getFileExtractor",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];return t.length?t.find(function(t){return t.extensions.find(function(t){return e.endsWith(t)})}).extractor:!0===this.options.legacy?LegacyExtractor:DefaultExtractor}},{key:"extractSelectors",value:function(e,t){var r=new Set,n=t.extract(e);if(null===n)throw new Error(ERROR_EXTRACTER_FAILED);return n.forEach(function(e){r.add(e)}),r.delete(""),r}},{key:"getSelectorsCss",value:function(e){var t=this;this.root.walk(function(r){return"rule"===r.type?t.evaluateRule(r,e):"atrule"===r.type?t.evaluateAtRule(r):void 0})}},{key:"evaluateRule",value:function(e,t){var r=this,n=e.prev();if(!this.isIgnoreAnnotation(n)){var o=!0;if(e.selector=selectorParser(function(e){e.walk(function(e){var n=[];if("selector"===e.type){if(e.parent&&":not"===e.parent.value&&"pseudo"===e.parent.type)return;var i=!0,a=!1,s=void 0;try{for(var l,u=e.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(e){a=!0,s=e}finally{try{!i&&u.return&&u.return()}finally{if(a)throw s}}(o=r.shouldKeepSelector(t,n))||e.remove()}})}).processSync(e.selector),o){var i=!0,a=!1,s=void 0;try{for(var l,u=e.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,E=y.split(" ")[Symbol.iterator]();!(h=(d=E.next()).done);h=!0){var m=d.value;this.usedAnimations.add(m)}}catch(e){p=!0,v=e}finally{try{!h&&E.return&&E.return()}finally{if(p)throw v}}}this.options.fontFace&&"font-family"===f&&this.usedFontFaces.add(y)}}catch(e){a=!0,s=e}finally{try{!i&&u.return&&u.return()}finally{if(a)throw s}}}var R=e.parent;e.selector||e.remove(),this.isRuleEmpty(R)&&R.remove()}}},{key:"evaluateAtRule",value:function(e){if(this.options.keyframes&&e.name.endsWith("keyframes"))this.atRules.keyframes.push(e);else if(this.options.fontFace&&"font-face"===e.name){var t=!0,r=!1,n=void 0;try{for(var o,i=e.nodes[Symbol.iterator]();!(t=(o=i.next()).done);t=!0){var a=o.value,s=a.prop,l=a.value;"font-family"===s&&this.atRules.fontFace.push({name:l,node:e})}}catch(e){r=!0,n=e}finally{try{!t&&i.return&&i.return()}finally{if(r)throw n}}}else;}},{key:"isIgnoreAnnotation",value:function(e){return!(!e||"comment"!==e.type)&&e.text.includes(IGNORE_ANNOTATION)}},{key:"isRuleEmpty",value:function(e){return!!("decl"===e.type&&!e.value||"rule"===e.type&&!e.selector||e.nodes&&!e.nodes.length||"atrule"===e.type&&(!e.nodes&&!e.params||!e.params&&!e.nodes.length))}},{key:"shouldKeepSelector",value:function(e,t){var r=!0,n=!1,o=void 0;try{for(var i,a=t[Symbol.iterator]();!(r=(i=a.next()).done);r=!0){var s=i.value;if(this.options.legacy){var l=s.split(/[^a-z]/g),u=!1,c=!0,f=!1,y=void 0;try{for(var h,p=l[Symbol.iterator]();!(c=(h=p.next()).done);c=!0){var v=h.value;if(this.isSelectorWhitelistedChildren(v)){u=!0;break}if(v){if(!e.has(v))break;u=!0}}}catch(e){f=!0,y=e}finally{try{!c&&p.return&&p.return()}finally{if(f)throw y}}if(u)return!0;if(e.has(s)||CSS_WHITELIST.includes(s)||this.isSelectorWhitelisted(s))return!0}else{var d=s.replace(/\\/g,"");if(d.startsWith(":"))continue;if(this.isSelectorWhitelistedChildren(d))return!0;if(!(e.has(d)||CSS_WHITELIST.includes(d)||this.isSelectorWhitelisted(d)))return!1}}}catch(e){n=!0,o=e}finally{try{!r&&a.return&&a.return()}finally{if(n)throw o}}return!this.options.legacy}},{key:"isSelectorWhitelisted",value:function(e){return!!(CSS_WHITELIST.includes(e)||this.options.whitelist&&this.options.whitelist.some(function(t){return t===e})||this.options.whitelistPatterns&&this.options.whitelistPatterns.some(function(t){return t.test(e)}))}},{key:"isSelectorWhitelistedChildren",value:function(e){return!(!this.options.whitelistPatternsChildren||!this.options.whitelistPatternsChildren.some(function(t){return t.test(e)}))}}]),e}();export default Purgecss; |
@@ -1,1 +0,1 @@ | ||
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var fs=_interopDefault(require("fs")),glob=_interopDefault(require("glob")),postcss=_interopDefault(require("postcss")),selectorParser=_interopDefault(require("postcss-selector-parser"));function normalizeArray(e,t){for(var r=0,n=e.length-1;n>=0;n--){var o=e[n];"."===o?e.splice(n,1):".."===o?(e.splice(n,1),r++):r&&(e.splice(n,1),r--)}if(t)for(;r--;r)e.unshift("..");return e}var splitPathRe=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,splitPath=function(e){return splitPathRe.exec(e).slice(1)};function resolve(){for(var e="",t=!1,r=arguments.length-1;r>=-1&&!t;r--){var n=r>=0?arguments[r]:"/";if("string"!=typeof n)throw new TypeError("Arguments to path.resolve must be strings");n&&(e=n+"/"+e,t="/"===n.charAt(0))}return e=normalizeArray(filter(e.split("/"),function(e){return!!e}),!t).join("/"),(t?"/":"")+e||"."}function normalize(e){var t=isAbsolute(e),r="/"===substr(e,-1);return(e=normalizeArray(filter(e.split("/"),function(e){return!!e}),!t).join("/"))||t||(e="."),e&&r&&(e+="/"),(t?"/":"")+e}function isAbsolute(e){return"/"===e.charAt(0)}function join(){return normalize(filter(Array.prototype.slice.call(arguments,0),function(e,t){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e}).join("/"))}function relative(e,t){function r(e){for(var t=0;t<e.length&&""===e[t];t++);for(var r=e.length-1;r>=0&&""===e[r];r--);return t>r?[]:e.slice(t,r-t+1)}e=resolve(e).substr(1),t=resolve(t).substr(1);for(var n=r(e.split("/")),o=r(t.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(e){var t=splitPath(e),r=t[0],n=t[1];return r||n?(n&&(n=n.substr(0,n.length-1)),r+n):"."}function basename(e,t){var r=splitPath(e)[2];return t&&r.substr(-1*t.length)===t&&(r=r.substr(0,r.length-t.length)),r}function extname(e){return splitPath(e)[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(e,t){if(e.filter)return e.filter(t);for(var r=[],n=0;n<e.length;n++)t(e[n],n,e)&&r.push(e[n]);return r}var substr="b"==="ab".substr(-1)?function(e,t,r){return e.substr(t,r)}:function(e,t,r){return t<0&&(t=e.length+t),e.substr(t,r)},defaultOptions={css:[],content:[],extractors:[],whitelist:[],output:void 0,stdout:!1,info:!1,rejected:!1,legacy:!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",ERROR_INFO_TYPE="Error Type option info: expected a boolean",ERROR_REJECTED_TYPE="Error Type option rejected: 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(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},classCallCheck=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},createClass=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),toConsumableArray=function(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)},DefaultExtractor=function(){function e(){classCallCheck(this,e)}return createClass(e,null,[{key:"extract",value:function(e){return e.match(/[A-Za-z0-9_-]+/g)}}]),e}(),LegacyExtractor=function(){function e(){classCallCheck(this,e)}return createClass(e,null,[{key:"extract",value:function(e){return e.split(/[^a-z]/g)}}]),e}(),Purgecss=function(){function e(t){classCallCheck(this,e),this.atRules={keyframes:[],fontFace:[]},this.usedAnimations=new Set,this.usedFontFaces=new Set,this.selectorsRemoved=new Set,"string"!=typeof t&&void 0!==t||(t=this.loadConfigFile(t)),this.checkOptions(t),this.options=Object.assign(defaultOptions,t)}return createClass(e,[{key:"loadConfigFile",value:function(e){var t=void 0===e?CONFIG_FILENAME:e,r=void 0;try{var n=path.resolve(process.cwd(),t);r=require(n)}catch(e){throw new Error(ERROR_CONFIG_FILE_LOADING+e.message)}return r}},{key:"checkOptions",value:function(e){if("object"!==(void 0===e?"undefined":_typeof(e)))throw new TypeError(ERROR_OPTIONS_TYPE);if(!e.content||!e.content.length)throw new Error(ERROR_MISSING_CONTENT);if(!e.css||!e.css.length)throw new Error(ERROR_MISSING_CSS);if(e.output&&"string"!=typeof e.output)throw new TypeError(ERROR_OUTPUT_TYPE);if(e.extractors&&!Array.isArray(e.extractors))throw new TypeError(ERROR_EXTRACTERS_TYPE);if(e.whitelist&&!Array.isArray(e.whitelist))throw new TypeError(ERROR_WHITELIST_TYPE);if(e.stdout&&"boolean"!=typeof e.stdout)throw new TypeError(ERROR_STDOUT_TYPE);if(e.info&&"boolean"!=typeof e.info)throw new TypeError(ERROR_INFO_TYPE);if(e.rejected&&"boolean"!=typeof e.rejected)throw new TypeError(ERROR_REJECTED_TYPE);if(e.whitelistPatterns&&!Array.isArray(e.whitelistPatterns))throw new TypeError(ERROR_WHITELIST_PATTERNS_TYPE)}},{key:"purge",value:function(){var e=this.options,t=e.content,r=e.extractors,n=e.css,o=t.filter(function(e){return"string"==typeof e}),i=t.filter(function(e){return"object"===(void 0===e?"undefined":_typeof(e))}),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(e,t){var r=[],n=!0,o=!1,i=void 0;try{for(var a,s=e[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(t),this.options.keyframes&&this.removeUnusedKeyframes(),this.options.fontFace&&this.removeUnusedFontFaces(),r.push({file:u,css:this.root.toString()})}}catch(e){o=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(o)throw i}}return r}},{key:"removeUnusedKeyframes",value:function(){var e=!0,t=!1,r=void 0;try{for(var n,o=this.atRules.keyframes[Symbol.iterator]();!(e=(n=o.next()).done);e=!0){var i=n.value,a=i.params;this.usedAnimations.has(a)||i.remove()}}catch(e){t=!0,r=e}finally{try{!e&&o.return&&o.return()}finally{if(t)throw r}}}},{key:"removeUnusedFontFaces",value:function(){var e=!0,t=!1,r=void 0;try{for(var n,o=this.atRules.fontFace[Symbol.iterator]();!(e=(n=o.next()).done);e=!0){var i=n.value,a=i.node,s=i.name;this.usedFontFaces.has(s)||a.remove()}}catch(e){t=!0,r=e}finally{try{!e&&o.return&&o.return()}finally{if(t)throw r}}}},{key:"extractRawSelector",value:function(e,t){var r=new Set,n=!0,o=!1,i=void 0;try{for(var a,s=e[Symbol.iterator]();!(n=(a=s.next()).done);n=!0){var l=a.value,u=l.raw,c=l.extension,f=this.getFileExtractor("."+c,t);r=new Set([].concat(toConsumableArray(r),toConsumableArray(this.extractSelectors(u,f))))}}catch(e){o=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(o)throw i}}return r}},{key:"extractFileSelector",value:function(e,t){var r=new Set,n=!0,o=!1,i=void 0;try{for(var a,s=e[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"),E=this.getFileExtractor(v,t);r=new Set([].concat(toConsumableArray(r),toConsumableArray(this.extractSelectors(d,E))))}}catch(e){f=!0,y=e}finally{try{!c&&p.return&&p.return()}finally{if(f)throw y}}}}catch(e){o=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(o)throw i}}return r}},{key:"getFileExtractor",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];return t.length?t.find(function(t){return t.extensions.find(function(t){return e.endsWith(t)})}).extractor:!0===this.options.legacy?LegacyExtractor:DefaultExtractor}},{key:"extractSelectors",value:function(e,t){var r=new Set,n=t.extract(e);if(null===n)throw new Error(ERROR_EXTRACTER_FAILED);return n.forEach(function(e){r.add(e)}),r.delete(""),r}},{key:"getSelectorsCss",value:function(e){var t=this;this.root.walk(function(r){return"rule"===r.type?t.evaluateRule(r,e):"atrule"===r.type?t.evaluateAtRule(r):void 0})}},{key:"evaluateRule",value:function(e,t){var r=this,n=e.prev();if(!this.isIgnoreAnnotation(n)){var o=!0;if(e.selector=selectorParser(function(e){e.walk(function(e){var n=[];if("selector"===e.type){if(e.parent&&":not"===e.parent.value&&"pseudo"===e.parent.type)return;var i=!0,a=!1,s=void 0;try{for(var l,u=e.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?n.push(y):"tag"!==f||/[+]|(even)|(odd)|^from$|^to$|^\d/.test(y)||n.push(y)}}catch(e){a=!0,s=e}finally{try{!i&&u.return&&u.return()}finally{if(a)throw s}}(o=r.shouldKeepSelector(t,n))||e.remove()}})}).processSync(e.selector),o){var i=!0,a=!1,s=void 0;try{for(var l,u=e.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,E=y.split(" ")[Symbol.iterator]();!(h=(d=E.next()).done);h=!0){var R=d.value;this.usedAnimations.add(R)}}catch(e){p=!0,v=e}finally{try{!h&&E.return&&E.return()}finally{if(p)throw v}}}this.options.fontFace&&"font-family"===f&&this.usedFontFaces.add(y)}}catch(e){a=!0,s=e}finally{try{!i&&u.return&&u.return()}finally{if(a)throw s}}}var m=e.parent;e.selector||e.remove(),this.isRuleEmpty(m)&&m.remove()}}},{key:"evaluateAtRule",value:function(e){if(this.options.keyframes&&e.name.endsWith("keyframes"))this.atRules.keyframes.push(e);else if(this.options.fontFace&&"font-face"===e.name){var t=!0,r=!1,n=void 0;try{for(var o,i=e.nodes[Symbol.iterator]();!(t=(o=i.next()).done);t=!0){var a=o.value,s=a.prop,l=a.value;"font-family"===s&&this.atRules.fontFace.push({name:l,node:e})}}catch(e){r=!0,n=e}finally{try{!t&&i.return&&i.return()}finally{if(r)throw n}}}else;}},{key:"isIgnoreAnnotation",value:function(e){return!(!e||"comment"!==e.type)&&e.text.includes(IGNORE_ANNOTATION)}},{key:"isRuleEmpty",value:function(e){return!!("decl"===e.type&&!e.value||"rule"===e.type&&!e.selector||e.nodes&&!e.nodes.length||"atrule"===e.type&&(!e.nodes&&!e.params||!e.params&&!e.nodes.length))}},{key:"shouldKeepSelector",value:function(e,t){var r=!0,n=!1,o=void 0;try{for(var i,a=t[Symbol.iterator]();!(r=(i=a.next()).done);r=!0){var s=i.value;if(this.options.legacy){var l=s.split(/[^a-z]/g),u=!1,c=!0,f=!1,y=void 0;try{for(var h,p=l[Symbol.iterator]();!(c=(h=p.next()).done);c=!0){var v=h.value;if(v){if(!e.has(v))break;u=!0}}}catch(e){f=!0,y=e}finally{try{!c&&p.return&&p.return()}finally{if(f)throw y}}if(u)return!0;if(e.has(s)||CSS_WHITELIST.includes(s)||this.isSelectorWhitelisted(s))return!0}else{var d=s.replace(/\\/g,"");if(d.startsWith(":"))continue;if(!(e.has(d)||CSS_WHITELIST.includes(d)||this.isSelectorWhitelisted(d)))return!1}}}catch(e){n=!0,o=e}finally{try{!r&&a.return&&a.return()}finally{if(n)throw o}}return!this.options.legacy}},{key:"isSelectorWhitelisted",value:function(e){return!!(CSS_WHITELIST.includes(e)||this.options.whitelist&&this.options.whitelist.some(function(t){return t===e})||this.options.whitelistPatterns&&this.options.whitelistPatterns.some(function(t){return t.test(e)}))}}]),e}();module.exports=Purgecss; | ||
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var fs=_interopDefault(require("fs")),glob=_interopDefault(require("glob")),postcss=_interopDefault(require("postcss")),selectorParser=_interopDefault(require("postcss-selector-parser"));function normalizeArray(e,t){for(var r=0,n=e.length-1;n>=0;n--){var o=e[n];"."===o?e.splice(n,1):".."===o?(e.splice(n,1),r++):r&&(e.splice(n,1),r--)}if(t)for(;r--;r)e.unshift("..");return e}var splitPathRe=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,splitPath=function(e){return splitPathRe.exec(e).slice(1)};function resolve(){for(var e="",t=!1,r=arguments.length-1;r>=-1&&!t;r--){var n=r>=0?arguments[r]:"/";if("string"!=typeof n)throw new TypeError("Arguments to path.resolve must be strings");n&&(e=n+"/"+e,t="/"===n.charAt(0))}return e=normalizeArray(filter(e.split("/"),function(e){return!!e}),!t).join("/"),(t?"/":"")+e||"."}function normalize(e){var t=isAbsolute(e),r="/"===substr(e,-1);return(e=normalizeArray(filter(e.split("/"),function(e){return!!e}),!t).join("/"))||t||(e="."),e&&r&&(e+="/"),(t?"/":"")+e}function isAbsolute(e){return"/"===e.charAt(0)}function join(){return normalize(filter(Array.prototype.slice.call(arguments,0),function(e,t){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e}).join("/"))}function relative(e,t){function r(e){for(var t=0;t<e.length&&""===e[t];t++);for(var r=e.length-1;r>=0&&""===e[r];r--);return t>r?[]:e.slice(t,r-t+1)}e=resolve(e).substr(1),t=resolve(t).substr(1);for(var n=r(e.split("/")),o=r(t.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(e){var t=splitPath(e),r=t[0],n=t[1];return r||n?(n&&(n=n.substr(0,n.length-1)),r+n):"."}function basename(e,t){var r=splitPath(e)[2];return t&&r.substr(-1*t.length)===t&&(r=r.substr(0,r.length-t.length)),r}function extname(e){return splitPath(e)[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(e,t){if(e.filter)return e.filter(t);for(var r=[],n=0;n<e.length;n++)t(e[n],n,e)&&r.push(e[n]);return r}var substr="b"==="ab".substr(-1)?function(e,t,r){return e.substr(t,r)}:function(e,t,r){return t<0&&(t=e.length+t),e.substr(t,r)},defaultOptions={css:[],content:[],extractors:[],whitelist:[],output:void 0,stdout:!1,info:!1,rejected:!1,legacy:!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",ERROR_INFO_TYPE="Error Type option info: expected a boolean",ERROR_REJECTED_TYPE="Error Type option rejected: 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(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},classCallCheck=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},createClass=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),toConsumableArray=function(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)},DefaultExtractor=function(){function e(){classCallCheck(this,e)}return createClass(e,null,[{key:"extract",value:function(e){return e.match(/[A-Za-z0-9_-]+/g)}}]),e}(),LegacyExtractor=function(){function e(){classCallCheck(this,e)}return createClass(e,null,[{key:"extract",value:function(e){return e.split(/[^a-z]/g)}}]),e}(),Purgecss=function(){function e(t){classCallCheck(this,e),this.atRules={keyframes:[],fontFace:[]},this.usedAnimations=new Set,this.usedFontFaces=new Set,this.selectorsRemoved=new Set,"string"!=typeof t&&void 0!==t||(t=this.loadConfigFile(t)),this.checkOptions(t),this.options=Object.assign(defaultOptions,t)}return createClass(e,[{key:"loadConfigFile",value:function(e){var t=void 0===e?CONFIG_FILENAME:e,r=void 0;try{var n=path.resolve(process.cwd(),t);r=require(n)}catch(e){throw new Error(ERROR_CONFIG_FILE_LOADING+e.message)}return r}},{key:"checkOptions",value:function(e){if("object"!==(void 0===e?"undefined":_typeof(e)))throw new TypeError(ERROR_OPTIONS_TYPE);if(!e.content||!e.content.length)throw new Error(ERROR_MISSING_CONTENT);if(!e.css||!e.css.length)throw new Error(ERROR_MISSING_CSS);if(e.output&&"string"!=typeof e.output)throw new TypeError(ERROR_OUTPUT_TYPE);if(e.extractors&&!Array.isArray(e.extractors))throw new TypeError(ERROR_EXTRACTERS_TYPE);if(e.whitelist&&!Array.isArray(e.whitelist))throw new TypeError(ERROR_WHITELIST_TYPE);if(e.stdout&&"boolean"!=typeof e.stdout)throw new TypeError(ERROR_STDOUT_TYPE);if(e.info&&"boolean"!=typeof e.info)throw new TypeError(ERROR_INFO_TYPE);if(e.rejected&&"boolean"!=typeof e.rejected)throw new TypeError(ERROR_REJECTED_TYPE);if(e.whitelistPatterns&&!Array.isArray(e.whitelistPatterns))throw new TypeError(ERROR_WHITELIST_PATTERNS_TYPE)}},{key:"purge",value:function(){var e=this.options,t=e.content,r=e.extractors,n=e.css,o=t.filter(function(e){return"string"==typeof e}),i=t.filter(function(e){return"object"===(void 0===e?"undefined":_typeof(e))}),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(e,t){var r=[],n=!0,o=!1,i=void 0;try{for(var a,s=e[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(t),this.options.keyframes&&this.removeUnusedKeyframes(),this.options.fontFace&&this.removeUnusedFontFaces(),r.push({file:u,css:this.root.toString()})}}catch(e){o=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(o)throw i}}return r}},{key:"removeUnusedKeyframes",value:function(){var e=!0,t=!1,r=void 0;try{for(var n,o=this.atRules.keyframes[Symbol.iterator]();!(e=(n=o.next()).done);e=!0){var i=n.value,a=i.params;this.usedAnimations.has(a)||i.remove()}}catch(e){t=!0,r=e}finally{try{!e&&o.return&&o.return()}finally{if(t)throw r}}}},{key:"removeUnusedFontFaces",value:function(){var e=!0,t=!1,r=void 0;try{for(var n,o=this.atRules.fontFace[Symbol.iterator]();!(e=(n=o.next()).done);e=!0){var i=n.value,a=i.node,s=i.name;this.usedFontFaces.has(s)||a.remove()}}catch(e){t=!0,r=e}finally{try{!e&&o.return&&o.return()}finally{if(t)throw r}}}},{key:"extractRawSelector",value:function(e,t){var r=new Set,n=!0,o=!1,i=void 0;try{for(var a,s=e[Symbol.iterator]();!(n=(a=s.next()).done);n=!0){var l=a.value,u=l.raw,c=l.extension,f=this.getFileExtractor("."+c,t);r=new Set([].concat(toConsumableArray(r),toConsumableArray(this.extractSelectors(u,f))))}}catch(e){o=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(o)throw i}}return r}},{key:"extractFileSelector",value:function(e,t){var r=new Set,n=!0,o=!1,i=void 0;try{for(var a,s=e[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"),E=this.getFileExtractor(v,t);r=new Set([].concat(toConsumableArray(r),toConsumableArray(this.extractSelectors(d,E))))}}catch(e){f=!0,y=e}finally{try{!c&&p.return&&p.return()}finally{if(f)throw y}}}}catch(e){o=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(o)throw i}}return r}},{key:"getFileExtractor",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];return t.length?t.find(function(t){return t.extensions.find(function(t){return e.endsWith(t)})}).extractor:!0===this.options.legacy?LegacyExtractor:DefaultExtractor}},{key:"extractSelectors",value:function(e,t){var r=new Set,n=t.extract(e);if(null===n)throw new Error(ERROR_EXTRACTER_FAILED);return n.forEach(function(e){r.add(e)}),r.delete(""),r}},{key:"getSelectorsCss",value:function(e){var t=this;this.root.walk(function(r){return"rule"===r.type?t.evaluateRule(r,e):"atrule"===r.type?t.evaluateAtRule(r):void 0})}},{key:"evaluateRule",value:function(e,t){var r=this,n=e.prev();if(!this.isIgnoreAnnotation(n)){var o=!0;if(e.selector=selectorParser(function(e){e.walk(function(e){var n=[];if("selector"===e.type){if(e.parent&&":not"===e.parent.value&&"pseudo"===e.parent.type)return;var i=!0,a=!1,s=void 0;try{for(var l,u=e.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(e){a=!0,s=e}finally{try{!i&&u.return&&u.return()}finally{if(a)throw s}}(o=r.shouldKeepSelector(t,n))||e.remove()}})}).processSync(e.selector),o){var i=!0,a=!1,s=void 0;try{for(var l,u=e.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,E=y.split(" ")[Symbol.iterator]();!(h=(d=E.next()).done);h=!0){var R=d.value;this.usedAnimations.add(R)}}catch(e){p=!0,v=e}finally{try{!h&&E.return&&E.return()}finally{if(p)throw v}}}this.options.fontFace&&"font-family"===f&&this.usedFontFaces.add(y)}}catch(e){a=!0,s=e}finally{try{!i&&u.return&&u.return()}finally{if(a)throw s}}}var m=e.parent;e.selector||e.remove(),this.isRuleEmpty(m)&&m.remove()}}},{key:"evaluateAtRule",value:function(e){if(this.options.keyframes&&e.name.endsWith("keyframes"))this.atRules.keyframes.push(e);else if(this.options.fontFace&&"font-face"===e.name){var t=!0,r=!1,n=void 0;try{for(var o,i=e.nodes[Symbol.iterator]();!(t=(o=i.next()).done);t=!0){var a=o.value,s=a.prop,l=a.value;"font-family"===s&&this.atRules.fontFace.push({name:l,node:e})}}catch(e){r=!0,n=e}finally{try{!t&&i.return&&i.return()}finally{if(r)throw n}}}else;}},{key:"isIgnoreAnnotation",value:function(e){return!(!e||"comment"!==e.type)&&e.text.includes(IGNORE_ANNOTATION)}},{key:"isRuleEmpty",value:function(e){return!!("decl"===e.type&&!e.value||"rule"===e.type&&!e.selector||e.nodes&&!e.nodes.length||"atrule"===e.type&&(!e.nodes&&!e.params||!e.params&&!e.nodes.length))}},{key:"shouldKeepSelector",value:function(e,t){var r=!0,n=!1,o=void 0;try{for(var i,a=t[Symbol.iterator]();!(r=(i=a.next()).done);r=!0){var s=i.value;if(this.options.legacy){var l=s.split(/[^a-z]/g),u=!1,c=!0,f=!1,y=void 0;try{for(var h,p=l[Symbol.iterator]();!(c=(h=p.next()).done);c=!0){var v=h.value;if(this.isSelectorWhitelistedChildren(v)){u=!0;break}if(v){if(!e.has(v))break;u=!0}}}catch(e){f=!0,y=e}finally{try{!c&&p.return&&p.return()}finally{if(f)throw y}}if(u)return!0;if(e.has(s)||CSS_WHITELIST.includes(s)||this.isSelectorWhitelisted(s))return!0}else{var d=s.replace(/\\/g,"");if(d.startsWith(":"))continue;if(this.isSelectorWhitelistedChildren(d))return!0;if(!(e.has(d)||CSS_WHITELIST.includes(d)||this.isSelectorWhitelisted(d)))return!1}}}catch(e){n=!0,o=e}finally{try{!r&&a.return&&a.return()}finally{if(n)throw o}}return!this.options.legacy}},{key:"isSelectorWhitelisted",value:function(e){return!!(CSS_WHITELIST.includes(e)||this.options.whitelist&&this.options.whitelist.some(function(t){return t===e})||this.options.whitelistPatterns&&this.options.whitelistPatterns.some(function(t){return t.test(e)}))}},{key:"isSelectorWhitelistedChildren",value:function(e){return!(!this.options.whitelistPatternsChildren||!this.options.whitelistPatternsChildren.some(function(t){return t.test(e)}))}}]),e}();module.exports=Purgecss; |
{ | ||
"name": "purgecss", | ||
"version": "0.20.1", | ||
"version": "0.21.0", | ||
"description": "Remove unused css selectors.", | ||
@@ -49,3 +49,3 @@ "main": "./lib/purgecss.js", | ||
"glob": "^7.1.2", | ||
"postcss": "^6.0.19", | ||
"postcss": "^6.0.20", | ||
"postcss-selector-parser": "^3.1.1", | ||
@@ -60,14 +60,14 @@ "yargs": "^11.1.0" | ||
"babel-preset-flow": "^6.23.0", | ||
"codacy-coverage": "^2.1.0", | ||
"eslint": "^4.18.2", | ||
"codacy-coverage": "^2.1.1", | ||
"eslint": "^4.19.0", | ||
"eslint-plugin-flowtype": "^2.46.1", | ||
"flow-bin": "^0.67.1", | ||
"flow-bin": "^0.68.0", | ||
"jest": "^22.4.2", | ||
"prettier": "^1.11.1", | ||
"regenerator-runtime": "^0.11.1", | ||
"rollup": "^0.56.4", | ||
"rollup": "^0.57.1", | ||
"rollup-plugin-babel": "^3.0.3", | ||
"rollup-plugin-flow": "^1.1.1", | ||
"rollup-plugin-node-builtins": "^2.1.2", | ||
"rollup-plugin-node-resolve": "^3.0.3", | ||
"rollup-plugin-node-resolve": "^3.3.0", | ||
"rollup-plugin-uglify": "^3.0.0", | ||
@@ -74,0 +74,0 @@ "rollup-watch": "^4.3.1" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38377
191
1
Updatedpostcss@^6.0.20