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.
 
 
 

1 lines
7.2 KiB

"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);function u(e){const t=e.selector?e:e.parent;return/(!\s*)?postcss-custom-properties:\s*off\b/i.test(t.toString())}function l(e,t){const r=new Map,o=new Map,s=new Map;e.nodes.slice().forEach((e=>{const s=m(e)?r:d(e)?o:null;s&&(e.nodes.slice().forEach((e=>{if(e.variable&&!u(e)){const{prop:r}=e;s.set(r,a.default(e.value)),t.preserve||e.remove()}})),t.preserve||!w(e)||u(e)||e.remove())}));for(const[e,t]of r.entries())s.set(e,t);for(const[e,t]of o.entries())s.set(e,t);return s}const p=/^html$/i,f=/^:root$/i,m=e=>"rule"===e.type&&e.selector.split(",").some((e=>p.test(e)))&&Object(e.nodes).length,d=e=>"rule"===e.type&&e.selector.split(",").some((e=>f.test(e)))&&Object(e.nodes).length,w=e=>0===Object(e.nodes).length;function v(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 y(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 v("default"in t?t.default:t)}async function h(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 l(o.parse(t,{from:e.toString()}),{preserve:!0})}(e.from);if("js"===e.type||"cjs"===e.type)return await y(e.from);if("mjs"===e.type)return await y(e.from);if("json"===e.type)return await async function(e){return v(await g(e))}(e.from);throw new Error("Invalid source type: "+e.type)}return v(e)}))),n=new Map;return r.forEach((e=>{for(const[t,r]of e.entries())n.set(t,r)})),n}const g=async e=>JSON.parse((await s.promises.readFile(e)).toString());function b(e,t){return e.nodes&&e.nodes.length&&e.nodes.slice().forEach((r=>{if(O(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),b(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]))),b(e,t))}else b(r,t)})),e.toString()}const j=/^var$/i,O=e=>"function"===e.type&&j.test(e.value)&&Object(e.nodes).length>0;var $=(e,t,r)=>{if(P(e)&&!function(e){const t=e.prev();return Boolean(u(e)||t&&"comment"===t.type&&/(!\s*)?postcss-custom-properties:\s*ignore\s+next\b/i.test(t.text))}(e)){const o=e.value;let s=b(a.default(o),t);const n=new Set;for(;s.includes("--")&&s.includes("var(")&&!n.has(s);){n.add(s);s=b(a.default(s),t)}if(s!==o)if(r.preserve){const t=e.cloneBefore({value:s});x(t)&&(t.raws.value.value=t.value.replace(F,"$1"),t.raws.value.raw=t.raws.value.value+t.raws.value.raw.replace(F,"$2"))}else e.value=s,x(e)&&(e.raws.value.value=e.value.replace(F,"$1"),e.raws.value.raw=e.raws.value.value+e.raws.value.raw.replace(F,"$2"))}};const P=e=>!e.variable&&e.value.includes("--")&&e.value.includes("var("),x=e=>"value"in Object(Object(e.raws).value)&&"raw"in e.raws.value&&F.test(e.raws.value.raw),F=/^([\W\w]+)(\s*\/\*[\W\w]+?\*\/)$/;async function S(e,t,r){"css"===t&&await async function(e,t){const r=`:root {\n${Object.keys(t).reduce(((e,r)=>(e.push(`\t${r}: ${t[r]};`),e)),[]).join("\n")}\n}\n`;await s.promises.writeFile(e,r)}(e,r),"scss"===t&&await async function(e,t){const r=`${Object.keys(t).reduce(((e,r)=>{const o=r.replace("--","$");return e.push(`${o}: ${t[r]};`),e}),[]).join("\n")}\n`;await s.promises.writeFile(e,r)}(e,r),"js"===t&&await async function(e,t){const r=`module.exports = {\n\tcustomProperties: {\n${Object.keys(t).reduce(((e,r)=>(e.push(`\t\t'${E(r)}': '${E(t[r])}'`),e)),[]).join(",\n")}\n\t}\n};\n`;await s.promises.writeFile(e,r)}(e,r),"json"===t&&await async function(e,t){const r=`${JSON.stringify({"custom-properties":t},null," ")}\n`;await s.promises.writeFile(e,r)}(e,r),"mjs"===t&&await async function(e,t){const r=`export const customProperties = {\n${Object.keys(t).reduce(((e,r)=>(e.push(`\t'${E(r)}': '${E(t[r])}'`),e)),[]).join(",\n")}\n};\n`;await s.promises.writeFile(e,r)}(e,r)}function k(e){const t={};for(const[r,o]of e.entries())t[r]=o.toString();return t}const E=e=>e.replace(/\\([\s\S])|(')/g,"\\$1$2").replace(/\n/g,"\\n").replace(/\r/g,"\\r"),M=e=>{const t=!("preserve"in Object(e))||Boolean(e.preserve),r="overrideImportFromWithRoot"in Object(e)&&Boolean(e.overrideImportFromWithRoot),o="disableDeprecationNotice"in Object(e)&&Boolean(e.disableDeprecationNotice);let s=[];Array.isArray(null==e?void 0:e.importFrom)?s=e.importFrom:null!=e&&e.importFrom&&(s=[e.importFrom]);let n=[];Array.isArray(null==e?void 0:e.exportTo)?n=e.exportTo:null!=e&&e.exportTo&&(n=[e.exportTo]);const i=h(s),a=0===s.length&&0===n.length;return{postcssPlugin:"postcss-custom-properties",prepare(){let e=new Map;return a?{Once:r=>{e=l(r,{preserve:t})},Declaration:r=>{$(r,e,{preserve:t})},OnceExit:()=>{e.clear()}}:{Once:async o=>{const s=(await i).entries(),a=l(o,{preserve:t}).entries();if(r)for(const[t,r]of[...s,...a])e.set(t,r);else for(const[t,r]of[...a,...s])e.set(t,r);await function(e,t){return Promise.all(t.map((async t=>{if(t instanceof Function)return void await t(k(e));if("string"==typeof t){const r=c.default.resolve(t),o=c.default.extname(r).slice(1).toLowerCase();return void await S(r,o,k(e))}let r={};if(r="toJSON"in t?t.toJSON(k(e)):k(e),"to"in t){const e=c.default.resolve(t.to);let o=t.type;return o||(o=c.default.extname(e).slice(1).toLowerCase()),void await S(e,o,r)}"customProperties"in t?t.customProperties=r:"custom-properties"in t&&(t["custom-properties"]=r)})))}(e,n)},Declaration:r=>{$(r,e,{preserve:t})},OnceExit:(t,{result:r})=>{!o&&(s.length>0||n.length>0)&&t.warn(r,'"importFrom" and "exportTo" will be removed in a future version of postcss-custom-properties.\nWe are looking for insights and anecdotes on how these features are used so that we can design the best alternative.\nPlease let us know if our proposal will work for you.\nVisit the discussion on github for more details. https://github.com/csstools/postcss-plugins/discussions/192'),e.clear()}}}}};M.postcss=!0,module.exports=M;