-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
run: Use esm-compiler to transform jsx/ts for localhost referer
- Loading branch information
Showing
10 changed files
with
232 additions
and
180 deletions.
There are no files selected for viewing
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// @ts-expect-error $TARGET is defined at build time | ||
import { init, transform } from "/esm-compiler@0.6.2/$TARGET/esm_compiler.mjs"; | ||
const initPromise = init("/esm-compiler@0.6.2/pkg/esm_compiler_bg.wasm"); | ||
|
||
export async function tsx( | ||
url: URL, | ||
code: string, | ||
importMap: { imports?: Record<string, string> }, | ||
target: string, | ||
cachePromise: Promise<Cache>, | ||
): Promise<Response> { | ||
await initPromise; | ||
const ret = transform(url.pathname, code, { importMap, target }); | ||
return new Response(ret.code, { headers: { "Content-Type": "application/javascript; charset=utf-8" } }); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const stringify = JSON.stringify; | ||
|
||
export async function tsx( | ||
url: URL, | ||
code: string, | ||
importMap: { imports?: Record<string, string> }, | ||
target: string, | ||
cachePromise: Promise<Cache>, | ||
): Promise<Response> { | ||
const filename = url.pathname.split("/").pop()!; | ||
const extname = filename.split(".").pop()!; | ||
const buffer = new Uint8Array( | ||
await crypto.subtle.digest( | ||
"SHA-1", | ||
new TextEncoder().encode(extname + code + stringify(importMap) + target + "false"), | ||
), | ||
); | ||
const id = [...buffer].map((b) => b.toString(16).padStart(2, "0")).join(""); | ||
const cache = await cachePromise; | ||
const cacheKey = new URL(url); | ||
cacheKey.searchParams.set("_tsxid", id); | ||
|
||
let res = await cache.match(cacheKey); | ||
if (res) { | ||
return res; | ||
} | ||
|
||
res = await fetch(urlFromCurrentModule(`/+${id}.mjs`)); | ||
if (res.status === 404) { | ||
res = await fetch(urlFromCurrentModule("/transform"), { | ||
method: "POST", | ||
body: stringify({ filename, code, importMap, target }), | ||
}); | ||
const ret = await res.json(); | ||
if (ret.error) { | ||
throw new Error(ret.error.message); | ||
} | ||
res = new Response(ret.code, { headers: { "Content-Type": "application/javascript; charset=utf-8" } }); | ||
} | ||
if (!res.ok) { | ||
return res; | ||
} | ||
|
||
cache.put(cacheKey, res.clone()); | ||
return res; | ||
} | ||
|
||
/** create a URL object from the given path in the current module. */ | ||
function urlFromCurrentModule(path: string) { | ||
return new URL(path, import.meta.url); | ||
} |
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
Binary file not shown.
Binary file not shown.
Oops, something went wrong.