Skip to content

Commit

Permalink
Update run script
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Nov 21, 2023
1 parent 1ac2e14 commit 8b8b277
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
2 changes: 0 additions & 2 deletions build.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// deno-lint-ignore-file no-explicit-any

export type BuildInput = {
code: string;
loader?: "js" | "jsx" | "ts" | "tsx";
Expand Down
47 changes: 26 additions & 21 deletions run.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
let jsxImportSource: string | undefined = undefined;
/*! esm.sh run
*
* Add `<script type="module" src="https://esm.sh/run" defer></script>` to your HTML to run jsx/tsx in browser without build.
*
*/

const d = document;
const runScripts: { loader: string; code: string }[] = [];
let jsxImportSource: string | undefined = undefined;

document.querySelectorAll("script").forEach((el) => {
d.querySelectorAll("script").forEach((el) => {
let loader: string | null = null;
switch (el.type) {
case "importmap": {
const o = JSON.parse(el.innerHTML);
jsxImportSource = o?.imports?.["@jsxImportSource"];
const im = JSON.parse(el.innerHTML);
jsxImportSource = im.imports?.["@jsxImportSource"];
break;
}
case "text/babel":
Expand All @@ -28,11 +35,19 @@ document.querySelectorAll("script").forEach((el) => {

runScripts.forEach(async (input) => {
const murl = new URL(import.meta.url);
const hash = await hashText(
murl.pathname + input.loader + (jsxImportSource ?? "") +
input.code,
const buffer = new Uint8Array(
await crypto.subtle.digest(
"SHA-1",
new TextEncoder().encode(
murl.pathname + input.loader + (jsxImportSource ?? "") +
input.code,
),
),
);
let js = localStorage.getItem(hash);
const hash = [...buffer].map((b) => b.toString(16).padStart(2, "0"))
.join("");
const cacheKey = "esm.sh/run/" + hash;
let js = localStorage.getItem(cacheKey);
if (!js) {
const res = await fetch(murl.origin + `/+${hash}.mjs`);
if (res.ok) {
Expand All @@ -42,20 +57,10 @@ runScripts.forEach(async (input) => {
const ret = await transform({ ...input, jsxImportSource, hash });
js = ret.code;
}
localStorage.setItem(hash, js!);
localStorage.setItem(cacheKey, js!);
}
const script = document.createElement("script");
const script = d.createElement("script");
script.type = "module";
script.innerHTML = js!;
document.body.appendChild(script);
d.body.appendChild(script);
});

async function hashText(s: string): Promise<string> {
const buffer = await crypto.subtle.digest(
"SHA-1",
new TextEncoder().encode(s),
);
return Array.from(new Uint8Array(buffer)).map((b) =>
b.toString(16).padStart(2, "0")
).join("");
}
1 change: 1 addition & 0 deletions server/build_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ func minify(code string, target api.Target, loader api.Loader) ([]byte, error) {
MinifyWhitespace: true,
MinifyIdentifiers: true,
MinifySyntax: true,
LegalComments: api.LegalCommentsInline,
Loader: loader,
})
if ret.Errors != nil && len(ret.Errors) > 0 {
Expand Down

0 comments on commit 8b8b277

Please sign in to comment.