2 lines
7.6 KiB
JavaScript
2 lines
7.6 KiB
JavaScript
|
"use strict";var e=require("postcss-value-parser"),t=require("path"),r=require("url"),o=require("postcss"),s=require("fs");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function i(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var o=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,o.get?o:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var a=n(e),c=n(t);const u=/(!\s*)?postcss-custom-properties:\s*off\b/i,l=new WeakMap;function p(e){if(!e||!e.nodes)return!1;if(l.has(e))return l.get(e);const t=e.some((e=>d(e,u)));return l.set(e,t),t}const f=/(!\s*)?postcss-custom-properties:\s*ignore\s+next\b/i;function m(e){return!!e&&(!!p(e.parent)||d(e.prev(),f))}function d(e,t){return e&&"comment"===e.type&&t.test(e.text)}function w(e,t){const r=new Map,o=new Map;e.nodes.slice().forEach((e=>{if(p(e))return;const s=h(e)?r:g(e)?o:null;s&&(e.nodes.slice().forEach((e=>{if(e.variable&&!m(e)){const{prop:r}=e;s.set(r,e.value),t.preserve||e.remove()}})),!t.preserve&&b(e)&&e.remove())}));const s=new Map;for(const[e,t]of r.entries())s.set(e,a.default(t));for(const[e,t]of o.entries())s.set(e,a.default(t));return s}const v=/^html$/i,y=/^:root$/i,h=e=>"rule"===e.type&&e.selector.split(",").some((e=>v.test(e)))&&Object(e.nodes).length,g=e=>"rule"===e.type&&e.selector.split(",").some((e=>y.test(e)))&&Object(e.nodes).length,b=e=>0===Object(e.nodes).length;function j(e){const t=new Map;if("customProperties"in e)for(const[r,o]of Object.entries(e.customProperties))t.set(r,a.default(o.toString()));if("custom-properties"in e)for(const[r,o]of Object.entries(e["custom-properties"]))t.set(r,a.default(o.toString()));return t}async function O(e){let t;try{t=await(o=e,Promise.resolve().then((function(){return i(require(o))})))}catch(o){t=await function(e){return Promise.resolve().then((function(){return i(require(e))}))}(r.pathToFileURL(e).href)}var o;return j("default"in t?t.default:t)}async function $(e){const t=(await Promise.all(e.map((async e=>{if(e instanceof Promise?e=await e:e instanceof Function&&(e=await e()),"string"==typeof e){const t=c.default.resolve(e);return{type:c.default.extname(t).slice(1).toLowerCase(),from:t}}if("customProperties"in e&&Object(e.customProperties)===e.customProperties)return e;if("custom-properties"in e&&Object(e["custom-properties"])===e["custom-properties"])return e;if("from"in e){const t=c.default.resolve(e.from);let r=e.type;return r||(r=c.default.extname(t).slice(1).toLowerCase()),{type:r,from:t}}return Object.keys(e).length,null})))).filter((e=>!!e)),r=await Promise.all(t.map((async e=>{if("type"in e&&"from"in e){if("css"===e.type||"pcss"===e.type)return await async function(e){const t=await s.promises.readFile(e);return w(o.parse(t,{from:e.toString()}),{preserve:!0})}(e.from);if("js"===e.type||"cjs"===e.type)return await O(e.from);if("mjs"===e.type)return await O(e.from);if("json"===e.type)return await async function(e){return j(await x(e))}(e.from);throw new Error("Invalid source type: "+e.type)}return j(e)}))),n=new Map;return r.forEach((e=>{for(const[t,r]of e.entries())n.set(t,r)})),n}const x=async e=>JSON.parse((await s.promises.readFile(e)).toString());function P(e,t){return e.nodes&&e.nodes.length&&e.nodes.slice().forEach((r=>{if(S(r)){const[o,...s]=r.nodes.filter((e=>"div"!==e.type)),{value:n}=o,i=e.nodes.indexOf(r);if(t.has(n)){const r=t.get(n).nodes;!function(e,t,r){const o=new Map(t);o.delete(r),P(e,o)}({nodes:r},t,n),i>-1&&e.nodes.splice(i,1,...r)}else s.length&&(i>-1&&e.nodes.splice(i,1,...r.nodes.slice(r.nodes.indexOf(s[0]))),P(e,t))}else P(r,t)})),e.toString()}const F=/^var$/i,S=e=>"function"===e.type&&F.test(e.value)&&Object(e.nodes).length>0;var k=(e,t,r)=>{if(M(e)&&!m(e)){const o=e.value;let s=P(a.default(o),t);const n=new Set;for(;s.includes("--")&&s.includes("var(")&&!n.has(s);){n.add(s);s=P(a.default(s),t)}if(s!==o){if(function(e,t){if(!e||!e.parent)return!1;let r=!1;const o=e.parent.index(e);return e.parent.each(((s,n)=>s!==e&&(!(n>=o)&&void("decl"===s.typ
|