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