mirror of
https://github.com/owncast/owncast.git
synced 2024-10-10 19:16:02 +00:00
2 lines
8.8 KiB
JavaScript
Executable File
2 lines
8.8 KiB
JavaScript
Executable File
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=require("postcss"),s=e(require("postcss-selector-parser")),r=require("fs"),i=require("util"),n=e(require("glob")),a=e(require("path"));const o={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:[]},c=["*","::-webkit-scrollbar","::selection",":root","::before","::after"];class u{constructor(e){this.nodes=[],this.isUsed=!1,this.value=e}}class l{constructor(){this.nodes=new Map,this.usedVariables=new Set}addVariable(e){const{prop:t}=e;if(!this.nodes.has(t)){const s=new u(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]of this.nodes)e.isUsed||e.value.remove()}}function h(e,t){t&&t.forEach(e.add,e)}class d{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)?h(this.undetermined,e):e instanceof d?(h(this.undetermined,e.undetermined),h(this.attrNames,e.attrNames),h(this.attrValues,e.attrValues),h(this.classes,e.classes),h(this.ids,e.ids),h(this.tags,e.tags)):(h(this.undetermined,e.undetermined),e.attributes&&(h(this.attrNames,e.attributes.names),h(this.attrValues,e.attributes.values)),h(this.classes,e.classes),h(this.ids,e.ids),h(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 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 f={access:i.promisify(r.access),readFile:i.promisify(r.readFile)};async function p(e="purgecss.config.js"){let t;try{const s=a.join(process.cwd(),e);t=await new Promise((function(e){e(function(e){if(e&&e.__esModule)return e;var t={};return e&&Object.keys(e).forEach((function(s){var r=Object.getOwnPropertyDescriptor(e,s);Object.defineProperty(t,s,r.get?r:{enumerable:!0,get:function(){return e[s]}})})),t.default=e,t}(require(s)))}))}catch(e){throw new Error(`Error loading the config file ${e.message}`)}return{...o,...t}}async function m(e,t){return new d(await t(e))}function g(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){const t=new d([]);return e.forEach(t.merge,t),t}function y(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 S(e,t){return t.hasId(e.value)}function b(e,t){return t.hasTag(e.value)}class x{constructor(){this.ignore=!1,this.atRules={fontFace:[],keyframes:[]},this.usedAnimations=new Set,this.usedFontFaces=new Set,this.selectorsRemoved=new Set,this.variablesStructure=new l,this.options=o}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=arguments,r=Array.prototype.slice.call(t,0,-2);return r.input=t[t.length-1],r.index=t[t.length-2],s.push(r),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=y(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){const s=new d([]);for(const i of e){let e=[];try{await f.access(i,r.constants.F_OK),e.push(i)}catch(t){e=n.sync(i,{nodir:!0})}for(const r of e){const e=await f.readFile(r,"utf-8"),i=this.getFileExtractor(r,t),n=await m(e,i);s.merge(n)}}return s}async extractSelectorsFromString(e,t){const s=new d([]);for(const{raw:r,extension:i}of e){const e=this.getFileExtractor(`.${i}`,t),n=await m(r,e);s.merge(n)}return s}evaluateAtRule(e){if(this.options.keyframes&&e.name.endsWith("keyframes"))this.atRules.keyframes.push(e);else if(this.options.fontFace&&"font-face"===e.name&&e.nodes)for(const t of e.nodes)"decl"===t.type&&"font-family"===t.prop&&this.atRules.fontFace.push({name:y(t.value),node:e})}async evaluateRule(e,t){if(this.ignore)return;const r=e.prev();if(r&&"comment"===r.type&&g(r,"next"))return void r.remove();if(e.parent&&"atrule"===e.parent.type&&"keyframes"===e.parent.name)return;if("rule"!==e.type)return;if(function(e){let t=!1;return e.walkComments(e=>{e&&"comment"===e.type&&e.text.includes("purgecss ignore current")&&(t=!0,e.remove())}),t}(e))return;let i=!0;if(e.selector=s(e=>{e.walk(e=>{"selector"===e.type&&(i=this.shouldKeepSelector(e,t),i||(this.options.rejected&&this.selectorsRemoved.add(e.toString()),e.remove()))})}).processSync(e.selector),i&&void 0!==e.nodes)for(const t of e.nodes)"decl"===t.type&&this.collectDeclarationsData(t);const n=e.parent;e.selector||e.remove(),function(e){return!!("rule"===e.type&&!e.selector||e.nodes&&!e.nodes.length||"atrule"===e.type&&(!e.nodes&&!e.params||!e.params&&e.nodes&&!e.nodes.length))}(n)&&n.remove()}async getPurgedCSS(e,s){const r=[],i=[];for(const t of e)"string"==typeof t?i.push(...n.sync(t,{nodir:!0})):i.push(t);for(const e of i){const i="string"==typeof e?this.options.stdin?e:await f.readFile(e,"utf-8"):e.raw,n=t.parse(i);this.walkThroughCSS(n,s),this.options.fontFace&&this.removeUnusedFontFaces(),this.options.keyframes&&this.removeUnusedKeyframes(),this.options.variables&&this.removeUnusedCSSVariables();const a={css:n.toString(),file:"string"==typeof e?e:void 0};"string"==typeof e&&(a.file=e),this.options.rejected&&(a.rejected=Array.from(this.selectorsRemoved),this.selectorsRemoved.clear()),r.push(a)}return r}isSelectorWhitelisted(e){return c.includes(e)||this.options.whitelist&&this.options.whitelist.some(t=>t===e)||this.options.whitelistPatterns&&this.options.whitelistPatterns.some(t=>t.test(e))}isSelectorWhitelistedChildren(e){return this.options.whitelistPatternsChildren&&this.options.whitelistPatternsChildren.some(t=>t.test(e))}async purge(e){this.options="object"!=typeof e?await p(e):{...o,...e};const{content:t,css:s,extractors:r}=this.options,i=t.filter(e=>"string"==typeof e),n=t.filter(e=>"object"==typeof e),a=await this.extractSelectorsFromFiles(i,r),c=await this.extractSelectorsFromString(n,r);return this.getPurgedCSS(s,v(a,c))}removeUnusedCSSVariables(){this.variablesStructure.removeUnused()}removeUnusedFontFaces(){for(const{name:e,node:t}of this.atRules.fontFace)this.usedFontFaces.has(e)||t.remove()}removeUnusedKeyframes(){for(const e of this.atRules.keyframes)this.usedAnimations.has(e.params)||e.remove()}shouldKeepSelector(e,t){if(function(e){return e.parent&&"pseudo"===e.parent.type&&e.parent.value.startsWith(":")||!1}(e))return!0;let s=!1;for(const i of e.nodes){const e="attribute"===i.type&&i.attribute||i.value;if(e&&this.isSelectorWhitelistedChildren(e))return!0;if(e&&(c.includes(e)||this.isSelectorWhitelisted(e)))s=!0;else{switch(i.type){case"attribute":s=!!["value","checked","selected","open"].includes(i.attribute)||w(i,t);break;case"class":r=i,s=t.hasClass(r.value);break;case"id":s=S(i,t);break;case"tag":s=b(i,t)}if(!s)return!1}}var r;return s}walkThroughCSS(e,t){e.walk(e=>"rule"===e.type?this.evaluateRule(e,t):"atrule"===e.type?this.evaluateAtRule(e):void("comment"===e.type&&(g(e,"start")?(this.ignore=!0,e.remove()):g(e,"end")&&(this.ignore=!1,e.remove()))))}}exports.PurgeCSS=x,exports.default=x,exports.defaultOptions=o,exports.mergeExtractorSelectors=v,exports.setOptions=p;
|