Home

Awesome

<div align="center"> <a href="https://github.com/WebAssembly"> <img height="190" alt="binaryen logo" src="https://upload.wikimedia.org/wikipedia/commons/c/c6/Web_Assembly_Logo.svg"> </a> <a href="https://github.com/webpack/webpack"> <img height="200" alt="webpack logo" src="https://webpack.js.org/assets/icon-square-big.svg"> </a> </div>

npm size npm deps tests

<!-- [![node][node]][node-url] --> <!-- [![cover][cover]][cover-url] -->

webassembly-loader

this loader can also be used as a library <sup>↙ see who use this?</sup>

<sup><sup>tl;dr -- see examples</sup></sup>

Motivation

future plans

Minimum Requirements

Installation

npm install webassembly-loader --save-dev

or

yarn add webassembly-loader --dev

Options

export

How wasm code would be exported. (see examples)

<details><summary><i>webpack.config.js</i></summary>
module.exports = {
    rules: [{
          test: /\.wasm$/,
          type: "javascript/auto",
          use: [{
              loader: "webassembly-loader",
              options: {
                  export: "async"
              }
          }]
    }]
}
</details>

tips: you can use query parameter to change export mode on demand

Examples

See the test cases and example projects in *.test.ts and examples for more insight.


{export: 'buffer'}

import wasmCode from "./lib.wasm";

WebAssembly.compile(wasmCode).then(module => {
  const instance = new WebAssembly.Instance(module);
  console(instance.exports.add(1, 2)); // 3
});

{export: 'module'}

import wasmModule from "./lib.wasm";

const instance = new WebAssembly.Instance(wasmModule);
console(instance.exports.add(1, 2)); // 3

{export: 'instance'}

import wasm from "./lib.wasm";

console(wasm.exports.add(1, 2)); // 3

{export: 'async'}

import wasmInstantiate from "./lib.wasm";

wasmInstantiate(importObject | undefined).then(({ instance, module }) => {
  console(instance.exports.add(1, 2)); // 3

  // create different instance, extra will be called in different environment
  const differentInstance = new WebAssembly.Instance(module);
  console(differentInstance.exports.add(1, 2)); // 6
});

{export: 'async-instance'}

import wasmInstantiate from "./lib.wasm";

wasmInstantiate(importObject | undefined).then(instance => {
  console(instance.exports.add(1, 2)); // 3
});

{export: 'async-module'}

import wasmInstantiate from "./lib.wasm";

wasmCompile(importObject | undefined).then(module => {
  const differentInstance = new WebAssembly.Instance(module);
  console(differentInstance.exports.add(1, 2)); // 3
});

Who use this?

Contributing

Credits


License

MIT