UNPKG

@devmore/vanilact

Version:
3 lines (2 loc) 5.05 kB
let n,t,o,e=0,r=0,l=[];const s=new Map;let u=0;const c=n=>{const t=(new DOMParser).parseFromString(n,"text/html");return Array.from(t.body.childNodes).some(n=>1===n.nodeType)},i=n=>"function"==typeof n&&n.prototype&&n.prototype.render;function p(n,t,o){let e=null,r=null;if("string"==typeof n||"number"==typeof n)return e="string"==typeof n&&c(n)?document.createRange().createContextualFragment(n):document.createTextNode(String(n)),t&&t.appendChild(e),(null==o?void 0:o.setDom)&&(null==o||o.setDom(t)),(null==o?void 0:o.onMount)&&(null==o||o.onMount()),e;if(Array.isArray(n))return e=document.createDocumentFragment(),n.forEach(n=>{const t=p(n,e);t&&e.appendChild(t)}),t&&t.appendChild(e),(null==o?void 0:o.setDom)&&(null==o||o.setDom(t)),(null==o?void 0:o.onMount)&&(null==o||o.onMount()),e;if("function"==typeof n.type){if(i(n.type)){r=new n.type(n.props||{}),n.instance=r,(null==r?void 0:r.willMount)&&(null==r||r.willMount());const l=r.render();return m(t,n,l),e=p(l,t,r),(null==o?void 0:o.setDom)&&(null==o||o.setDom(t)),(null==o?void 0:o.onMount)&&(null==o||o.onMount()),e}{const r=n.type(n.props||{});return e=p(r,t,o),m(t,n,e),e}}e=document.createElement(n.type);const l=n.props||{};for(const[n,t]of Object.entries(l))"ref"==n?"function"==typeof t?t(e):"object"==typeof t&&null!==t&&"current"in t&&(t.current=e):n.startsWith("on")&&"function"==typeof t?e.addEventListener(n.slice(2).toLowerCase(),t):"children"!==n&&e.setAttribute(n,t);return[].concat(l.children||[]).forEach(n=>{const t=p(n,e);t&&e.appendChild(t)}),t&&t.appendChild(e),(null==r?void 0:r.setDom)&&(null==r||r.setDom(t)),(null==r?void 0:r.onMount)&&(null==r||r.onMount()),(null==o?void 0:o.setDom)&&(null==o||o.setDom(t)),(null==o?void 0:o.onMount)&&(null==o||o.onMount()),e}function f(t,o){n=t,s.clear(),e=0,u=0;let r=null,l=null;i(t.type)?(l=new t.type(t.props),t.instance=l,(null==l?void 0:l.willMount)&&(null==l||l.willMount()),r=l.render()):r=t.type(t.props),m(o,t,r),p(r,o,l),function(n){const t=n.hooks,o=n.effects||[];for(const n of o){const o=t[n];o.cleanup&&o.cleanup();const e=o.callback();"function"==typeof e&&(o.cleanup=e)}n.effects=[]}(t)}function d(){if(r++>100)throw new Error("Too many rerenders!");if(o.innerHTML="",f(t,o),l&&l.length>0){for(const n of l)if("function"==typeof n)try{n()}catch(n){console.error(n.stack)}l=[]}r=0}function a(n,t){const o=n.split("/").filter(n=>n),e=t.split("/").filter(n=>n);if(o.length!==e.length)return null;const r={};for(let n=0;n<o.length;n++)if(e[n].startsWith(":")){r[e[n].slice(1)]=o[n]}else if(o[n]!==e[n])return null;return r}function m(n,t,o){const e=u++;return s.set(e,{container:n,componentNode:t}),t.container=n,t.__id=e,t.__dom=o,e}function h(t){const o=n.hooks||(n.hooks=[]);void 0===o[e]&&(o[e]=t);const r=e;return[o[e++],n=>{o[r]!==n&&(o[r]=n,d())}]}function y(t,o){const r=n.hooks||(n.hooks=[]),l=r[e];(!l||!o||o.some((n,t)=>n!==l.deps[t]))&&(r[e]={callback:t,deps:o,cleanup:null},n.effects||(n.effects=[]),n.effects.push(e)),e++}function v(n,t={},...o){return{type:n,props:{...t,children:o}}}function w(){const[n,t]=h(()=>location.pathname);return y(()=>{const n=()=>t(location.pathname);return window.addEventListener("popstate",n),()=>window.addEventListener("popstate",n)},[]),n}function M(n,t={}){let o=[];t&&Object.entries(t).map(([n,t])=>{o.push(`${encodeURIComponent(decodeURIComponent(n))}=${encodeURIComponent(decodeURIComponent(t))}`)}),history.pushState({},"",[n,o.filter(n=>n).join("&")].filter(n=>n).join("?")),d()}function D(n){let t=null,o=!1,e=null;return function(r){const[l,s]=h(0);return t||o||(o=!0,n().then(n=>{t=n.default,s(n=>n+1)}).catch(n=>{e=n,s(n=>n+1)})),e?(console.error(e.stack),v("div")):t?v(t,r,...r.children||[]):v("div")}}function g({routes:n,errorViews:t=[]}){const o=location.pathname||"/";for(const{path:e,component:r,middlewares:l}of n){const n=a(o,e);if(n){if(l&&l.length>0)for(const n of l||[])if("function"==typeof n&&!n()){let n=null==t?void 0:t.find(n=>401===n.statusCode);return n||(n={component:()=>v("center",{style:"position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%)"},v("h1",null,"401 Access Denied"))}),v(n.component,n.props)}return v(r,{params:n})}}let e=null==t?void 0:t.find(n=>404===n.statusCode);return e||(e={component:()=>v("center",{style:"position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%)"},v("h1",null,"404 Not Found"))}),v(e.component,e.props)}function C(n){return o=n,{render(n){t={type:n,props:{},hooks:[]},d(),window.addEventListener("popstate",d)}}}class b{constructor(){this.dom=null}setDom(n){this.dom=n}getDom(n){return n?this.dom.querySelector(n):this.dom}getDomAll(n){return this.dom.querySelectorAll(n)}willMount(){}render(){}onMount(){}}const k=(n=null)=>({current:n}),E=({children:n})=>n,A=n=>{"function"==typeof n&&l.push(n)};export{E as Fragment,b as IComponent,g as Router,C as createApp,v as createElement,k as createRef,i as isClassComponent,c as isHTML,D as lazy,M as navigate,A as onSetup,y as useEffect,w as useLocation,h as useState}; //# sourceMappingURL=vanilact.min.js.map