Skip to content

Commit 7830335

Browse files
committed
feat(export): add option to export wasm code as X
X can be 🐣 'buffer' will export wasm code as Buffer class 🐥 'module' will export wasm code as WebAssembly.Module 🐤 'instance' will export wasm code as WebAssembly.Instance 🐔 'promise' will "instantiate" wasm code asynchronously first attempt, no test yet, so [skip ci]
1 parent 0da8b30 commit 7830335

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

flow-typed/rollupPluginRustDef.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
declare type Options = {
2+
export: 'buffer' | 'instance' | 'module' | 'promise',
23
include?: string[] | string, // rollup recommend to add this
34
exclude?: string[] | string // rollup recommend to add this
45
};

src/main.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// @flow
22
import { createFilter } from 'rollup-pluginutils';
3+
import wrap from './wrapper';
34
import predef from './options';
45

56
const extension = /\.rs$/; // rust file extension
67

78
export default function(options: Options = predef) {
8-
options = Object.assign(options, predef); // diff and merge with predefined options
9+
options = Object.assign(predef, options); // diff and merge with predefined options
910
const filter = createFilter(options.include, options.exclude);
1011

1112
return {
@@ -14,17 +15,17 @@ export default function(options: Options = predef) {
1415
if (!extension.test(id)) return;
1516
if (!filter(id)) return;
1617

17-
const wasmCode = Buffer.from([
18-
0x00,
19-
0x61,
20-
0x73,
21-
0x6d,
22-
0x01,
23-
0,
24-
0,
25-
0
26-
]).toJSON().data;
27-
return `export default Buffer.from([${wasmCode.toString()}])`;
18+
const wasmCode = Buffer.from([0x00, 0x61, 0x73, 0x6d, 0x01, 0, 0, 0]);
19+
switch (options.export) {
20+
case 'buffer':
21+
return wrap(wasmCode).asBuffer;
22+
case 'instance':
23+
return wrap(wasmCode).asWebAssembly.Instance;
24+
case 'module':
25+
return wrap(wasmCode).asWebAssembly.Module;
26+
case 'promise':
27+
return wrap(wasmCode).promiseWebAssembly;
28+
}
2829
}
2930
};
3031
}

src/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @flow
22
/** Default options */
33
export default ({
4+
export: 'promise',
45
include: ['**/*.rs'],
56
exclude: ['node_modules/**', 'target/**']
67
}: Options);

src/wrapper.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// @flow
2+
3+
/** Wrap binary data as es6 module so it can be imported by rollup
4+
* @param buffer raw binary data to be wrapped as es6 module
5+
* @return chainable object which represent `wrap this data as...`
6+
* @example return wrap(arrayBuffer).asWebAssembly.Module
7+
*/
8+
export default function(buffer: Buffer) {
9+
const data = buffer.toJSON().data.toString();
10+
return {
11+
asBuffer: `export default Buffer.from([${data}])`,
12+
asWebAssembly: {
13+
Module: `export default new WebAssembly.Module(
14+
Buffer.from([${data}])
15+
)`,
16+
Instance: `export default new WebAssembly.Instance(
17+
new WebAssembly.Module(
18+
Buffer.from([${data}])
19+
)
20+
)`
21+
},
22+
promiseWebAssembly: `export default WebAssembly.instantiate(
23+
Buffer.from([${data}])
24+
)`
25+
};
26+
}

0 commit comments

Comments
 (0)