Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add exposeWasm option #2

Merged
merged 1 commit into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ to breaking changes, although any such changes will follow semantic versioning.
- [Requirements & basic setup](#requirements--basic-setup)
- [The transformation](#the-transformation)
- [The plugin config](#the-plugin-config)
- [Filtering](#filtering)
- [Raw wasm output](#raw-wasm-output)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Expand Down Expand Up @@ -126,6 +128,13 @@ interface Filter {

interface Opts {

/**
* When set to true, adds a __getWasm() function to the import() response which returns what the original
* init function returned
* @default false
*/
exposeWasm?: boolean;

/**
* JS source files to look for dynamic imports in.
* @default {include: /\.[jt]sx?$/}
Expand All @@ -146,7 +155,22 @@ interface Opts {
}
```

## Filtering

There are two sets of files to match. Using the `my_lib` example from earlier,

- `wasmFilter` should match `my_lib.js`
- `jsFilter` should match files that'll contain the dynamic `import()`s

## Raw wasm output

By default, the response of the original init function is hidden. You can turn the `exposeWasm` option on to append
a `__getWasm` function to the module which will return it. If you're using Typescript, you can correct your typings by
adding the following to your Rust source files (constant name doesn't matter):

```rust
#[wasm_bindgen(typescript_custom_section)]
const TS_APPEND_CONTENT: &'static str = r#"
export function __getWasm(): InitOutput;
"#;
```
82 changes: 64 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@alorel/rollup-plugin-copy": "^1.0.2",
"@alorel/rollup-plugin-copy-pkg-json": "^1.0.3",
"@alorel/rollup-plugin-dts": "^2.0.1",
"@rollup/plugin-alias": "^3.1.2",
"@rollup/plugin-node-resolve": "^13.0.0",
"@rollup/plugin-replace": "^2.4.2",
"@rollup/pluginutils": "^4.1.0",
Expand Down
33 changes: 23 additions & 10 deletions projects/core/autoinit-wasm-import.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
export default function autoinitWasmImport(imp, url) {
return Promise
.all([
imp,
fetch(url)
])
.then(function wasmImportInitialiser([wasmModule, response]) {
return wasmModule.default(response)
.then(() => wasmModule);
});
function createGetter(initResponse) {
const getter = {};
Object.defineProperty(getter, '__getWasm', {
value: function __getWasm() {
return initResponse;
}
});

return getter;
}

/**
* @param {Promise<any>} imp - The dynamic import
* @param {string} url - The wasm file's URL
* @param {boolean} exposeWasm - Whether to expose the init function's response or not
*/
export default async function autoinitWasmImport(imp, url, exposeWasm) {
const [wasmModule, fetchResponse] = await Promise.all([imp, fetch(url)]);
const initResponse = await wasmModule.default(fetchResponse);

return exposeWasm ?
Object.assign(createGetter(initResponse), wasmModule) :
wasmModule;
}
Loading