A rollup plugin that inlines (base64 encoded) and imports WebAssembly modules
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
test
.babelrc
.gitignore
.npmignore
.travis.yml
README.md
package-lock.json
package.json
rollup.config.js

README.md

deprecated!!! rollup-plugin-webassembly

This plugin is deprecated - I've joined forces with an author of a similar plugin, to work on an official plugin https://github.com/rollup/rollup-plugin-wasm

semantic-release travis

A rollup plugin that inlines (base64 encoded) and imports WebAssembly modules.

import wasm from './hello.wasm';

wasm()
  .then(instance =>
    console.log(instance.exports.main())
  );

This plugin is particularly useful if you want to distribute a library or framework that mixes WebAssembly and JavaScript code. Note, the WebAssembly JavaScript API compiles WebAssembly modules asynchronously, hence the above code returns the module via a Promise.

Installation

Install via npm:

npm install --save-dev rollup-plugin-webassembly

Configuration

Simply add the plugin to your rollup config. Any imported file with the wasm extension will be processed by this plugin.

import { rollup } from 'rollup';
import webAssembly from 'rollup-plugin-webassembly';

rollup({
  entry: 'main.js',
  plugins: [
    webAssembly()
  ]
});

This plugin also supports the standard include / exclude pattern:

rollup({
  entry: 'main.js',
  plugins: [
    webAssembly({
      // All wasm files will be parsed by default,
      // but you can also specifically include/exclude files
      include: 'node_modules/**',  // Default: undefined
      exclude: [ 'node_modules/foo/**', 'node_modules/bar/**' ],  // Default: undefined
    })
  ]
});

Example

Given the following simple C file, compiled to wasm (using emscripten, or the online WasmFiddle tool):

int main() {
  return 42;
}

The plugin will look for any wasm imports. For any it finds, the wasm file is inlined as a base64 encoded string (which means it will be ~33% larger than the original). The string is decoded and asynchronously compiled into a wasm module, which is returned by the import"

import wasm from './hello.wasm';

wasm()
  .then(instance =>
    console.log(instance.exports.main())
  );

If your wasm module requires imports, these can be supplied as the first argument to the wasm function:

import wasm from './hello.wasm';

wasm({
    env: {
      log: (value) => console.log(value) }
    }
  })
  .then(instance => {
    ...
  });