unwasm: Universal WebAssembly Tools

0.5.3 · active · verified Sun Apr 19

unwasm provides a suite of universal WebAssembly (Wasm) tools for JavaScript, designed to simplify the integration of `.wasm` modules across various JavaScript runtimes, frameworks, and build environments. It currently stands at version 0.5.3, with a release cadence that includes frequent patch and minor updates. The project's core mission is to align with the WebAssembly/ES Module Integration proposal while maintaining broad compatibility with existing ecosystem libraries. Its key differentiators include automated resolution of WebAssembly imports, dynamic generation of code bindings tailored for bundlers, and a flexible API supporting both static and dynamic `.wasm` module imports. This includes explicit support for import objects and direct `WebAssembly.Module` instances via a `?module` suffix, catering to environments both with and without top-level `await` capabilities.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the JavaScript API for interacting with WebAssembly modules after they have been processed by unwasm's build-time integration. It showcases both static and dynamic imports, including initialization with an import object, and the subsequent calling of exported WebAssembly functions.

import initRand, { rand } from "unwasm/examples/rand.wasm";

// This quickstart assumes unwasm's build-time integration is configured
// to process '.wasm' imports, e.g., through a Vite or Rollup plugin.

async function runWasmExample() {
  // Initialize the WebAssembly module that requires an import object.
  // This example provides a 'seed' function for the 'env' namespace.
  await initRand({
    env: {
      seed: () => () => Math.random() * Date.now()
    }
  });

  // Now the 'rand' function (exported from rand.wasm) can be called.
  const randomNumber = rand();
  console.log("Generated random number from WASM module:", randomNumber);

  // Demonstrate dynamic import for a simpler module without explicit initialization.
  const { sum } = await import("unwasm/examples/sum.wasm");
  const result = sum(10, 20);
  console.log("Result of sum(10, 20) from WASM module:", result);
}

runWasmExample();

view raw JSON →