esbuild example
This example shows how to use rust-wasmpack-loader with esbuild. The loader ships an
esbuild plugin, so you import .rs files directly from your build.
The plugin inlines the compiled WebAssembly as bytes into the generated JavaScript, so there is no extra asset to handle.
It picks the build strategy from esbuild's platform option: node builds for Node.js, while browser, neutral, and
the default target a browser-like environment.
Project structureβ
esbuild-example/
βββ src/
β βββ index.js # Entry point importing the .rs module
β βββ lib.rs # Rust WebAssembly code
βββ dist/ # Build output
βββ build.mjs # esbuild build script wiring up the plugin
βββ Cargo.toml # Rust configuration
βββ package.json # Dependencies and scripts
Setupβ
1. Initialize the projectβ
mkdir my-esbuild-wasm-app
cd my-esbuild-wasm-app
npm init -y
2. Install dependenciesβ
npm install --save-dev rust-wasmpack-loader esbuild
3. Create Cargo.tomlβ
[package]
name = "esbuild-wasm-example"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2.95"
4. Create the Rust codeβ
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn fibonacci(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
_ => fibonacci(n - 1) + fibonacci(n - 2),
}
}
#[wasm_bindgen]
pub fn greet(name: &str) -> String {
format!("Hello, {}! Greetings from Rust via esbuild!", name)
}
5. Create the entry pointβ
Import the .rs file like any other module and call its exports.
import wasmModule from "./lib.rs";
console.log(wasmModule.greet("esbuild Developer"));
console.log(`fibonacci(10) = ${wasmModule.fibonacci(10)}`);
6. Create the build scriptβ
import * as esbuild from "esbuild";
import rustWasmLoader from "rust-wasmpack-loader";
await esbuild.build({
entryPoints: ["src/index.js"],
bundle: true,
format: "esm",
platform: "node",
outdir: "dist",
logLevel: "info",
plugins: [rustWasmLoader.esbuild({ logLevel: "info" })],
});
For browser output, set platform: "browser" (or leave it unset). The plugin then builds the web strategy and inlines
the WebAssembly into the bundle.
7. Update package.jsonβ
{
...,
"type": "module",
"scripts": {
"build": "node build.mjs",
"start": "node build.mjs && node dist/index.js"
},
...
}
Running the exampleβ
# Build the bundle
npm run build
# Build and run it
npm start
Plugin optionsβ
The esbuild plugin accepts the same logLevel option as the other targets:
rustWasmLoader.esbuild({
logLevel: "info", // "verbose" | "info" | "warn" | "error" | "quiet"
});
The esbuild plugin currently inlines the .wasm bytes into the generated JavaScript, so there is no separate asset to
copy or serve and no extra configuration. Separate-asset delivery may arrive as a later enhancement.