A minimal starter template for building high-performance web apps with Svelte on the frontend and Rust compiled to WebAssembly for compute-heavy tasks.
| Technology | Role |
|---|---|
| Svelte | Reactive UI with minimal boilerplate |
| Rust | High-performance logic compiled to WASM |
| Vite | Lightning-fast dev server with HMR |
| Bun | Fast JavaScript runtime and package manager |
| rsw | Automatic Rust hot-reload on save |
Perfect for creative coding, data visualization, games, simulations, or any project where JavaScript performance isn't enough.
# Install wasm-pack
cargo install wasm-pack
# Install rsw CLI
cargo install rsw
# Clone this repo
git clone https://github.com/YOUR_USERNAME/svelte-rust-wasm-starter.git
cd svelte-rust-wasm-starter
# Install dependencies
bun install
# Run dev server (Rust hot-reload enabled)
bun run dev
.
āāā src/
ā āāā App.svelte # Main Svelte component
ā āāā main.ts # Entry point
ā āāā app.css # Global styles
āāā wasm-lib/ # Rust crate
ā āāā Cargo.toml
ā āāā src/
ā āāā lib.rs # Rust functions exposed to JS
āāā vite.config.ts
āāā rsw.toml # Rust-WASM config
āāā package.json
If you want to create this from zero:
bun create vite my-project --template svelte-ts
cd my-project
bun install
bun add -d vite-plugin-rsw concurrently
bunx rsw init
bunx rsw new wasm-lib
[new]
using = "wasm-pack"
cli = "bun"
[[crates]]
name = "wasm-lib"
link = true
import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import { ViteRsw } from 'vite-plugin-rsw'
export default defineConfig({
plugins: [svelte(), ViteRsw()]
})
{
"scripts": {
"dev": "rsw build && concurrently \"rsw watch\" \"vite\"",
"build": "rsw build && vite build",
"preview": "vite preview"
}
}
Note: The dev script runs an initial rsw build before starting the concurrent processes. This ensures the WASM package is built before Vite tries to resolve imports, preventing "Failed to resolve import" errors on first run.
Edit wasm-lib/src/lib.rs:
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 process_pixels(data: &mut [u8]) {
// Manipulate image data at native speed
for pixel in data.chunks_mut(4) {
pixel[0] = 255 - pixel[0]; // Invert R
pixel[1] = 255 - pixel[1]; // Invert G
pixel[2] = 255 - pixel[2]; // Invert B
}
}
<script lang="ts">
import init, { fibonacci, process_pixels } from 'wasm-lib'
import { onMount } from 'svelte'
let result = 0
onMount(async () => {
await init()
result = fibonacci(40) // Fast even for large numbers
})
</script>
<p>fibonacci(40) = {result}</p>
Edit wasm-lib/Cargo.toml:
[dependencies]
wasm-bindgen = "0.2"
noise = "0.9" # Perlin noise
nalgebra = "0.32" # Linear algebra
rand = "0.8" # Random numbers
| Command | Description |
|---|---|
bun run dev |
Start dev server with Rust hot-reload |
bun run build |
Build for production |
bun run preview |
Preview production build |
This happens when Vite starts before the initial WASM build completes. The updated dev script (rsw build && concurrently...) fixes this by ensuring an initial build runs first.
If you still see this error:
bun run devcargo install wasm-pack
cargo install rsw
Make sure rsw watch is running. Check the terminal for Rust compilation errors.
MIT