UNPKG

2.68 kBMarkdownView Raw
1[npm]: https://img.shields.io/npm/v/@rollup/plugin-wasm
2[npm-url]: https://www.npmjs.com/package/@rollup/plugin-wasm
3[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-wasm
4[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-wasm
5
6[![npm][npm]][npm-url]
7[![size][size]][size-url]
8[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)
9
10# @rollup/plugin-wasm
11
12🍣 A Rollup which allows importing and bundling [WebAssembly modules](http://webassembly.org).
13
14WebAssembly Modules are imported asynchronous as base64 strings. Small modules [can be imported synchronously](#synchronous-modules).
15
16## Requirements
17
18This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.
19
20## Install
21
22Using npm:
23
24```console
25npm install @rollup/plugin-wasm --save-dev
26```
27
28## Usage
29
30Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
31
32```js
33import { wasm } from '@rollup/plugin-wasm';
34
35export default {
36 input: 'src/index.js',
37 output: {
38 dir: 'output',
39 format: 'cjs'
40 },
41 plugins: [wasm()]
42};
43```
44
45Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).
46
47## Options
48
49### `sync`
50
51Type: `Array[...String]`<br>
52Default: `null`
53
54Specifies an array of strings that each represent a WebAssembly file to load synchronously. See [Synchronous Modules](#synchronous-modules) for a functional example.
55
56## WebAssembly Example
57
58Given the following simple C file:
59
60```c
61int main() {
62 return 42;
63}
64```
65
66Compile the file using `emscripten`, or the online [WasmFiddle](https://wasdk.github.io/WasmFiddle//) tool. Then import and instantiate the resulting file:
67
68```js
69import sample from './sample.wasm';
70
71sample({ ...imports }).then(({ instance }) => {
72 console.log(instance.exports.main());
73});
74```
75
76The WebAssembly is inlined as a base64 encoded string. At runtime the string is decoded and a module is returned.
77
78_Note: The base64 string that represents the WebAssembly within the bundle will be ~33% larger than the original file._
79
80### Synchronous Modules
81
82Small modules (< 4KB) can be compiled synchronously by specifying them in the configuration.
83
84```js
85wasm({
86 sync: ['web/sample.wasm', 'web/foobar.wasm']
87});
88```
89
90This means that the exports can be accessed immediately.
91
92```js
93import sample from './sample.wasm';
94
95const instance = sample({ ...imports });
96
97console.log(instance.exports.main());
98```
99
100## Meta
101
102[CONTRIBUTING](/.github/CONTRIBUTING.md)
103
104[LICENSE (MIT)](/LICENSE)
105
\No newline at end of file