Skip to content

A plugin for rsbuild to compile Rust crates to WebAssembly using wasm-pack

License

Notifications You must be signed in to change notification settings

im-neiru/rsbuild-plugin-wasmpack

Repository files navigation

rsbuild-plugin-wasmpack

version downloads license

rsbuild-plugin-wasmpack is a plugin for rsbuild that enables you to compile and build Rust crates into WebAssembly (Wasm) using wasm-pack.

This plugin simplifies the integration of Rust to WebAssembly in your rsbuild projects, allowing you to easily compile and bundle your Rust code for use in web applications.

Prerequisites

Before using the plugin, make sure you have:

Installation

You can add rsbuild-plugin-wasmpack as a development dependency using your preferred package manager:

npm

npm install --save-dev rsbuild-plugin-wasmpack

bun

bun add -d rsbuild-plugin-wasmpack

pnpm

pnpm add -D rsbuild-plugin-wasmpack

yarn

yarn add -D rsbuild-plugin-wasmpack

Usage

Once installed, you can add the plugin to your rsbuild configuration. Here’s an example configuration for compiling a Rust crate to WebAssembly:

Example rsbuild.config.js

import { defineConfig } from "@rsbuild/core";
import { pluginWasmPack } from "rsbuild-plugin-wasmpack";

export default defineConfig({
  plugins: [
    pluginWasmPack({
      crates: [
        {
          path:   "rust1", // The path to your Rust crate
          output: "wasm1", // The output directory for your wasm package
          target: "web",   // The target environment (e.g., 'web', 'nodejs')
        },
        {
          path:   "rust2",
          output: "wasm2",
          target: "web",
        },
      ],
    }),
  ],
});

Configuration Options

  • path (string): The path to your Rust crate or project. This is typically the folder containing Cargo.toml.

  • output (string): The directory where the generated .wasm package will be placed.

  • target (string): The WebAssembly target. See all supported targets in the wasm-pack documentation.