2 lines
9.6 KiB
JavaScript
2 lines
9.6 KiB
JavaScript
|
import{access as e,readFile as t,constants as s}from"fs";import r from"glob";import i from"path";import{parse as a}from"postcss";import n from"postcss-selector-parser";import{promisify as o}from"util";function c(e,t){t&&t.forEach(e.add,e)}class l{constructor(e){this.undetermined=new Set,this.attrNames=new Set,this.attrValues=new Set,this.classes=new Set,this.ids=new Set,this.tags=new Set,this.merge(e)}merge(e){return Array.isArray(e)?c(this.undetermined,e):e instanceof l?(c(this.undetermined,e.undetermined),c(this.attrNames,e.attrNames),c(this.attrValues,e.attrValues),c(this.classes,e.classes),c(this.ids,e.ids),c(this.tags,e.tags)):(c(this.undetermined,e.undetermined),e.attributes&&(c(this.attrNames,e.attributes.names),c(this.attrValues,e.attributes.values)),c(this.classes,e.classes),c(this.ids,e.ids),c(this.tags,e.tags)),this}hasAttrName(e){return this.attrNames.has(e)||this.undetermined.has(e)}someAttrValue(e){for(const t of this.attrValues)if(e(t))return!0;for(const t of this.undetermined)if(e(t))return!0;return!1}hasAttrPrefix(e){return this.someAttrValue((t=>t.startsWith(e)))}hasAttrSuffix(e){return this.someAttrValue((t=>t.endsWith(e)))}hasAttrSubstr(e){return e.trim().split(" ").every((e=>this.someAttrValue((t=>t.includes(e)))))}hasAttrValue(e){return this.attrValues.has(e)||this.undetermined.has(e)}hasClass(e){return this.classes.has(e)||this.undetermined.has(e)}hasId(e){return this.ids.has(e)||this.undetermined.has(e)}hasTag(e){return this.tags.has(e)||this.undetermined.has(e)}}const u=["*","::-webkit-scrollbar","::selection",":root","::before","::after"],h={css:[],content:[],defaultExtractor:e=>e.match(/[A-Za-z0-9_-]+/g)||[],extractors:[],fontFace:!1,keyframes:!1,rejected:!1,stdin:!1,stdout:!1,variables:!1,safelist:{standard:[],deep:[],greedy:[],variables:[],keyframes:[]},blocklist:[],skippedContentGlobs:[],dynamicAttributes:[]};class d{constructor(e){this.nodes=[],this.isUsed=!1,this.value=e}}class f{constructor(){this.nodes=new Map,this.usedVariables=new Set,this.safelist=[]}addVariable(e){const{prop:t}=e;if(!this.nodes.has(t)){const s=new d(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);null==r||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&&!e.isUsed&&(e.isUsed=!0,t.push(...e.nodes))}}removeUnused(){for(const e of this.usedVariables)this.setAsUsed(e);for(const[e,t]of this.nodes)t.isUsed||this.isVariablesSafelisted(e)||t.value.remove()}isVariablesSafelisted(e){return this.safelist.some((t=>"string"==typeof t?t===e:t.test(e)))}}const p={access:o(e),readFile:o(t)};function m(e=[]){return Array.isArray(e)?{...h.safelist,standard:e}:{...h.safelist,...e}}async function g(e="purgecss.config.js"){let t;try{const s=i.join(process.cwd(),e);t=await import(s)}catch(e){throw new Error("Error loading the config file "+e.message)}return{...h,...t,safelist:m(t.safelist)}}async function v(e,t){return new l(await t(e))}function y(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 S(...e){const t=new l([]);return e.forEach(t.merge,t),t}function b(e){return e.replace(/(^["'])|(["']$)/g,"")}function w(e,t){if(!t.hasAttrName(e.attribute))return!1;if(void 0===e.value)return!0;switch(e.operator){case"$=":return t.hasAttrSuffix(e.value);case"~=":case"*=":return t.hasAttrSubstr(e.value);case"=":return t.hasAttrValue(e.value);case"|=":case"^=":return t.hasAttrPrefix(e.value);default:return!0}}function A(e,t){return t.hasId(e.value)}function F(e,t){return t.hasTag(e.value)}function k(e){return"atrule"===(null==e?void 0:e.type)}function V(e){return"rule"===(null==e?void 0:e.type)}class x{constructor(){this.ignore=!1,this.atRules={fontFace:[],keyframes:[]},this.usedAnimations=new Set,this.usedFontFaces=new Set,this.selectorsRemoved=new Set,this.
|