Skip to content

Commit f94dab5

Browse files
committed
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.
1 parent 5888f57 commit f94dab5

3 files changed

Lines changed: 8 additions & 10 deletions

File tree

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ builds because Vite owns the `.wasm` asset URL.
4343

4444
## Usage in inline workers (`?worker&inline`)
4545

46-
Vite inline workers are loaded from `blob:` URLs, so sibling `.wasm` files cannot be resolved
47-
from `import.meta.url`. Fetch the wasm bytes on the main thread and pass them into the worker:
46+
Vite inline workers are loaded from `blob:` URLs, so neither sibling `.wasm` files nor sibling
47+
JS chunks can be resolved relative to the worker. To keep inline workers self-contained, the
48+
Emscripten glue is imported **statically** (not via a runtime `import("./wasm-zstd.js")`), so
49+
bundlers inline it into the worker chunk. You only need to supply the wasm **bytes** — fetch
50+
them on the main thread and pass them into the worker:
4851

4952
```ts
5053
// main thread

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ioai/wasm-zstd",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "Vite-friendly WebAssembly bindings for facebook/zstd",
55
"type": "module",
66
"main": "dist/index.js",

src/index.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
let createModulePromise;
1+
import createModule from "./wasm-zstd.js";
2+
23
let modulePromise;
34
let moduleInstance;
45

@@ -49,18 +50,12 @@ function normalizeInitOptions(options = {}) {
4950
return moduleOptions;
5051
}
5152

52-
async function loadCreateModule() {
53-
createModulePromise ??= import("./wasm-zstd.js").then((mod) => mod.default);
54-
return await createModulePromise;
55-
}
56-
5753
export async function init(options = {}) {
5854
if (moduleInstance != undefined) {
5955
return;
6056
}
6157

6258
const moduleOptions = normalizeInitOptions(options);
63-
const createModule = await loadCreateModule();
6459

6560
modulePromise ??= createModule(moduleOptions).then((mod) => {
6661
moduleInstance = mod;

0 commit comments

Comments
 (0)