-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackload.min.js
9 lines (9 loc) · 3.78 KB
/
stackload.min.js
1
2
3
4
5
6
7
8
9
/**
* StackLoad - A tiny JS/CSS module loader that simply does its job.
*
* @version 1.1.0
* @link https://github.com/lubber-de/stackload
* @copyright Copyright (c) 2022 Marco 'Lubber' Wienkoop
* @license MIT https://github.com/lubber-de/stackload/blob/master/LICENSE
*/
!function(window,document){var registry=[],fullStack=[],currentLoadIndex=0,callBacks=[],stopAll=!1,continueStack=function(){++currentLoadIndex===callBacks[0].doneIndex&&(callBacks[0].success(),callBacks.shift()),currentLoadIndex===fullStack.length||stopAll||loadSingle()},stackLoadError=function(e){var t=e.target;console.error("[StackLoad Error] Missing file: "+(t.src?t.src:t.href)),!1===callBacks[0].error(e)&&(stopAll=!0),t.parentNode.removeChild(t),stackLoadDone(e)},stackLoadDone=function(e){var t,r=e.target;r.removeEventListener("error",stackLoadError),r.removeEventListener("load",stackLoadDone),r.jsonp&&r.parentNode&&r.parentNode.removeChild(r),stopAll?(t=callBacks[0].doneIndex-++currentLoadIndex)>0&&(fullStack.splice(currentLoadIndex,t),callBacks.shift(),callBacks.forEach(function(e,r,s){s[r].doneIndex-=t})):(r.href&&searchCssImport(document.styleSheets[document.styleSheets.length-1]),continueStack())},loadSingle=function(){var o=fullStack[currentLoadIndex],s;try{!o.check||""!==o.check&&!eval(o.check)?(o.type&&"css"===o.type?(s=document.createElement("link"),s.type="text/css",s.rel="stylesheet",s.href=o.url):(s=document.createElement("script"),s.type="text/javascript",s.src=o.url,s.async=!1,o.type&&"jsonp"===o.type&&(s.jsonp=!0)),document.head.appendChild(s),s.addEventListener("load",stackLoadDone),s.addEventListener("error",stackLoadError)):continueStack()}catch(e){console.error(e),continueStack()}},setup=function(e){if(void 0!==e&&("string"==typeof e||e.url?e={files:[e]}:Array.isArray(e)&&(e={files:e}),e.files)){var t=[];("string"==typeof e.files||e.files.url)&&(e.files=[e.files]),e.files.forEach(function(e){if("string"==typeof e&&(e={url:e}),e.url.match(/^(@[a-z0-9-]+\/)?[a-z0-9-]+@[0-9]+(\.[0-9]+)?(\.[0-9]+)?(-[0-9a-z-]+(\.[0-9a-z-]+)?)?(\/.*)?/i)&&(e.url="https://cdn.jsdelivr.net/npm/"+e.url),e.url&&-1===registry.indexOf(e.url)){if(!e.type){var r=e.url.match(/\.([0-9a-zA-Z]+)(?:[?#]|$)/i);r&&(e.type=r[1].toLowerCase())}if(e.type&&"jsonp"===e.type.toLowerCase()||e.noCache){var s=(new Date).getTime();e.url.match(/\?/)?e.url+="&_="+s:e.url+="?_="+s}t.push(e),registry.push(e.url)}});var r=t.length;if("function"!=typeof e.success&&(e.success=function(){}),r>0){var s=(fullStack=fullStack.concat(t)).length;"function"!=typeof e.error&&(e.error=function(){}),callBacks.push({doneIndex:s,success:e.success,error:e.error}),currentLoadIndex===s-r&&loadSingle()}else e.success()}},searchCssImport=function(e){try{if(e.cssRules)for(var t=0,r=e.cssRules.length;t<r;t++)e.cssRules[t].href&&-1===registry.indexOf(e.cssRules[t].href)&®istry.push(e.cssRules[t].href)}catch(e){}},cssProperties=function(e){var t,r=document.createElement("div");return r.className=e,document.body.appendChild(r),t=JSON.parse(JSON.stringify(window.getComputedStyle(r))),r.parentNode.removeChild(r),t};window.stackLoad=function(e){if(stopAll=!1,0===registry.length){var t,r,s=document.querySelectorAll("link,script"),o=document.styleSheets;for(t=0,r=s.length;t<r;t++)s[t].src&&-1===registry.indexOf(s[t].src)?registry.push(s[t].src):s[t].href&&-1===registry.indexOf(s[t].href)&®istry.push(s[t].href);for(t=0,r=o.length;t<r;t++)searchCssImport(o[t])}return setup(e),{then:function(e){return setup(e),this}}};var currentScript=document.currentScript||document.getElementById("stackload"),autoload;currentScript&&(autoload=decodeURIComponent(currentScript.src).split(/[?&]autoload=([^&]*)/),autoload.length>1&&stackLoad(autoload[1].match(/^[\[{].*[\]}]$/)?JSON.parse(autoload[1],function(k,v){return"success"===k||"error"===k?eval(v):v}):autoload[1]))}(window,document);