pragma-views2
Version:
1 lines • 1.41 kB
JavaScript
export async function loadComponent(e,t){1==t&&await loadTemplates(`${e}.html`),loadScriptToHead(`${e}.js`)}export async function loadScriptToHead(e){const t=await loadScript(e);document.head.appendChild(t)}export async function loadScript(e){const t=document.createElement("script");return t.async=!0,t.src=e,t.type="module",t}export async function loadLibrary(e,t,n){const o=getExistingScript(e);if(null!=o)return null!=n&&n(),o;const c=document.createElement("script");return c.async=!0,c.src=e,null!=n&&(c.onload=n),null!=t?c.defer=t:c.type="module",document.head.appendChild(c),c}export async function loadModule(e,t){if(null==window.modules&&(window.modules=new Map),window.modules.has(t))return window.modules.get(t);const n=await import(relToAbsPath(e));return window.modules.set(t,n),n}function relToAbsPath(e){const t=document.createElement("a");return t.setAttribute("href",e),t.cloneNode(!1).href}function getExistingScript(e){return Array.from(document.head.childNodes).find((t=>"SCRIPT"===t.tagName&&t.src.includes(e.replace("./",""))))}export async function removeScript(e){const t=getExistingScript(e);null!=t&&document.head.removeChild(t)}export async function loadTemplates(e){const t=await fetch(e).then((e=>e.text())),n=document.createElement("template");n.innerHTML=t;const o=n.content.querySelectorAll("template");for(let e of o)window.templates.set(e.id,e.content);return o[0].content}