Skip to content

Commit

Permalink
Merge da02269 into e3515ed
Browse files Browse the repository at this point in the history
  • Loading branch information
Alorel committed May 6, 2021
2 parents e3515ed + da02269 commit f5fc3b7
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 54 deletions.
24 changes: 24 additions & 0 deletions README.md
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
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
@@ -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;
}

0 comments on commit f5fc3b7

Please sign in to comment.