Last active
January 23, 2023 18:47
-
-
Save CarsonSlovoka/c19252bcbe8ac7f7decf9869fea3edd9 to your computer and use it in GitHub Desktop.
Example: import javascript from gist
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
| 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))) | |
| } |
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
| export class Person { | |
| constructor(name, age) { | |
| this.name = name | |
| this.age = age | |
| } | |
| Say(msg) { | |
| console.log(`${this.name}:${msg}`) | |
| } | |
| } |
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
| <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> |
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
| 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