mirror of
https://github.com/swc-project/swc.git
synced 2024-12-21 04:32:01 +03:00
34 lines
958 B
JavaScript
34 lines
958 B
JavaScript
export const loadScript = (options) => {
|
|
const { id, onLoad = () => null, ...rest } = options;
|
|
|
|
return new Promise((resolve) => {
|
|
let scriptEl = document.getElementById(id);
|
|
|
|
const isMounted = !!scriptEl;
|
|
|
|
if (!scriptEl) {
|
|
scriptEl = document.createElement("script");
|
|
Object.keys(rest).forEach((key) => (scriptEl[key] = rest[key]));
|
|
scriptEl.id = id;
|
|
scriptEl.async = true;
|
|
scriptEl.type = "text/javascript";
|
|
}
|
|
if (rest.src) {
|
|
scriptEl.addEventListener("load", () => {
|
|
onLoad();
|
|
return resolve();
|
|
});
|
|
}
|
|
|
|
if (!isMounted) {
|
|
// when enabling swcMinify, this section won't be executed
|
|
document.getElementsByTagName("head")[0].appendChild(scriptEl);
|
|
}
|
|
|
|
if (!rest.src) {
|
|
onLoad();
|
|
return resolve();
|
|
}
|
|
});
|
|
};
|