generated from Alorel/basic-library-template-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
984 additions
and
1,206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,152 @@ | ||
# Placeholder | ||
# rollup-plugin-wasm-bindgen-web | ||
|
||
[![CI](https://github.com/Alorel/rollup-plugin-wasm-bindgen-web/workflows/Core/badge.svg?branch=master)](https://github.com/Alorel/rollup-plugin-wasm-bindgen-web/actions?query=workflow%3ACore+branch%3Amaster+) | ||
[![Coverage Status](https://coveralls.io/repos/github/Alorel/rollup-plugin-wasm-bindgen-web/badge.svg?branch=master)](https://coveralls.io/github/Alorel/rollup-plugin-wasm-bindgen-web) | ||
[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/Alorel/rollup-plugin-wasm-bindgen-web.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Alorel/rollup-plugin-wasm-bindgen-web/context:javascript) | ||
|
||
**Note**: `semantic-release` starts at v1, but this plugin is in early alpha and should be considered unstable and prone | ||
to breaking changes, although any such changes will follow semantic versioning. | ||
|
||
----- | ||
|
||
# Table of Contents | ||
|
||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
|
||
- [What is this](#what-is-this) | ||
- [Installation](#installation) | ||
- [Requirements & basic setup](#requirements--basic-setup) | ||
- [The transformation](#the-transformation) | ||
- [The plugin config](#the-plugin-config) | ||
|
||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
|
||
# What is this | ||
|
||
This is a rollup plugin for importing wasm files generated by [wasm-bindgen](https://github.com/rustwasm/wasm-bindgen); | ||
it's an early proof of concept and may or may not handle all scenarios. | ||
|
||
# Installation | ||
|
||
Add the registry to `.npmrc`: | ||
|
||
```bash | ||
@alorel:registry=https://npm.pkg.github.com | ||
``` | ||
|
||
Then install it: | ||
|
||
```bash | ||
npm install @alorel/rollup-plugin-wasm-bindgen-web | ||
``` | ||
|
||
# Requirements & basic setup | ||
|
||
- wasm-bindgen must output in `web` mode | ||
- the resulting js file **must be imported via dynamic import**, i.e. `import('./foo')` | ||
|
||
Given a Rust input like this: | ||
|
||
```rust | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen(js_name = multiplyByTwo)] | ||
pub fn multiply_by_two(num: i32) -> i32 { | ||
num * 2 | ||
} | ||
``` | ||
|
||
And a build command sequence like this: | ||
|
||
```bash | ||
cargo build --release --target wasm32-unknown-unknown; | ||
wasm-bindgen target/wasm32-unknown-unknown/release/my_lib.wasm --out-name my_lib --target web; | ||
``` | ||
|
||
You'd produce four files: `my_lib.js`, `my_lib.d.ts`, `my_lib_bg.wasm`, `my_lib_bg.wasm.d.ts` | ||
|
||
The file you want to be `import()`ing is `my_lib.js`; it contains an asynchronous initialiser function that the plugin | ||
will call and await on before giving you the import's response. | ||
|
||
# The transformation | ||
|
||
Whenever the plugin sees that you're importing the js file of your wasm-bindgen output, it'll emit the associated wasm | ||
file as an asset and reformat your import: | ||
|
||
```javascript | ||
import('./wasm/my_lib') | ||
.then(lib => console.log(lib.multiplyByTwo(2), "That'll be 4")); | ||
``` | ||
|
||
will get transformed into | ||
|
||
```javascript | ||
import __wasmBindgenRollupPluginDynamicImportLoader from '@alorel/rollup-plugin-wasm-bindgen-web/autoinit-wasm-import'; | ||
|
||
__wasmBindgenRollupPluginDynamicImportLoader(import('./wasm/my_lib'), 'urlOfWasmFile.wasm') | ||
.then(lib => {/* ... */ | ||
}); | ||
``` | ||
|
||
Where the loader returns the following: | ||
|
||
```javascript | ||
Promise | ||
.all([dynamicImport, fetch(url)]) | ||
.then(([wasmModule, response]) => ( | ||
wasmModule.default(response) | ||
.then(() => wasmModule) | ||
)); | ||
``` | ||
|
||
# The plugin config | ||
|
||
```javascript | ||
import {wasmBindgenPlugin} from '@alorel/rollup-plugin-wasm-bindgen-web'; | ||
|
||
export default { | ||
// ... your config | ||
plugins: [ | ||
wasmBindgenPlugin(config) | ||
] | ||
} | ||
``` | ||
|
||
Where `config` is the following interface: | ||
|
||
```typescript | ||
|
||
/** Standard Rollup filter */ | ||
interface Filter { | ||
exclude?: FilterPattern; | ||
|
||
include?: FilterPattern; | ||
} | ||
|
||
interface Opts { | ||
|
||
/** | ||
* JS source files to look for dynamic imports in. | ||
* @default {include: /\.[jt]sx?$/} | ||
*/ | ||
jsFilter?: Filter; | ||
|
||
/** | ||
* Whether to include a source map in the generated code | ||
* @default true | ||
*/ | ||
sourceMap?: boolean; | ||
|
||
/** | ||
* Filter to match dynamically imported wasm-bindgen output files | ||
* @default Don't match anything | ||
*/ | ||
wasmFilter?: Filter; | ||
} | ||
``` | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.