Gathering detailed insights and metrics for unwasm
Gathering detailed insights and metrics for unwasm
Gathering detailed insights and metrics for unwasm
Gathering detailed insights and metrics for unwasm
npm install unwasm
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
178 Stars
76 Commits
6 Forks
3 Watching
8 Branches
15 Contributors
Updated on 28 Oct 2024
JavaScript (92.8%)
TypeScript (7.2%)
Cumulative downloads
Total Downloads
Last day
-0.9%
93,379
Compared to previous day
Last week
3.1%
489,109
Compared to previous week
Last month
13.8%
2,068,730
Compared to previous month
Last year
0%
13,269,235
Compared to previous year
Universal WebAssembly tools for JavaScript.
This project aims to make a common and future-proof solution for WebAssembly modules support suitable for various JavaScript runtimes, frameworks, and build Tools following WebAssembly/ES Module Integration proposal from WebAssembly Community Group as much as possible while also trying to keep compatibility with current ecosystem libraries.
When importing a .wasm
module, unwasm resolves, reads, and then parses the module during the build process to get the information about imports and exports and even tries to automatically resolve imports and generate appropriate code bindings for the bundler.
If the target environment supports top level await
and also the wasm module requires no imports object (or they are auto resolvable), unwasm generates bindings to allow importing wasm module like any other ESM import.
If the target environment lacks support for top-level await
or the wasm module requires an imports object or lazy
plugin option is set to true
, unwasm will export a wrapped Proxy object which can be called as a function to evaluate the module with custom imports object lazily. This way we still have a simple syntax as close as possible to ESM modules and also we can lazily initialize modules.
Example: Using static import
1import { sum } from "unwasm/examples/sum.wasm";
Example: Using dynamic import
1const { sum } = await import("unwasm/examples/sum.wasm");
If your WebAssembly module requires an import object (unwasm can automatically infer them), the usage syntax would be slightly different as we need to initiate the module with an import object first.
Example: Using dynamic import with imports object
1const { rand } = await import("unwasm/examples/rand.wasm").then((r) => 2 r.default({ 3 env: { 4 seed: () => () => Math.random() * Date.now(), 5 }, 6 }), 7);
Example: Using static import with imports object
1import initRand, { rand } from "unwasm/examples/rand.wasm"; 2 3await initRand({ 4 env: { 5 seed: () => () => Math.random() * Date.now(), 6 }, 7});
[!NOTE] When using static import syntax, and before initializing the module, the named exports will be wrapped into a function by proxy that waits for the module initialization and if called before init, will immediately try to call init without imports and return a Promise that calls a function after init.
There are situations where libraries require a WebAssembly.Module
instance to initialize WebAssembly.Instance
themselves. In order to maximize compatibility, unwasm allows a specific import suffix ?module
to import .wasm
files as a Module directly.
1import _sumMod from "unwasm/examples/sum.wasm?module"; 2const { sum } = await WebAssembly.instantiate(_sumMod).then((i) => i.exports);
[!NOTE] Open an issue to us! We would love to help those libraries to migrate!
Unwasm needs to transform the .wasm
imports to the compatible bindings. Currently, the only method is using a rollup plugin. In the future, more usage methods will be introduced.
First, install the unwasm
npm package.
1# ✨ Auto-detect 2npx nypm install unwasm 3 4# npm 5npm install unwasm 6 7# yarn 8yarn add unwasm 9 10# pnpm 11pnpm install unwasm 12 13# bun 14bun install unwasm
1// rollup.config.js 2import { rollup as unwasm } from "unwasm/plugin"; 3 4export default { 5 plugins: [ 6 unwasm({ 7 /* options */ 8 }), 9 ], 10};
esmImport
: Direct import the wasm file instead of bundling, required in Cloudflare Workers and works with environments that allow natively importing a .wasm
module (default is false
)lazy
: Import .wasm
files using a lazily evaluated proxy for compatibility with runtimes without top-level await support (default is false
)unwasm provides useful build tools to operate on .wasm
modules directly.
Note: unwasm/tools
subpath export is not meant or optimized for production runtime. Only rely on it for development and build time.
parseWasm
Parses wasm
binary format with useful information using webassemblyjs/wasm-parser.
1import { readFile } from "node:fs/promises"; 2import { parseWasm } from "unwasm/tools"; 3 4const source = await readFile(new URL("./examples/sum.wasm", import.meta.url)); 5const parsed = parseWasm(source); 6console.log(JSON.stringify(parsed, undefined, 2));
Example parsed result:
1{ 2 "modules": [ 3 { 4 "exports": [ 5 { 6 "id": 5, 7 "name": "rand", 8 "type": "Func" 9 }, 10 { 11 "id": 0, 12 "name": "memory", 13 "type": "Memory" 14 } 15 ], 16 "imports": [ 17 { 18 "module": "env", 19 "name": "seed", 20 "params": [], 21 "returnType": "f64" 22 } 23 ] 24 } 25 ] 26}
unwasm can automatically infer the imports object and bundle them using imports maps (read more: MDN, Node.js and WICG).
To hint to the bundler how to resolve imports needed by the .wasm
file, you need to define them in a parent package.json
file.
Example:
1{ 2 "exports": { 3 "./rand.wasm": "./rand.wasm" 4 }, 5 "imports": { 6 "env": "./env.mjs" 7 } 8}
Note: The imports can also be prefixed with #
like #env
if you like to respect Node.js conventions.
Published under the MIT license.
Made by @pi0 and community 💛
🤖 auto updated with automd
No vulnerabilities found.
No security vulnerabilities found.