You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2 lines
6.5 KiB

2 years ago
import e from"postcss-value-parser";import t from"path";import{pathToFileURL as o}from"url";import{parse as r}from"postcss";import{promises as s}from"fs";function n(e){const t=e.selector?e:e.parent;return/(!\s*)?postcss-custom-properties:\s*off\b/i.test(t.toString())}function i(t,o){const r=new Map,s=new Map,i=new Map;t.nodes.slice().forEach((t=>{const i=l(t)?r:u(t)?s:null;i&&(t.nodes.slice().forEach((t=>{if(t.variable&&!n(t)){const{prop:r}=t;i.set(r,e(t.value)),o.preserve||t.remove()}})),o.preserve||!p(t)||n(t)||t.remove())}));for(const[e,t]of r.entries())i.set(e,t);for(const[e,t]of s.entries())i.set(e,t);return i}const a=/^html$/i,c=/^:root$/i,l=e=>"rule"===e.type&&e.selector.split(",").some((e=>a.test(e)))&&Object(e.nodes).length,u=e=>"rule"===e.type&&e.selector.split(",").some((e=>c.test(e)))&&Object(e.nodes).length,p=e=>0===Object(e.nodes).length;function f(t){const o=new Map;if("customProperties"in t)for(const[r,s]of Object.entries(t.customProperties))o.set(r,e(s.toString()));if("custom-properties"in t)for(const[r,s]of Object.entries(t["custom-properties"]))o.set(r,e(s.toString()));return o}async function m(e){let t;try{t=await import(e)}catch(r){t=await import(o(e).href)}return f("default"in t?t.default:t)}async function w(e){const o=(await Promise.all(e.map((async e=>{if(e instanceof Promise?e=await e:e instanceof Function&&(e=await e()),"string"==typeof e){const o=t.resolve(e);return{type:t.extname(o).slice(1).toLowerCase(),from:o}}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 o=t.resolve(e.from);let r=e.type;return r||(r=t.extname(o).slice(1).toLowerCase()),{type:r,from:o}}return Object.keys(e).length,null})))).filter((e=>!!e)),n=await Promise.all(o.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.readFile(e);return i(r(t,{from:e.toString()}),{preserve:!0})}(e.from);if("js"===e.type||"cjs"===e.type)return await m(e.from);if("mjs"===e.type)return await m(e.from);if("json"===e.type)return await async function(e){return f(await v(e))}(e.from);throw new Error("Invalid source type: "+e.type)}return f(e)}))),a=new Map;return n.forEach((e=>{for(const[t,o]of e.entries())a.set(t,o)})),a}const v=async e=>JSON.parse((await s.readFile(e)).toString());function d(e,t){return e.nodes&&e.nodes.length&&e.nodes.slice().forEach((o=>{if(h(o)){const[r,...s]=o.nodes.filter((e=>"div"!==e.type)),{value:n}=r,i=e.nodes.indexOf(o);if(t.has(n)){const o=t.get(n).nodes;!function(e,t,o){const r=new Map(t);r.delete(o),d(e,r)}({nodes:o},t,n),i>-1&&e.nodes.splice(i,1,...o)}else s.length&&(i>-1&&e.nodes.splice(i,1,...o.nodes.slice(o.nodes.indexOf(s[0]))),d(e,t))}else d(o,t)})),e.toString()}const y=/^var$/i,h=e=>"function"===e.type&&y.test(e.value)&&Object(e.nodes).length>0;var g=(t,o,r)=>{if(j(t)&&!function(e){const t=e.prev();return Boolean(n(e)||t&&"comment"===t.type&&/(!\s*)?postcss-custom-properties:\s*ignore\s+next\b/i.test(t.text))}(t)){const s=t.value;let n=d(e(s),o);const i=new Set;for(;n.includes("--")&&n.includes("var(")&&!i.has(n);){i.add(n);n=d(e(n),o)}if(n!==s)if(r.preserve){const e=t.cloneBefore({value:n});b(e)&&(e.raws.value.value=e.value.replace(O,"$1"),e.raws.value.raw=e.raws.value.value+e.raws.value.raw.replace(O,"$2"))}else t.value=n,b(t)&&(t.raws.value.value=t.value.replace(O,"$1"),t.raws.value.raw=t.raws.value.value+t.raws.value.raw.replace(O,"$2"))}};const j=e=>!e.variable&&e.value.includes("--")&&e.value.includes("var("),b=e=>"value"in Object(Object(e.raws).value)&&"raw"in e.raws.value&&O.test(e.raws.value.raw),O=/^([\W\w]+)(\s*\/\*[\W\w]+?\*\/)$/;async function $(e,t,o){"css"===t&&await async function(e,t){const o=`:root {\n${Object.keys(t).reduce(((e,o)=>(e.push(`\t${o}: ${t[o]};`),e)),[]).join("\n")}\n}\n`;await s.writeFile(e,o)}(e,o),"scss"===t&&await async function(e,t){const o=`${Object.keys(t).reduce(((e,o)=>{const r=o.replace("--","$");return e.push(`${r}: ${t[o]};`),e}),[]).join("\n")}\n`;a