rollup-plugin-rust
tl;dr -- see examples
This is a rollup plugin that loads Rust code so it can be interop with Javascript base project. Currently, the Rust code will be compiled as:
- WebAssembly module/instance
- Node.js addon/ffi
Requirements
- Node v8 or later
- Rollup v0.64 or later
Rust v1.28.0 with wasm32-uknown-unknown installed
rustup default 1.28.0rustup target add wasm32-unknown-unknown
This module requires a minimum of Node v8.9.0, Rollup v0.64.0, and Rust in [nightly channel][].
Getting Started
To begin, you'll need to install rollup-plugin-rust
:
npm install rollup-plugin-rust --save-dev
Then add the plugin to your rollup
config. For example:
rollup.config.js
; input: "src/main.js" output: file: "dist.index.js" format: "esm" plugins: ;
quick usage
lib.rs
pub fn add(a: i32, b: i32) -> i32 { a + b}
index.js
; { const instance = await wasm; return instanceexports;}
And run rollup
via your preferred method.
Options
export
How wasm code would be exported. This options is identical with option export
in webassembly-loader. (see examples)
// in your rollup.config.js plugins: ;
target
- Type:
String
- Default:
wasm32-unknown-unknown
- Expected value: see supported platform
The Rust target to use. Currently it only support wasm related target
// in your rollup.config.js plugins: ;
release
- Type:
Boolean
- Default:
true
Whether to compile the Rust code in debug or release mode.
// in your rollup.config.js plugins: ; // preserve debug symbol
include
- Type:
Array<string>
orstring
- Default:
['**/*.rs']
A single file, or array of files, to include when compiling.
// in your rollup.config.js plugins: ;
exclude
- Type:
Array<string>
orstring
- Default:
['node_modules/**', 'target/**']
A single file, or array of files, to exclude when linting.
// in your rollup.config.js plugins: ;
Examples
See the test cases and example projects in fixtures and examples for more insight.
TL;DR
Given this Rust code
lib.rs
pub fn add(a: i32, b: i32) -> i32 { a + b}
Cargo.toml
[package]name = "adder"version = "0.1.0"authors = ["Full Name <email@site.domain>"] [lib]crate-type = ["cdylib"]path = "lib.rs"
With options
{export: 'buffer'}
; WebAssembly;
{export: 'module'}
; const instance = wasmModule;consoleinstanceexports; // 3
{export: 'instance'}
; consolewasmexports; // 3
{export: 'async'}
extern { fn hook(c: i32);} pub fn add(a: i32, b: i32) -> i32 { hook(a + b)}
; ;
{export: 'async-instance'}
; ;
{export: 'async-module'}
; ;
Who use this?
- example-stencil-rust
- [add yours 😉]
Contributing
- CONTRIBUTING.md for how you can make contribution
- HACKING.md for technical details