UNPKG

@mornya/dynamic-load-libs

Version:

The project of Dynamic Loadable script and style library.

128 lines (127 loc) 5.51 kB
export function script(attributes) { return new Promise(function (resolve, reject) { var _a; var scriptElement = document.getElementById(attributes.id); if (scriptElement && !attributes.isReload) { resolve(false); } else { if (attributes.isReload) { scriptElement === null || scriptElement === void 0 ? void 0 : scriptElement.remove(); } var elScript = document.createElement('script'); elScript.id = attributes.id; elScript.type = (_a = attributes.type) !== null && _a !== void 0 ? _a : 'text/javascript'; if (attributes.text) { elScript.innerText = attributes.text; var head = document.getElementsByTagName('head')[0]; head.appendChild(elScript); resolve(true); } else { var isLoaded_1 = false; if (attributes.src) { elScript.src = attributes.src; } elScript.async = true; elScript.onreadystatechange = function () { if ((this.readyState === 'loaded' || this.readyState === 'complete') && isLoaded_1) { this.onload(); } else { isLoaded_1 = true; } }; elScript.onload = function () { return resolve(true); }; elScript.onerror = function () { scriptElement === null || scriptElement === void 0 ? void 0 : scriptElement.remove(); reject(new Error("Failed to load script: ".concat(attributes.src))); }; if (attributes.noModule !== undefined) { elScript.noModule = attributes.noModule; } if (attributes.nonce !== undefined) { elScript.nonce = attributes.nonce; } if (attributes.crossOrigin !== undefined) { elScript.crossOrigin = attributes.crossOrigin; } if (attributes.integrity !== undefined) { elScript.integrity = attributes.integrity; } if (attributes.referrerPolicy !== undefined) { elScript.referrerPolicy = attributes.referrerPolicy; } var head = document.getElementsByTagName('head')[0]; head.appendChild(elScript); } } }); } export function style(attributes) { return new Promise(function (resolve, reject) { var _a, _b; var styleElement = document.getElementById(attributes.id); if (styleElement && !attributes.isReload) { resolve(false); } else if (attributes.text) { var elStyle = document.createElement('style'); elStyle.id = attributes.id; elStyle.innerText = attributes.text; if (attributes.media !== undefined) { elStyle.media = attributes.media; } if (attributes.nonce !== undefined) { elStyle.nonce = attributes.nonce; } var head = document.getElementsByTagName('head')[0]; head.appendChild(elStyle); resolve(true); } else { var isLoaded_2 = false; var elLink = document.createElement('link'); elLink.type = (_a = attributes.type) !== null && _a !== void 0 ? _a : 'text/css'; elLink.rel = attributes.isAlternate ? 'alternate stylesheet' : 'stylesheet'; elLink.id = attributes.id; if (attributes.src) { elLink.href = attributes.src; } elLink.onreadystatechange = function () { if ((this.readyState === 'loaded' || this.readyState === 'complete') && isLoaded_2) { this.onload(); } else { isLoaded_2 = true; } }; elLink.onload = function () { return resolve(true); }; elLink.onerror = function () { styleElement === null || styleElement === void 0 ? void 0 : styleElement.remove(); reject(new Error("Failed to load style: ".concat(attributes.src))); }; if (attributes.media !== undefined) { elLink.media = attributes.media; } if (attributes.imageSizes !== undefined && ((_b = attributes.type) === null || _b === void 0 ? void 0 : _b.startsWith('image/'))) { elLink.imageSizes = attributes.imageSizes; } if (attributes.preloadAs !== undefined && attributes.type !== undefined) { elLink.rel = 'preload'; elLink.as = attributes.preloadAs; } if (attributes.crossOrigin !== undefined) { elLink.crossOrigin = attributes.crossOrigin; } if (attributes.integrity !== undefined) { elLink.integrity = attributes.integrity; } if (attributes.referrerPolicy !== undefined) { elLink.referrerPolicy = attributes.referrerPolicy; } var head = document.getElementsByTagName('head')[0]; head.appendChild(elLink); } }); }