Native WebAssembly loader for Webpack


#1

With the new wasm32-unknown-unknown target being available in rustc, I made a simple POC library for easily importing Rust code from Javascript (both node.js and browsers). There are other similar libraries using Emscripten that served as inspiration but I tried to keep this one quite a bit simpler in comparison.

Some tl;dr example Javascript (ES6) code:

import loadRustlib from './path/to/rustlib/src/lib.rs'

loadRustlib().then(result => {
  // This is a normal #[no_mangle] Rust function: fn add(i32, i32) -> i32
  const add = result.instance.exports['add'];
  console.log('return value was', add(2, 3));
});

I would love some feedback on the code: https://github.com/dflemstr/rust-native-wasm-loader

See published documentation on NPM: https://www.npmjs.com/package/rust-native-wasm-loader


#2

Nice! I showed this to the Webpack team, and one of them asked:


#3

Looks fine! I have a small test project and switched it to the aforementioned loader: https://github.com/yamafaktory/rust-wasm-webpack.

Edit: running yarn && yarn setup && yarn start should be enough.


#4

I have now added both wasm-bindgen and cargo-web support to this loader! See the README for short usage instructions.