Skip to content

Instantly share code, notes, and snippets.

@CarsonSlovoka
Last active January 23, 2023 18:47
Show Gist options
  • Select an option

  • Save CarsonSlovoka/c19252bcbe8ac7f7decf9869fea3edd9 to your computer and use it in GitHub Desktop.

Select an option

Save CarsonSlovoka/c19252bcbe8ac7f7decf9869fea3edd9 to your computer and use it in GitHub Desktop.
Example: import javascript from gist
export async function ImportModule(url: string) {
const res = await fetch(url)
const newBlob = new Blob([(await res.text())],{type: 'text/javascript'})
return (await import(URL.createObjectURL(newBlob)))
}
export class Person {
constructor(name, age) {
this.name = name
this.age = age
}
Say(msg) {
console.log(`${this.name}:${msg}`)
}
}
<script type="module">
const ImportModule = async (gistURL) => {
const res = await fetch(gistURL);
const newBlob = new Blob([(await res.text())],{type: 'text/javascript'})
const module = (await import(URL.createObjectURL(newBlob)))
return module
}
(async ()=>{
const module = await ImportModule("https://gist.githubusercontent.com/CarsonSlovoka/c19252bcbe8ac7f7decf9869fea3edd9/raw/a29e42bed1d18f0722fbaf3bd6a4281276322164/person.js")
const carson = new module.Person("Carson", 18)
carson.Say("hello world")
})()
</script>
import * as esm from "./import.js"
interface IPerson {
name: string
age: number
Say: (msg: string) => void
}
const module = await esm.ImportModule("https://gist.githubusercontent.com/CarsonSlovoka/c19252bcbe8ac7f7decf9869fea3edd9/raw/a29e42bed1d18f0722fbaf3bd6a4281276322164/person.js")
const Person = module.Person
const carson = new Person("Carson", 18) as IPerson
carson.Say("hello")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment