rollup-plugin-rust
raw JSON → 1.2.0 verified Mon Apr 27 auth: no javascript
A Rollup plugin that compiles Rust code into WebAssembly modules, enabling seamless interop between Rust and JavaScript projects. Version 1.2.0, currently in active development with no recent updates. Requires Node >= 8.9.0, Rollup >= 0.64.0, and Rust nightly with wasm32-unknown-unknown target. Supports various export modes (WebAssembly.Instance, WebAssembly.Module, buffer), release/debug builds, and custom include/exclude patterns. Differentiates from other WASM plugins by focusing specifically on Rust compilation via the rustc compiler, offering tight integration with Rollup's build pipeline.
Common errors
error Error: Could not resolve 'lib.rs' from 'src/main.js' ↓
cause The plugin's include pattern does not cover the Rust file, or the file path is incorrect.
fix
Ensure 'lib.rs' exists at the specified path and that 'include' option in plugin configuration matches the file.
error Error: wasm32-unknown-unknown target is not installed ↓
cause The required Rust compilation target is missing.
fix
Run 'rustup target add wasm32-unknown-unknown' and ensure Rust is on nightly channel.
error TypeError: rust is not a function ↓
cause Improper import; using CommonJS require without .default or importing as named export.
fix
Use 'import rust from "rollup-plugin-rust"' or 'const rust = require("rollup-plugin-rust").default'.
error Error: rollup-plugin-rust: No Rust source file provided ↓
cause The plugin did not match any Rust files in the Rollup build.
fix
Verify that your import statement points to a .rs file and the file exists.
Warnings
breaking Requires Rust nightly channel to compile WASM targets. ↓
fix Use 'rustup default nightly' and install wasm32-unknown-unknown target.
gotcha Only supports wasm-related targets (wasm32-unknown-unknown, wasm32-unknown-emscripten). ↓
fix Set 'target' option to a valid WASM target; other targets will fail compilation.
gotcha The plugin compiles Rust code via rustc, which can be slow on first run due to crate compilation. ↓
fix Consider using caching or incremental compilation in Rust (nightly feature).
deprecated Node v8 is EOL; plugin may not work on newer Node versions due to dependencies. ↓
fix Upgrade Node to v12 or later and test compatibility; consider using an alternative plugin with newer maintenance.
gotcha Import path must point to a .rs file; the plugin resolves Rust files by extension. ↓
fix Use './lib.rs' (with extension) in import statements.
Install
npm install rollup-plugin-rust yarn add rollup-plugin-rust pnpm add rollup-plugin-rust Imports
- default wrong
const rust = require('rollup-plugin-rust').defaultcorrectimport rust from 'rollup-plugin-rust' - rust (default import with options) wrong
import { rust } from 'rollup-plugin-rust'correctimport rust from 'rollup-plugin-rust'; rust({ release: false }) - RustOptions (type)
import type { RustOptions } from 'rollup-plugin-rust' - RustPlugin (type alias)
import type { RustPlugin } from 'rollup-plugin-rust'
Quickstart
// rollup.config.js
import rust from 'rollup-plugin-rust';
export default {
input: 'src/main.js',
output: {
file: 'dist/bundle.js',
format: 'esm'
},
plugins: [rust({ release: true })]
};
// lib.rs
// #[no_mangle]
// pub fn add(a: i32, b: i32) -> i32 {
// a + b
// }
// src/main.js
import wasm from './lib.rs';
export async function increment(a) {
const { instance } = await wasm;
return instance.exports.add(1, a);
}