load-remote
Version:
在 web 项目中引用载入远程资源(css, js),基于<link>/<script>标签实现。Used to load remote resources(CSS/JS) in a web project , based on the html tag(<link>/<script>).
10 lines (8 loc) • 1.68 kB
JavaScript
/**
* Bundle of load-remote
* Generated: 2020-03-14
* Version: 1.0.2
* License: MIT
* Author: 2631541504@qq.com
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).LoadRemote={})}(this,function(e){"use strict";var s=new Map;e.injectToHtml=function(e,t){var r=e.id;if(!document.getElementById(r)){var n=t||document.head,i="gnt"in e?e.gnt():e;i.id=r,n.appendChild(i)}},e.loadRemote=function(f,m){if(!f)return Promise.reject(new Error("The params url should not be empty"));var l=m&&m.id||f;return function(e,t){function r(){return s.delete(o)}var n=1<arguments.length&&void 0!==t?t:{},i=n.id,o=i?"promise-".concat(i):e;if(!s.has(o)){var a=e();s.set(o,a),a.then(function(){var e=n.cacheTime||n.delayTime;void 0===e?r():setTimeout(r,e)}).catch(r)}return s.get(o)}(function(){return new Promise(function(e,t){var r=document.getElementById(l);if(r)return e({target:r});var n=/\.css$/.test(f),i=/\.js$/.test(f),o=m&&m.tagName||(n?"link":i?"script":"");if(!o)return t(new Error("Please set the tagName of this remote resource"));var a=m&&m.type||(n?"text/css":i?"text/javascript":"");if(!a)return t(new Error("Please provide the type attribute of this remote resource"));var s=m&&m.rel||(n?"stylesheet":i?"prefetch":""),c=m&&m.charset||"utf-8",u=m&&m.injectParentElement||document.head,d=document.createElement(o);d.type=a,d.charset=c,l&&(d.id=l),"link"===o?(s&&d.setAttribute("rel",s),d.setAttribute("href",f)):d.setAttribute("src",f),d.onload=e,d.onerror=function(e){t(e),u.removeChild(d)},u.appendChild(d)})},{id:l})},Object.defineProperty(e,"__esModule",{value:!0})});