rsbuild-plugin-wasmpack is a plugin for Rsbuild that enables you to compile Rust crates into WebAssembly (Wasm) using wasm-pack. It simplifies integration of Rust code into web applications with support for hot-reloading, crate aliasing, and automatic Rust toolchain management.
The demo above showcases live reloading of compiled WebAssembly as Rust code is updated.
Ensure the following are available in your environment:
If you don’t have Rust or
wasm-pack, the plugin can optionally install them for you (see Configuration Options).
Install using your package manager of choice:
npm install --save-dev rsbuild-plugin-wasmpackbun add -d rsbuild-plugin-wasmpackpnpm add -D rsbuild-plugin-wasmpackyarn add -D rsbuild-plugin-wasmpackOnce installed, you can add the plugin to your rsbuild configuration. Here’s an example configuration for compiling a Rust crate to WebAssembly:
import { defineConfig } from "@rsbuild/core";
import { pluginWasmPack } from "rsbuild-plugin-wasmpack";
export default defineConfig({
plugins: [
pluginWasmPack({
crates: [
{
path: "rust1",
target: "web",
},
{
path: "rust2",
target: "web",
profileOnDev: "profiling",
profileOnProd: "release",
},
],
wasmpackPath: "~/.cargo/bin/wasm-pack", // Optional (this can be loaded from envfile)
pkgsDir: "pkgs", // Optional (default is "pkgs")
aliasPkgDir: true, // Optional (default is true)
autoInstallWasmPack: true, // Optional
// Optional Rust auto-install setup
autoInstallRust: true,
rustToolchainOptions: {
defaultToolchain: "stable",
profile: "minimal",
components: ["clippy"],
targets: ["wasm32-unknown-unknown"],
},
}),
],
});💡 When
aliasPkgDiris enabled, an alias will be created that maps@pkgs/*to the contents ofpkgsDir(default is"pkgs"). The plugin also attempts to update yourtsconfig.jsonwith the correct alias. Be sure to check it manually if the automatic patch fails.
import initializeRust1 from "@pkgs/rust1"; // Maps to pkgs/rust1 based on pkgsDir and aliasPkgDir
import initializeRust2 from "@pkgs/rust2";
// 🔔 Note: This import alias only works if `aliasPkgDir` is enabled.
// If disabled, you must import from the actual relative path (e.g., "../pkgs/rust1").
initializeRust1().then((rust1) => {
rust1.greet("World1");
});
initializeRust2().then((rust2) => {
rust2.greet("World2");
});An array of objects representing the Rust crates you want to compile. Each object should have the following properties:
-
path(string): The path to your Rust crate or project. This is typically the folder containingCargo.toml. -
target("web" | "nodejs" | "deno"): The WebAssembly target. -
profileOnDev("dev"| "profiling" | "release"): The profile to use when building the crate in development mode. This is optional and defaults todev. -
profileOnProd("dev"| "profiling" | "release"): The profile to use when building the crate in production mode. This is optional and defaults torelease.
Custom path to the wasm-pack binary.
Defaults to the standard Cargo bin path (~/.cargo/bin/wasm-pack).
The directory where compiled Wasm output will be written.
Defaults to "pkgs".
If enabled (true by default), the plugin will:
- Create an alias mapping
@pkgsto thepkgsDir - Attempt to update your
tsconfig.jsonwith the following path:
{
"compilerOptions": {
"paths": {
"@pkgs/*": ["./pkgs/*"]
}
}
}
⚠️ You may need to manually verify this mapping if automatic insertion fails.
If true, the plugin will install wasm-pack via Cargo if it's not found in your system.
Configure how the Rust toolchain should be handled.
