Skip to content

A Webpack 4 📦 loader for wasm bindgen Rust code

License

Notifications You must be signed in to change notification settings

cwheel/rust-bindgen-loader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rust-bindgen-loader

A Webpack 4 loader to use bindgen'd Rust code from Javascript. This project was inspired by rust-native-wasm-loader. However, unlike rust-native-wasm-loader, Rust built with wasm bindgen can be imported in the same manner as Javascript; no need for explicit dynamic imports (dynamic imports are automatically generated).

Usage

You'll need Rust nightly setup with support for wasm32-unknown-unknown. Check out the docs for more info.

Install from npm: npm i --save-dev rust-bindgen-loader

Add to Webpack as a loader for .rs files:

{
    test: /\.rs$/,
    use: 'rust-bindgen-loader',
}

Create a Rust project: cargo init --lib myCrate

Add wasm-bindgen as a dependency of the crate (in Cargo.toml): wasm-bindgen = "0.2.22"

Add something like the following to lib.rs:

extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

And add something like the following in Javascript:

import { wasmBooted, add } from './src/lib.rs';

wasmBooted.then(() => {
    console.log(add(1,2));
});

In your Babel configuration, you'll also need to include dynamic-import to properly compile the code generated by the rust-bindgen-loader.

Loader options

  • release (boolean) - Build the imported crate in release mode
  • buildArgs (array) - Additional arguments to pass to cargo during the build

About

A Webpack 4 📦 loader for wasm bindgen Rust code

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published