mirror of
https://github.com/swc-project/swc.git
synced 2024-12-21 04:32:01 +03:00
9 lines
631 B
JavaScript
9 lines
631 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 = !0, scriptEl.type = "text/javascript"), rest.src && scriptEl.addEventListener("load", ()=>(onLoad(), resolve())), !isMounted && document.getElementsByTagName("head")[0].appendChild(scriptEl), !rest.src) return onLoad(), resolve();
|
||
|
});
|
||
|
};
|