Created
September 2, 2023 10:54
-
-
Save farism/c2cf818ac7fc05e5ad1a6b1fbb113aa0 to your computer and use it in GitHub Desktop.
ESMLoader in mint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ESMLoader { | |
fun load (name : String, imports : string) : Promise(Value) { | |
let {resolve, promise} = | |
Promise.create() | |
let uid = | |
Uid.generate() | |
let content = | |
" | |
import #{imports} from \"#{name}\"; | |
window['#{uid}'](#{imports}); | |
delete window['#{uid}']; | |
" | |
` | |
(() => { | |
window[#{uid}] = #{resolve} | |
const script = document.createElement('script'); | |
script.setAttribute('type', 'module'); | |
script.appendChild(document.createTextNode(#{content})); | |
document.body.appendChild(script); | |
})() | |
` | |
promise | |
} | |
fun loadDefault (name : String) : Promise(Value) { | |
load(name, "m") | |
} | |
fun loadNamed (name : String, imports : Array(String)) : Promise(Value) { | |
load(name, "{ #{String.join(imports, ",")} }") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment