Repository URL to install this package:
|
Version:
1.2.9 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createScriptTag = createScriptTag;
exports.default = void 0;
/**
* @description scoped default argument
*/
const defaultAttributes = {
async: true
};
function shouldIgnore(src) {
// no need for this in test env
// @todo we may want to render a local import though, using a mock/
if (process.env.NODE_ENV === 'test') {
return true;
}
if (typeof window !== 'object') {
return true;
} // all scripts
const scriptElements = window.document.getElementsByTagName('script');
for (let index = 0; index < scriptElements.length; index++) {
const script = scriptElements[index]; // ignore it if we already loaded
if (script.src === src) {
// console.warn('already loaded: ', src)
return true;
}
}
return false;
}
/**
* @tutorial https://hacks.mozilla.org/2009/06/defer/
*
* @param {String} src url of the script tag
* @param {*} attributes
* @return {void}
* @modifies window.dom -> createElement -> <script src>
*/
function createScriptTag(src, attributes = defaultAttributes) {
if (shouldIgnore(src) === true) {
return;
}
const scriptElements = window.document.getElementsByTagName('script'); // find first script
const ref = scriptElements[0]; // make our own new script
const script = window.document.createElement('script'); // set attributes
script.src = src; // can also object async, or enable a fn to tap this
script.async = attributes.async; // put before first script
ref.parentNode.insertBefore(script, ref);
}
var _default = createScriptTag;
exports.default = _default;