Parcel 1.x wasm-bindgen plugin for Rust
parcel-plugin-wasm.rs is a Parcel 1.x bundler plugin designed to integrate Rust projects compiled to WebAssembly (Wasm) using `wasm-bindgen`. It streamlines the development workflow by automatically compiling Rust code with `wasm-pack` when Parcel 1.x builds the project. The latest stable version available is 1.3.0. A key differentiator was its ability to provide zero-configuration Rust/Wasm compilation for Parcel projects at a time when such integration was less mature. However, it is explicitly designed for `parcel-bundler` (Parcel 1.x), which has been officially deprecated and is no longer maintained. Parcel 2.x includes its own `@parcel/transformer-rust` for native Rust/Wasm support, rendering this plugin obsolete for modern Parcel environments.
Common errors
-
Error: Cannot find module 'path/to/Cargo.toml'
cause Parcel 1.x cannot locate or process the Rust crate defined by the `Cargo.toml` path, often due to incorrect pathing, missing `parcel-plugin-wasm.rs` in `devDependencies`, or issues with `cargo`/`wasm-pack` installation preventing the WASM module from being generated.fixVerify the path to `Cargo.toml` is correct. Ensure `parcel-plugin-wasm.rs` is listed in `devDependencies` in `package.json`. Confirm that `cargo` and `wasm-pack` are installed and working. Check Parcel's build logs for underlying Rust/Wasm compilation errors. -
error: linker `link.exe` not found
cause Rust compilation failed because the linker required by the Rust toolchain is not found. This often occurs on Windows systems if a C++ build environment (like MSVC) is not installed or configured correctly.fixInstall the appropriate C++ build tools for your platform, usually by installing 'Desktop development with C++' from Visual Studio Installer on Windows, or `build-essential` on Linux/macOS. Ensure the Rust toolchain is properly configured for your target. -
🚨 Error writing to cache: ENOENT: no such file or directory, stat 'build.rs'
cause This error, reported in issue #37 on the plugin's GitHub, indicates a problem with Parcel's caching mechanism interacting with Rust's build system, possibly related to `build.rs` files or project structure.fixTry clearing Parcel's cache (`rm -rf .parcel-cache dist`) and rebuilding. Review your Rust project's `build.rs` or `Cargo.toml` for any non-standard configurations. This may also be a plugin-specific bug that has not been resolved.
Warnings
- breaking This plugin targets `parcel-bundler` (Parcel 1.x), which is officially unmaintained. It is not compatible with Parcel 2.x. Using it with Parcel 2.x will lead to build failures or incorrect behavior. Parcel 2.x has its own native support for Rust/Wasm via `@parcel/transformer-rust`.
- gotcha Requires external tools `cargo` and `wasm-pack` to be installed globally on the system where Parcel is running. Missing these tools will result in compilation errors during the Parcel build process.
- gotcha The `WASM_PACK_PROFILE` environment variable is used to control the `wasm-pack build` profile (e.g., `dev`, `release`). This variable needs to be set in the shell before running Parcel commands.
Install
-
npm install parcel-plugin-wasm.rs -
yarn add parcel-plugin-wasm.rs -
pnpm add parcel-plugin-wasm.rs
Imports
- foo
import { foo } from 'path/to/Cargo.toml' - lib
import lib from 'path/to/Cargo.toml'
- WasmModule
import type { WasmModule } from 'path/to/Cargo.toml'
Quickstart
/* file: my-crate/src/lib.rs */
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
/* file: src/index.js */
import { greet } from '../my-crate/Cargo.toml';
document.getElementById('app').innerHTML = `
<h1>Rust + WebAssembly in Parcel 1.x</h1>
<p>${greet('Parcel user')}</p>
`;
console.log('Greeting from Rust:', greet('world'));
/* file: package.json (relevant part) */
{
"name": "my-parcel-wasm-app",
"version": "1.0.0",
"devDependencies": {
"parcel-bundler": "^1.12.5",
"parcel-plugin-wasm.rs": "^1.3.0"
},
"scripts": {
"start": "parcel src/index.html",
"build": "parcel build src/index.html"
}
}
/* file: src/index.html */
<!DOCTYPE html>
<html>
<head>
<title>Parcel WASM Example</title>
</head>
<body>
<div id="app"></div>
<script src="index.js"></script>
</body>
</html>