Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
9 changes: 2 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let createModulePromise;
import createModule from "./wasm-zstd.js";

let modulePromise;
let moduleInstance;

Expand Down Expand Up @@ -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;
Expand Down
Loading