rust-wasmpack-loader
rust-wasmpack-loader compiles .rs (Rust) files to WebAssembly at build time and gives you back JavaScript that exposes
the Rust exports. You import a .rs file the way you would import any module, and the loader handles the wasm-pack
compilation and the glue code.
Supported build toolsβ
- Webpack
^5.0.0(webandnode/node-asynctargets) - Rspack
>=1.0.0(the same Webpack-compatible loader,webandnodetargets) - Bun runtime (
nodetarget only) - esbuild plugin (
nodeandwebtargets, WASM inlined) - Rollup plugin (
nodeandwebtargets, WASM inlined) - Vite plugin (SSR uses the
nodestrategy, the client usesweb, with an emitted.wasmasset on production builds) - Next.js App Router through the
withRustWasmhelper (one.rsworks from Server Components, Client Components, and Edge routes;nodeon the server andwebon the client with bytes inlined, and a pre-compiled module on Edge) - Electron apps with Webpack (
electron-main/electron-preloaduse thenodestrategy,electron-rendererusesweb, bytes inlined)
What it doesβ
- Import
.rsfiles directly in your JavaScript or TypeScript. There is no separate build step to wire up. - Works with
wasm_bindgenexports and with plain functions. - Finds the nearest
Cargo.tomlby walking up from the.rsfile, so you do not configure the crate path by hand. - Builds for
webandnodetargets, and across the bundlers listed above. - Types
.rsimports in TypeScript: an ambient floor keeps imports valid, and generated.d.rs.tssidecars (plus an editor Language Service plugin) give the exact#[wasm_bindgen]signatures.
You write computation-heavy code in Rust, pull in crates from the Rust ecosystem, and call the result from JavaScript without managing a separate compile-and-link pipeline.
Quick exampleβ
// Import Rust code directly
import wasmModule from './fibonacci.rs';
// Use the compiled WASM module
const result = wasmModule.fibonacci(10);
console.log(result); // Output: 55
// fibonacci.rs
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn fibonacci(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
_ => fibonacci(n - 1) + fibonacci(n - 2),
}
}
Getting startedβ
Examplesβ
View the examples documentation.
The example folder in the repository shows how the loader works across different setups:
- Web Webpack - browser applications
- Node.js Webpack - server-side applications
- Rspack - the Webpack-compatible loader on Rspack
- Bun Node.js - the Bun runtime
- esbuild - bundling for Node.js or the browser
- Rollup - bundling for Node.js or the browser with the Rollup plugin
- Vite - SSR and client builds with the Vite plugin
- Next.js - App Router with Server and Client
Components using the
withRustWasmhelper - Electron - main and renderer processes with Webpack
Contributingβ
Contributions are welcome. See CONTRIBUTING.md for how to get started. Bug reports, feature suggestions, documentation fixes, and pull requests all help.
Acknowledgementsβ
Thanks to the projects this one builds on:
- @wasm-tool/wasm-pack-plugin - the original inspiration for the WebAssembly integration
- wasm-pack - the tool that handles the Rust-to-WASM compilation
Licenseβ
This project is licensed under the MIT License.
See the LICENSE file for details.