Skip to content

Commit 145df59

Browse files
committed
docs(export): add description and examples 📝
[ci skip]
1 parent 6d066c5 commit 145df59

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

README.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
## Options
2+
3+
### `export`
4+
How wasm code would be exported. (see [examples](#examples))
5+
- Type: `string`
6+
- Default: `async`
7+
- Expected value:
8+
- `buffer` will export wasm code as [Buffer][]
9+
- `module` will export wasm code as [WebAssembly.Module][]
10+
- `instance` will export wasm code as [WebAssembly.Instance][]
11+
- `async` will [instantiate][webassembly.instantiate] wasm code asynchronously, return promise of both [WebAssembly.Module][] and [WebAssembly.Instance][]
12+
- `async-module` will [compile][webassembly.compile] wasm code asynchronously, return promise of [WebAssembly.Module][]
13+
- `async-instance` will [instantiate][webassembly.instantiate] wasm code asynchronously, return promise of [WebAssembly.Instance][]
14+
15+
<details><summary><i>webpack.config.js</i></summary>
16+
17+
```js
18+
module.exports = {
19+
rules: [{
20+
test: /\.wasm$/,
21+
type: "javascript/auto",
22+
use: [{
23+
loader: "webassembly-loader",
24+
options: {
25+
export: "async"
26+
}
27+
}]
28+
}]
29+
}
30+
```
31+
</details>
32+
33+
> tips: you can use [query parameter][inline] to change export mode on demand
34+
35+
[inline]: https://webpack.js.org/concepts/loaders/#inline
36+
[buffer]: https://nodejs.org/api/buffer.html
37+
[webassembly.module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module
38+
[webassembly.instance]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance
39+
[webassembly.instantiate]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate
40+
[webassembly.compile]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/compile
41+
42+
## Examples
43+
44+
See the test cases and example projects in [*.test.ts](./test) and [examples](./examples/) for more insight.
45+
46+
### With options
47+
48+
#### `{export: 'buffer'}`
49+
50+
```js
51+
import wasmCode from "./lib.wasm";
52+
53+
WebAssembly.compile(wasmCode).then(module => {
54+
const instance = new WebAssembly.Instance(module);
55+
console(instance.exports.add(1, 2)); // 3
56+
});
57+
```
58+
59+
---
60+
61+
#### `{export: 'module'}`
62+
63+
```js
64+
import wasmModule from "./lib.wasm";
65+
66+
const instance = new WebAssembly.Instance(wasmModule);
67+
console(instance.exports.add(1, 2)); // 3
68+
```
69+
70+
---
71+
72+
#### `{export: 'instance'}`
73+
74+
```js
75+
import wasm from "./lib.wasm";
76+
77+
console(wasm.exports.add(1, 2)); // 3
78+
```
79+
80+
---
81+
82+
#### `{export: 'async'}`
83+
84+
```js
85+
import wasmInstantiate from "./lib.wasm";
86+
87+
wasmInstantiate(importObject | undefined).then(({ instance, module }) => {
88+
console(instance.exports.add(1, 2)); // 3
89+
90+
// create different instance, extra will be called in different environment
91+
const differentInstance = new WebAssembly.Instance(module);
92+
console(differentInstance.exports.add(1, 2)); // 6
93+
});
94+
```
95+
96+
---
97+
98+
#### `{export: 'async-instance'}`
99+
100+
```js
101+
import wasmInstantiate from "./lib.wasm";
102+
103+
wasmInstantiate(importObject | undefined).then(instance => {
104+
console(instance.exports.add(1, 2)); // 3
105+
});
106+
```
107+
108+
---
109+
110+
#### `{export: 'async-module'}`
111+
112+
```js
113+
import wasmInstantiate from "./lib.wasm";
114+
115+
wasmCompile(importObject | undefined).then(module => {
116+
const differentInstance = new WebAssembly.Instance(module);
117+
console(differentInstance.exports.add(1, 2)); // 3
118+
});
119+
```
120+
---

0 commit comments

Comments
 (0)