UNPKG

umd-package-loader

Version:

umd 包加载器

3 lines (2 loc) 1.74 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self)["umd-package-loader"]=t()}(this,(function(){"use strict";return class{constructor(e){this.resolveName=e=>e.replace(/-([^-])/g,(e,t)=>t.toUpperCase()),this.resolveURL=e=>this.shouldApplyLocalCache(e.url),this.setCache=(e,t)=>{((e,t)=>{window.ASSETS_CACHE[e]=t})(this.name,e),"function"==typeof t&&t(e)},this.clearCache=()=>{var e;e=this.name,delete window.ASSETS_CACHE[e],this.timestamp=+new Date,this.url=this.resolveURL(this.options)},this.createNode=(e,t)=>{var i=document.createElement("script");return i.type=e.scriptType||"text/javascript",i.charset="utf-8",i.async=!0,i.setAttribute("data-name",this.name),i},this.loadScript=(e={})=>{const{name:t,contextWindow:i}=this;return new Promise((s,n)=>{if(this.getCache())return this.getCache();if(console.time("loadscript "+this.name),"function"==typeof window.requirejs)window.requirejs([this.url],e=>{this.setCache(e,s),console.timeEnd("loadscript "+t)},e=>n(new Error(e)));else{const o=this.createNode(e,"script");o.addEventListener("load",e=>{"load"===e.type&&(this.setCache(i[t],s),console.timeEnd("loadscript "+t))}),o.addEventListener("error",(function(e){n(e)})),o.src=this.url,document.head.appendChild(o)}})},this.timestamp=+new Date,this.contextWindow=e.context||window,window.ASSETS_CACHE||(window.ASSETS_CACHE=Object.create(null)),this.name=this.resolveName(e.name),this.url=this.resolveURL(e),this.options={...e}}shouldApplyLocalCache(e){return e+(this.noCache?"?s="+this.timestamp:"")}getCache(){return e=this.name,window.ASSETS_CACHE[e];var e}}})); //# sourceMappingURL=index.js.map