esor
Version:
JavaScript framework for dynamic Web applications based on Web Components
3 lines (2 loc) • 7.07 kB
JavaScript
var M=Object.defineProperty;var O=e=>{throw TypeError(e)};var X=(e,t,o)=>t in e?M(e,t,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[t]=o;var r=(e,t)=>M(e,"name",{value:t,configurable:!0});var C=(e,t,o)=>X(e,typeof t!="symbol"?t+"":t,o),L=(e,t,o)=>t.has(e)||O("Cannot "+o);var _=(e,t,o)=>(L(e,t,"read from private field"),o?o.call(e):t.get(e)),g=(e,t,o)=>t.has(e)?O("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(e):t.set(e,o);var D=(e,t,o)=>(L(e,t,"access private method"),o);var b=null,k=0,A=r(e=>{let t=e,o=new Set;return(...c)=>{if(c.length===0)return b&&o.add(b),t;let s=c[0];if(t!==s&&(t=s,k===0))for(let a of o)a();return t}},"signal"),m=r(e=>{let t=r(()=>{b=t,e(),b=null},"execute");return t(),t},"effect"),w=r(e=>{let t=A(void 0);return m(()=>t(e())),t},"computed"),H=r(e=>{k++;let t=e();return--k,t},"batch");function T(e,t){let o=new Map;for(let s of e.children)s._key!==void 0&&o.set(s._key,s);let c=[];for(let s of t){let a=s._key,n=o.get(a),l=document.createElement("div");d(l,s);let i=l.firstElementChild;n&&i?(j(n,i),c.push(n),o.delete(a)):i&&(i._key=a,c.push(i))}for(let s of o.values())s._cleanup?.(),e.removeChild(s);for(let s=0;s<c.length;s++){let a=c[s],n=e.children[s];n!==a&&e.insertBefore(a,n||null)}}r(T,"reconcileArray");function j(e,t){if(e.nodeType===Node.ELEMENT_NODE&&t.nodeType===Node.ELEMENT_NODE){if(e.tagName!==t.tagName){e.replaceWith(t.cloneNode(!0));return}let o=new Map;for(let{name:n,value:l}of e.attributes)o.set(n,l);for(let{name:n,value:l}of t.attributes)n==="value"||n==="checked"?e[n]!==l&&(e[n]=l):e.getAttribute(n)!==l&&e.setAttribute(n,l),o.delete(n);for(let n of o.keys())e.removeAttribute(n);let c=Array.from(e.childNodes),s=Array.from(t.childNodes),a=Math.max(c.length,s.length);for(let n=0;n<a;n++){let l=c[n],i=s[n];l?i?j(l,i):(l._cleanup?.(),e.removeChild(l)):e.appendChild(i.cloneNode(!0))}}else e.nodeType===Node.TEXT_NODE&&t.nodeType===Node.TEXT_NODE?e.textContent!==t.textContent&&(e.textContent=t.textContent):e.replaceWith(t.cloneNode(!0))}r(j,"patchNode");var x="\uFEFF",W=new WeakMap,F=r((e,...t)=>{let o=W.get(e);if(!o){let l=document.createElement("template");l.innerHTML=e.join(x);let i=e.findIndex(f=>f.trim().endsWith("key="));o={template:l,keyAttrIndex:i},W.set(e,o)}let{template:c,keyAttrIndex:s}=o,a,n=[...t];return s!==-1&&(a=t[s],n.splice(s,1)),{template:c,values:n,_isTemplate:!0,_key:a}},"html"),N=r((e,t,o=!0)=>{if(Array.isArray(t)&&t.length>0&&t[0]?._key!==void 0){T(e,t);return}if(o&&!(e instanceof DocumentFragment))for(;e.firstChild;)e.removeChild(e.firstChild);if(!(t==null||t===!1))if(t._isTemplate)d(e,t);else if(Array.isArray(t))for(let c=0;c<t.length;c++){let s=document.createDocumentFragment();N(s,t[c],!1),e.appendChild(s)}else t instanceof Node?e.appendChild(t):e.appendChild(document.createTextNode(String(t)))},"renderValue"),d=r((e,{template:t,values:o})=>{let c=t.content.cloneNode(!0),s=0,a=r(n=>{if(n.nodeType===Node.TEXT_NODE&&n.nodeValue.includes(x)){let l=n.nodeValue.split(x),i=document.createDocumentFragment();for(let f=0;f<l.length;f++){if(f>0){let u=o[s++];if(typeof u=="function"){let p=document.createElement("span");i.appendChild(p),m(()=>N(p,u()))}else N(i,u,!1)}l[f]&&i.appendChild(document.createTextNode(l[f]))}n.parentNode.replaceChild(i,n)}else if(n.nodeType===Node.ELEMENT_NODE){let l=[];for(let i=0;i<n.attributes.length;i++){let f=n.attributes[i];f.name!=="key"&&f.value===x&&l.push(f)}for(let i=0;i<l.length;i++){let f=l[i],u=o[s++];if(n.removeAttribute(f.name),f.name==="ref")typeof u=="function"?u(n):u&&typeof u=="object"&&(u.current=n);else if(f.name==="style"&&typeof u=="object"&&u!==null)typeof u=="function"?m(()=>Object.assign(n.style,u())):Object.assign(n.style,u);else if(typeof u=="function"&&n.tagName?.includes("-"))n._functionProps||(n._functionProps={}),n._functionProps[f.name]=u;else if(f.name.startsWith("on")){let p=f.name.slice(2).toLowerCase();typeof u=="function"&&(n._cleanup&&(n._cleanup(),n._cleanup=null),n.addEventListener(p,u),n._cleanup=()=>n.removeEventListener(p,u))}else{let p=r(h=>{["value","checked","selected"].includes(f.name)?n[f.name]=h:h==null||h===!1?n.removeAttribute(f.name):n.setAttribute(f.name,h===!0?"":h)},"setAttribute");typeof u=="function"?m(()=>p(u())):p(u)}}n.hasAttribute("key")&&n.removeAttribute("key");for(let i=0;i<n.childNodes.length;i++)a(n.childNodes[i])}},"processNode");for(let n=0;n<c.childNodes.length;n++)a(c.childNodes[n]);e.appendChild(c)},"renderTemplate");var y=null,P=["beforeMount","mount","beforeUpdate","update","destroy"],z=r(e=>{y=e,e._lifecycles=Object.fromEntries(P.map(t=>[t,[]])),e.runHook=t=>{let o=e._lifecycles?.[t];o?.length&&o.forEach(c=>queueMicrotask(()=>c.call(e)))}},"createLifecycle"),S=r((e,t)=>{if(!y?._lifecycles)throw new Error(`[Esor] Hook called outside ctx setup for "${e}"`);y._lifecycles[e].push(t)},"addHook"),U={};P.forEach(e=>{let t=e.startsWith("before")?e:`on${e[0].toUpperCase()}${e.slice(1)}`;U[t]=o=>S(e,o)});var $=r(e=>{let t=e();return typeof t=="function"&&S("destroy",t),()=>{}},"onEffect"),R=r(()=>(!y&&console.warn("getCurrentContext called outside of component lifecycle"),y),"getCurrentContext"),{beforeMount:G,onMount:q,beforeUpdate:B,onUpdate:K,onDestroy:J}=U;var Y=/^-?\d+(?:\.\d+)?(?:e[+-]?\d+)?$/,v=r(e=>{if(e==null)return"";if(e==="true")return!0;if(e==="false")return!1;if(Y.test(e))return Number(e);if(typeof e=="string"&&(e[0]==="{"||e[0]==="["))try{return JSON.parse(e)}catch{}return e},"parseAttributeValue"),I=r(e=>{e._functionProps&&Object.assign(e.props,e._functionProps);for(let{name:t,value:o}of e.attributes)t.startsWith("on")||t.startsWith("ref")||o==="function"&&e._functionProps?.[t]||(e.props[t]=v(o))},"initializeProps");var E=r(()=>E.cache||(E.cache=[...document.querySelectorAll("head style, head link[rel=stylesheet]")]),"s");var Q=/^[a-z][a-z0-9]*-[a-z0-9-]*$/,Z=r((e,t={})=>{var o,c,V,a;return a=class extends HTMLElement{constructor(){super();g(this,c);g(this,o,this.attachShadow({mode:t.shadowMode||"open"}));C(this,"props",Object.create(null));C(this,"_cleanup",[]);D(this,c,V).call(this),this.runHook("beforeMount")}connectedCallback(){this.runHook("mount")}disconnectedCallback(){this._cleanup.forEach(l=>l()),this._cleanup=[],this.runHook("destroy")}},o=new WeakMap,c=new WeakSet,V=r(function(){z(this),I(this),t.globalStyles&&E().forEach(i=>_(this,o).appendChild(i.cloneNode(!0)));let l=e?.call(this,this.props);d(_(this,o),l)},"#initializeComponent"),a},"BaseComponent"),ee=r((e,t,o={})=>{typeof customElements<"u"&&Q.test(e)&&!customElements.get(e)&&customElements.define(e,Z(t,o))},"component");function te(e=null){let t=e;return(...o)=>o.length===0?t:t=o[0]}r(te,"ref");function ne(e,t,o=null){let c=new CustomEvent(e,{detail:t,bubbles:!0,composed:!0,cancelable:!0});return o?.dispatchEvent(c),c}r(ne,"emit");export{H as batch,G as beforeMount,B as beforeUpdate,ee as component,w as computed,m as effect,ne as emit,R as getCurrentContext,F as html,J as onDestroy,$ as onEffect,q as onMount,K as onUpdate,te as ref,A as signal};
//# sourceMappingURL=esor.min.js.map