This project demonstrates:
- How to bundle WebAssembly (WASM) modules written in Rust into a web application using Vite bundler
- How to organize a monorepo using pnpm workspaces to manage WASM, library, and application packages
Before you begin, ensure you have the following installed:
.
├── packages/
│ ├── app/ # Frontend application
│ └── library/ # TypeScript library that uses and reexports the wasm
│ └── wasm/ # Generated WASM output
├── src/ # Rust source code
│ └── lib.rs # Rust WASM module source
├── package.json # Workspace configuration
└── pnpm-workspace.yaml # PNPM workspace configuration
The project uses pnpm workspaces to organize the codebase into three main parts:
- Rust WASM module (root/src)
- TypeScript wrapper library (packages/library)
- Application (packages/app)
This workspace structure allows for:
- Independent versioning of packages
- Simplified dependency management
- Local package linking for development
- Centralized build scripts
- Clone the repository:
git clone <repository-url>
cd <project-directory>- Install dependencies:
pnpm install- Build the project:
pnpm run buildThis command executes the following steps:
build:wasm: Compiles Rust code to WebAssemblybuild:library: Builds the TypeScript wrapper librarybuild:app: Builds the application
- Run the script with node:
pnpm run startThe Rust code in src/lib.rs contains the WebAssembly module implementation. When you run build:wasm, wasm-pack compiles this code and generates:
- WebAssembly binary (
.wasm) - JavaScript bindings
- TypeScript type definitions
These files are output to packages/library/wasm/.
Located in packages/library, this wrapper provides:
- Type-safe interface to the WASM module
- Additional utility functions
- Easy integration with JavaScript/TypeScript applications
The frontend application in packages/app demonstrates:
- How to import and initialize the WASM module
- Integration with the TypeScript library
- Usage of WASM functions in a web application
pnpm run build: Build all componentspnpm run build:wasm: Build only the WASM modulepnpm run build:library: Build only the TypeScript librarypnpm run build:app: Build only the frontend applicationpnpm run start: Start the development server
ISC