forked from esm-dev/esm.sh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
49 additions
and
61 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,31 @@ | ||
/*! 🚀 esm.sh/x - ts/jsx/vue/svelte just works™️ in browser. */ | ||
|
||
((document) => { | ||
const $ = document.querySelector; | ||
const $: typeof document.querySelector = (s: string) => document.querySelector(s); | ||
const currentScript = document.currentScript as HTMLScriptElement | null; | ||
const modUrl = currentScript?.src || import.meta.url; | ||
const { hostname, href, pathname } = location; | ||
const { hostname, href, pathname, origin } = location; | ||
|
||
// import the `main` module from esm.sh if it's provided. | ||
// e.g. <script type="module" src="https://esm.sh/run" main="/main.tsx"></script> | ||
const el = currentScript ?? $<HTMLScriptElement>("script[type=module][src='" + modUrl + "'][main]"); | ||
// e.g. <script type="module" src="https://esm.sh/x" main="/main.tsx"></script> | ||
const el = currentScript ?? $<HTMLScriptElement>("script[type=module][main][src='" + modUrl + "']"); | ||
if (el) { | ||
const main = el.getAttribute("main"); | ||
if (main) { | ||
if (hostname === "localhost" || hostname === "127.0.0.1" || /^192\.168\.\d+\.\d+$/.test(hostname)) { | ||
alert("Please serve your app with `esm.sh run` for local development."); | ||
alert("Please serve your app with `esm.sh serve` for local development."); | ||
return; | ||
} | ||
const mainUrl = new URL(main, href); | ||
const q = mainUrl.searchParams; | ||
const v = $<HTMLMetaElement>("meta[name=version]")?.content; | ||
mainUrl.search = ""; | ||
if ($("script[type=importmap]")) { | ||
q.set("im", btoa(pathname).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "")); | ||
} | ||
if (v) { | ||
q.set("v", v); | ||
} | ||
import(new URL(modUrl).origin + "/" + mainUrl); | ||
import(mainUrl.origin === origin ? new URL(modUrl).origin + "/" + mainUrl : "" + mainUrl); | ||
} | ||
} | ||
})(document); |