From f94dab562481fa17d0db48732caafde983dad077 Mon Sep 17 00:00:00 2001 From: joaner Date: Wed, 3 Jun 2026 20:50:21 +0800 Subject: [PATCH] fix: statically import zstd glue so inline workers are self-contained Inline workers (Vite ?worker&inline) run from blob: URLs, where the runtime relative import("./wasm-zstd.js") cannot be resolved, breaking consumers bundled with Next.js Turbopack ("Failed to resolve module specifier './wasm-zstd-*.js'"). Import the Emscripten glue statically so bundlers inline it into the worker chunk, making the worker fully self-contained. Bump to 1.1.2. --- README.md | 7 +++++-- package.json | 2 +- src/index.js | 9 ++------- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index f6be580..05b0e7c 100644 --- a/README.md +++ b/README.md @@ -43,8 +43,11 @@ builds because Vite owns the `.wasm` asset URL. ## Usage in inline workers (`?worker&inline`) -Vite inline workers are loaded from `blob:` URLs, so sibling `.wasm` files cannot be resolved -from `import.meta.url`. Fetch the wasm bytes on the main thread and pass them into the worker: +Vite inline workers are loaded from `blob:` URLs, so neither sibling `.wasm` files nor sibling +JS chunks can be resolved relative to the worker. To keep inline workers self-contained, the +Emscripten glue is imported **statically** (not via a runtime `import("./wasm-zstd.js")`), so +bundlers inline it into the worker chunk. You only need to supply the wasm **bytes** — fetch +them on the main thread and pass them into the worker: ```ts // main thread diff --git a/package.json b/package.json index a1dd85a..1bbaec7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ioai/wasm-zstd", - "version": "1.1.1", + "version": "1.1.2", "description": "Vite-friendly WebAssembly bindings for facebook/zstd", "type": "module", "main": "dist/index.js", diff --git a/src/index.js b/src/index.js index 2f93816..95e9673 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ -let createModulePromise; +import createModule from "./wasm-zstd.js"; + let modulePromise; let moduleInstance; @@ -49,18 +50,12 @@ function normalizeInitOptions(options = {}) { return moduleOptions; } -async function loadCreateModule() { - createModulePromise ??= import("./wasm-zstd.js").then((mod) => mod.default); - return await createModulePromise; -} - export async function init(options = {}) { if (moduleInstance != undefined) { return; } const moduleOptions = normalizeInitOptions(options); - const createModule = await loadCreateModule(); modulePromise ??= createModule(moduleOptions).then((mod) => { moduleInstance = mod;