load worker as a module

This commit is contained in:
Alexander Wang 2025-01-13 20:40:25 -07:00
parent 03c7d1eff1
commit f50f02b9c5
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927

View file

@ -11,18 +11,17 @@ export async function loadFile(path) {
}
export async function createWorker() {
// Combine wasmExecJs with worker script
const workerResponse = await fetch(new URL("./worker.js", import.meta.url));
if (!workerResponse.ok) {
let response = await fetch(new URL("./worker.js", import.meta.url));
if (!response.ok)
throw new Error(
`Failed to load worker.js: ${workerResponse.status} ${workerResponse.statusText}`
`Failed to load worker.js: ${response.status} ${response.statusText}`
);
}
const workerJs = await workerResponse.text();
let workerScript = await response.text();
const blob = new Blob(["(() => {", wasmExecJs, "})();", workerJs], {
type: "application/javascript",
let blob = new Blob(["(() => {", wasmExecJs, "})();", workerScript], {
type: "text/javascript;charset=utf-8",
});
return new Worker(URL.createObjectURL(blob), {
type: "module",
});
return new Worker(URL.createObjectURL(blob));
}