Syllabus/themes/piratecare/node_modules/purgecss/lib/purgecss.esm.js

2 lines
7.9 KiB
JavaScript
Raw Normal View History

2020-03-05 04:10:52 +00:00
import{parse as e}from"postcss";import t from"postcss-selector-parser";import{access as s,readFile as r,constants as i}from"fs";import{promisify as n}from"util";import o from"glob";import a from"path";const c={css:[],content:[],defaultExtractor:e=>e.match(/[A-Za-z0-9_-]+/g)||[],extractors:[],fontFace:!1,keyframes:!1,rejected:!1,stdin:!1,stdout:!1,variables:!1,whitelist:[],whitelistPatterns:[],whitelistPatternsChildren:[]},u=["*","::-webkit-scrollbar","::selection",":root","::before","::after"];class l{constructor(e){this.nodes=[],this.isUsed=!1,this.value=e}}class d{constructor(){this.nodes=new Map,this.usedVariables=new Set}addVariable(e){const{prop:t}=e;if(!this.nodes.has(t)){const s=new l(e);this.nodes.set(t,s)}}addVariableUsage(e,t){const{prop:s}=e,r=this.nodes.get(s);for(const e of t){const t=e[1];if(this.nodes.has(t)){const e=this.nodes.get(t);r.nodes.push(e)}}}addVariableUsageInProperties(e){for(const t of e){const e=t[1];this.usedVariables.add(e)}}setAsUsed(e){const t=[this.nodes.get(e)];for(;0!==t.length;){const e=t.pop();e.isUsed||(e.isUsed=!0,t.push(...e.nodes))}}removeUnused(){for(const e of this.usedVariables)this.setAsUsed(e);for(const[,e]of this.nodes)e.isUsed||e.value.remove()}}const h={access:n(s),readFile:n(r)};async function f(e="purgecss.config.js"){let t;try{const s=a.join(process.cwd(),e);t=await import(s)}catch(e){throw new Error(`Error loading the config file ${e.message}`)}return{...c,...t}}function m(e,t){const s=t(e);return Array.isArray(s)?{attributes:{names:[],values:[]},classes:[],ids:[],tags:[],undetermined:s}:s}function p(e,t){switch(t){case"next":return e.text.includes("purgecss ignore");case"start":return e.text.includes("purgecss start ignore");case"end":return e.text.includes("purgecss end ignore")}}function v(e,t){return{attributes:{names:[...e.attributes.names,...t.attributes.names],values:[...e.attributes.values,...t.attributes.values]},classes:[...e.classes,...t.classes],ids:[...e.ids,...t.ids],tags:[...e.tags,...t.tags],undetermined:[...e.undetermined,...t.undetermined]}}function g(e){return e.replace(/(^["'])|(["']$)/g,"")}function y(e,t){if(!t.attributes.names.includes(e.attribute)&&!t.undetermined.includes(e.attribute))return!1;switch(e.operator){case"$=":return t.attributes.values.some(t=>t.endsWith(e.value))||t.undetermined.some(t=>t.endsWith(e.value));case"~=":case"*=":return t.attributes.values.some(t=>t.includes(e.value))||t.undetermined.some(t=>t.includes(e.value));case"=":return t.attributes.values.some(t=>t===e.value)||t.undetermined.some(t=>t===e.value);case"|=":case"^=":return t.attributes.values.some(t=>t.startsWith(e.value))||t.undetermined.some(t=>t.startsWith(e.value));default:return!0}}function b(e,t){return t.ids.includes(e.value)||t.undetermined.includes(e.value)}function w(e,t){return t.tags.includes(e.value)||t.undetermined.includes(e.value)}class S{constructor(){this.ignore=!1,this.atRules={fontFace:[],keyframes:[]},this.usedAnimations=new Set,this.usedFontFaces=new Set,this.selectorsRemoved=new Set,this.variablesStructure=new d,this.options=c}collectDeclarationsData(e){const{prop:t,value:s}=e;if(this.options.variables){const r=function(e,t){const s=[];return e.replace(t,(function(){const t=Array.prototype.slice.call(arguments,0,-2);return t.input=arguments[arguments.length-1],t.index=arguments[arguments.length-2],s.push(t),e})),s}(s,/var\((.+?)[\,)]/g);t.startsWith("--")?(this.variablesStructure.addVariable(e),r.length>0&&this.variablesStructure.addVariableUsage(e,r)):r.length>0&&this.variablesStructure.addVariableUsageInProperties(r)}if(!this.options.keyframes||"animation"!==t&&"animation-name"!==t)if(this.options.fontFace){if("font-family"===t)for(const e of s.split(",")){const t=g(e.trim());this.usedFontFaces.add(t)}}else;else for(const e of s.split(/[\s,]+/))this.usedAnimations.add(e)}getFileExtractor(e,t){const s=t.find(t=>t.extensions.find(t=>e.endsWith(t)));return void 0===s?this.options.defaultExtractor:s.extractor}async extractSelectorsFromFiles(e,t){let s={attributes:{names:[],values:[]},classes:[],ids:[],tags:[],undetermined:[]};for(const r of e