ColinEberhardt/rollup-plugin-webassembly(opens in a new tab)
Language: JavaScript
A rollup plugin that inlines (base64 encoded) and imports WebAssembly modules
- stars
- 2
- watchers
- 1
- forks
- 0
- open issues
- 1
- Last updated
- 12/15/2017
Language: JavaScript
A rollup plugin that inlines (base64 encoded) and imports WebAssembly modules
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
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.
Install via npm:
npm install --save-dev rollup-plugin-webassembly
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 }) ] });
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 => { ... });