Skip to content

Commit

Permalink
Merge 5c75257 into 1b343f7
Browse files Browse the repository at this point in the history
  • Loading branch information
Alorel committed May 5, 2021
2 parents 1b343f7 + 5c75257 commit 527cabf
Show file tree
Hide file tree
Showing 19 changed files with 984 additions and 1,206 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module.exports = {
],
ignorePatterns: [
'**/.eslintrc.js',
'/projects/*/fixture-output',
'/projects/core/test-input.js',
'/projects/core/autoinit-wasm-import.js',
'/dist',
'/coverage',
'/**/*.d.ts',
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
os:
- ubuntu-latest
node:
- 14
- 15
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down Expand Up @@ -54,13 +54,11 @@ jobs:
fail-fast: false
matrix:
os:
# - windows-latest
# - macos-latest
- windows-latest
- ubuntu-latest
node:
- 15
- 14
- 12
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
153 changes: 152 additions & 1 deletion README.md
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
1 change: 1 addition & 0 deletions build/rollup/_syncPkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function _buildSyncPkgJson(fields) {
const _buildPkgDefaultSyncFields = [
'version',
'main',
'engines',
'module',
'esm5',
'fesm5',
Expand Down
Loading

0 comments on commit 527cabf

Please sign in to comment.