wasmrust 0.3.2

Rust WebAssembly plugin for Wasmrun - compile Rust projects to WebAssembly with wasm-bindgen support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# ๐Ÿฆ€ WasmRust

[![Crates.io Version](https://img.shields.io/crates/v/wasmrust)](https://crates.io/crates/wasmrust) [![Crates.io Downloads](https://img.shields.io/crates/d/wasmrust)](https://crates.io/crates/wasmrust) [![Crates.io Downloads (latest version)](https://img.shields.io/crates/dv/wasmrust)](https://crates.io/crates/wasmrust) [![Open Source](https://img.shields.io/badge/open-source-brightgreen)](https://github.com/anistark/wasmrust) [![Contributors](https://img.shields.io/github/contributors/anistark/wasmrust)](https://github.com/anistark/wasmrust/graphs/contributors) ![maintenance-status](https://img.shields.io/badge/maintenance-actively--developed-brightgreen.svg)

![Rust](https://img.shields.io/badge/rust-%23000000.svg?style=for-the-badge&logo=rust&logoColor=white) **Rust to WebAssembly compiler plugin for [Wasmrun](https://github.com/anistark/wasmrun)**. Compile and run Rust projects to WebAssembly to run easily on any wasm based ecosystem.

## ๐Ÿ“ฆ Installation

### Primary Installation (Wasmrun Plugin)

```sh
# Install wasmrun first
cargo install wasmrun

# Install the wasmrust plugin  
wasmrun plugin install wasmrust

# Verify installation
wasmrun plugin info wasmrust
```

### Standalone Installation (Development/Testing)

```sh
# Install as standalone CLI tool
cargo install wasmrust --features cli

# Verify standalone installation
wasmrust info
```

### Library Integration

```toml
[dependencies]
wasmrust = "0.2.1"

# For wasmrun plugin development
wasmrust = { version = "0.2.1", features = ["wasmrun-integration"] }

# For CLI usage
wasmrust = { version = "0.2.1", features = ["cli"] }
```

## ๐Ÿ› ๏ธ Usage

### Primary Usage (via Wasmrun) - Recommended

Wasmrun automatically detects Rust projects and uses the wasmrust plugin:

```sh
# Automatic project detection and compilation
wasmrun ./my-rust-project

# Web application with live reload  
wasmrun ./my-yew-app --watch

# Compile with specific optimization
wasmrun compile ./my-project --optimization size

# Force Rust plugin usage (mixed projects)
wasmrun ./mixed-project --language rust

# Plugin management
wasmrun plugin info wasmrust
wasmrun plugin list
```

### Standalone Usage (Development/Testing)

For development, testing, or environments without wasmrun:

```sh
# Compile project to WebAssembly
wasmrust compile --project ./my-project --output ./dist

# Run project for execution (AOT compilation)
wasmrust run ./my-project

# Inspect project structure and dependencies
wasmrust inspect ./my-project

# Check if project is supported
wasmrust can-handle ./my-project

# Check system dependencies
wasmrust check-deps

# Clean build artifacts
wasmrust clean ./my-project

# Show supported frameworks
wasmrust frameworks
```

### Library Usage

```rust
use wasmrust::{WasmRustPlugin, CompileConfig, OptimizationLevel, TargetType};

let plugin = WasmRustPlugin::new();

// Check if project is supported
if plugin.can_handle("./my-project") {
    let config = CompileConfig {
        project_path: "./my-project".to_string(),
        output_dir: "./dist".to_string(),
        optimization: OptimizationLevel::Release,
        target_type: TargetType::WebApp,
        verbose: true,
    };
    
    match plugin.compile(&config) {
        Ok(result) => {
            println!("WASM: {}", result.wasm_path);
            if let Some(js_path) = result.js_path {
                println!("JS: {}", js_path);
            }
        }
        Err(e) => eprintln!("Compilation failed: {}", e),
    }
}
```

## ๐ŸŽฏ Supported Project Types & Frameworks

### Project Types (Auto-detected)

| Type | Description | Output | Build Tool |
|------|-------------|---------|------------|
| **Standard WASM** | Basic Rust โ†’ WebAssembly | `.wasm` file | `cargo` |
| **wasm-bindgen** | JavaScript integration | `.wasm` + `.js` | `wasm-pack` |
| **Web Application** | Full-stack web apps | Complete bundle | `trunk` / `wasm-pack` |

### Supported Web Frameworks

| Framework | Auto-Detection | Build Strategy | Status |
|-----------|----------------|----------------|---------|
| **[Yew](https://yew.rs/)** | `yew` dependency | trunk โ†’ wasm-pack | โœ… Full Support |
| **[Leptos](https://leptos.dev/)** | `leptos` dependency | trunk โ†’ wasm-pack | โœ… Full Support |
| **[Dioxus](https://dioxuslabs.com/)** | `dioxus` dependency | wasm-pack | โœ… Full Support |
| **[Sycamore](https://sycamore-rs.netlify.app/)** | `sycamore` dependency | wasm-pack | โœ… Full Support |
| **[Trunk](https://trunkrs.dev/)** | `Trunk.toml` present | trunk | โœ… Full Support |

### Framework Examples

#### Standard Rust WASM
```toml
[package]
name = "my-wasm-lib"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]
```

#### wasm-bindgen Project
```toml
[package]
name = "my-bindgen-project"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2"
web-sys = "0.3"
```

#### Yew Web Application
```toml
[package]
name = "my-yew-app"
version = "0.1.0"
edition = "2021"

[dependencies]
yew = "0.21"
wasm-bindgen = "0.2"
```

## ๐Ÿ”ง Build Strategies & Optimization

### Build Strategy Selection

WasmRust intelligently selects the optimal build strategy:

```
Project Analysis
      โ†“
Framework Detection (Yew, Leptos, etc.)
      โ†“
Build Tool Selection:
  โ€ข Standard WASM โ†’ cargo build
  โ€ข wasm-bindgen โ†’ wasm-pack  
  โ€ข Web Apps โ†’ trunk (preferred) โ†’ wasm-pack (fallback)
      โ†“
Optimization Application
      โ†“
Output Generation
```

### Optimization Levels

| Level | Compilation Time | File Size | Performance | Use Case |
|-------|------------------|-----------|-------------|----------|
| **debug** | Fast โšก | Large ๐Ÿ“ฆ | Basic โญ | Development, debugging |
| **release** | Moderate โฑ๏ธ | Medium ๐Ÿ“ฆ | Good โญโญโญ | Production builds |
| **size** | Slow ๐ŸŒ | Minimal ๐Ÿ“ฆ | Good โญโญโญ | Bandwidth-constrained |

### Advanced Optimization

```toml
# Cargo.toml optimization for smallest WASM
[profile.release]
opt-level = "s"          # Optimize for size
lto = true               # Link-time optimization
codegen-units = 1        # Slower compile, smaller binary
panic = "abort"          # Smaller binary
strip = "symbols"        # Remove debug symbols

[profile.release.package."*"]
opt-level = "s"

# Web-specific optimizations
[dependencies]
console_error_panic_hook = "0.1"
wee_alloc = "0.4"
```

## ๐Ÿ” Project Analysis & Dependencies

### Inspect Your Project

```sh
wasmrust inspect ./my-project
```

**Example Output:**
```
๐Ÿ” Analyzing Rust project...

๐Ÿ“Š Project Analysis
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
๐Ÿ“ Name: my-yew-app
๐Ÿท๏ธ  Version: 0.1.0  
๐ŸŽฏ Type: Web Application
๐Ÿ”ง Build Strategy: trunk + wasm-pack
๐ŸŒ Frameworks: yew, trunk

๐Ÿ“‹ Dependencies
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
Required:
   โœ… cargo - Rust build tool
   โœ… rustc - Rust compiler  
   โœ… wasm32-unknown-unknown - WebAssembly compilation target
   โœ… trunk - Required for web application builds

Optional:
   โœ… rustup - Rust toolchain manager
   โš ๏ธ  wasm-opt - WebAssembly optimizer

๐ŸŽ‰ Project is ready to build!
```

### System Dependencies

#### Required Tools
- **Rust Toolchain**: `rustup`, `cargo`, `rustc`
- **WASM Target**: `wasm32-unknown-unknown`

#### Optional Tools (Auto-detected)
- **wasm-pack**: For wasm-bindgen projects
- **trunk**: For web applications  
- **wasm-opt**: For additional optimization

#### Quick Installation

```sh
# Install Rust toolchain
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Add WebAssembly target
rustup target add wasm32-unknown-unknown

# Install additional tools
cargo install wasm-pack trunk wasm-opt

# Verify installation
wasmrust check-deps
```

## ๐Ÿ”„ Live Development & Watch Mode

### File Watching (via Wasmrun)

WasmRust automatically monitors:
- `src/**/*.rs` - Source files
- `Cargo.toml` - Dependencies and configuration
- `Trunk.toml` - Trunk configuration  
- `assets/`, `static/`, `public/` - Static assets
- `style.css`, `index.html` - Web assets

### Development Workflow

```sh
# Start development server with live reload
wasmrun ./my-project --watch
```

## โš™๏ธ Configuration

### Project Configuration

Create `wasmrun.toml` in your project root:

```toml
[project]
language = "rust"

[build]
optimization = "release"
target_type = "webapp"
output_dir = "./dist"

[rust]
build_strategy = "trunk"        # cargo, wasm-pack, trunk
wasm_pack_target = "web"        # web, bundler, nodejs
enable_optimization = true
custom_flags = ["--features", "web"]
```

### Global Plugin Configuration

Configure in `~/.wasmrun/config.toml`:

```toml
[external_plugins.wasmrust]
enabled = true
auto_update = true
install_path = "/home/user/.wasmrun/plugins/wasmrust"

[external_plugins.wasmrust.defaults]
optimization = "size"
verbose = false
build_strategy = "auto"
```

### Environment Variables

```sh
# Enable verbose compilation
export WASMRUST_VERBOSE=1

# Custom optimization flags
export RUSTFLAGS="-C target-feature=+simd128"

# Force build strategy
export WASMRUST_BUILD_STRATEGY=trunk
```

## ๐Ÿ”ง Plugin Architecture & Integration

### Wasmrun Plugin Interface

WasmRust implements the full Wasmrun plugin architecture:

```rust
// Plugin trait implementation
impl Plugin for WasmrustPlugin {
    fn info(&self) -> &PluginInfo;
    fn can_handle_project(&self, project_path: &str) -> bool;
    fn get_builder(&self) -> Box<dyn WasmBuilder>;
}

// Builder trait implementation  
impl WasmBuilder for WasmrustBuilder {
    fn build(&self, config: &BuildConfig) -> CompilationResult<BuildResult>;
    fn check_dependencies(&self) -> Vec<String>;
    fn validate_project(&self, project_path: &str) -> CompilationResult<()>;
    fn clean(&self, project_path: &str) -> Result<()>;
    // ... additional methods
}
```

### Dynamic Loading Support

WasmRust supports both library integration and dynamic loading:

```c
// C interface for dynamic loading
extern "C" {
    fn wasmrun_plugin_create() -> *mut c_void;
    fn wasmrust_can_handle_project(builder: *const c_void, path: *const c_char) -> bool;
    fn wasmrust_build(builder: *const c_void, config: *const BuildConfigC) -> *mut BuildResultC;
    // ... additional C functions
}
```

### Plugin Registration

```rust
// Rust integration
use wasmrust::create_plugin;

let plugin = create_plugin(); // Returns Box<dyn Plugin>

// C integration  
extern "C" fn wasmrun_plugin_create() -> *mut c_void;
```

## ๐Ÿ” Troubleshooting

### Common Issues

**"Plugin not found"**
```sh
# Verify plugin installation
wasmrun plugin list
wasmrun plugin info wasmrust

# Reinstall if needed
wasmrun plugin install wasmrust
```

**"wasm32-unknown-unknown target not found"**
```sh
rustup target add wasm32-unknown-unknown
```

**"wasm-pack not found" (for wasm-bindgen projects)**
```sh
cargo install wasm-pack
```

**"trunk not found" (for web applications)**
```sh
cargo install trunk
```

**"Compilation timeout"**
```sh
# Increase timeout for large projects
wasmrun compile ./large-project --timeout 300

# Use incremental compilation
export CARGO_INCREMENTAL=1
```

## ๐Ÿงช Testing & Development

### Running Tests

```sh
# Run all tests
cargo test

# Test wasmrun integration
cargo test --features wasmrun-integration

# Test CLI functionality
cargo test --features cli

# Integration tests (requires Rust toolchain)
cargo test test_actual_compilation -- --ignored
```

### Contributing

See [CONTRIBUTING.md](./CONTRIBUTING.md) for prerequisites, the full `just` recipe list, the project layout, the pre-PR checklist, and guidance on adding framework support.

### docs.rs Build Targets

Starting with `0.3.2`, `wasmrust` explicitly declares its docs.rs build targets in `Cargo.toml`:

```toml
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc"]
```

This aligns with the [docs.rs default-targets change](https://blog.rust-lang.org/2026/04/04/docsrs-only-default-targets/), which stops building non-default targets unless a crate opts in. Pinning the targets keeps Linux and Windows documentation available on docs.rs without relying on the previous implicit behavior.

## ๐Ÿ“Š Benchmarks & Performance

### Compilation Speed

| Project Type | Debug | Release | Size |
|-------------|-------|---------|------|
| Simple WASM | ~5s | ~15s | ~25s |
| wasm-bindgen | ~10s | ~30s | ~45s |
| Yew App | ~15s | ~45s | ~60s |

### Output Size Comparison

| Optimization | Simple WASM | wasm-bindgen | Yew App |
|-------------|-------------|--------------|---------|
| debug | ~500KB | ~800KB | ~1.2MB |
| release | ~200KB | ~400KB | ~600KB |
| size | ~100KB | ~250KB | ~400KB |

*Benchmarks on Apple M1, Rust 1.70, realistic projects*

## ๐Ÿ”— Related Projects & Ecosystem

### Core Dependencies
- **[wasm-bindgen](https://github.com/rustwasm/wasm-bindgen)** - JavaScript integration
- **[wasm-pack](https://github.com/rustwasm/wasm-pack)** - WebAssembly toolkit
- **[trunk](https://github.com/thedodd/trunk)** - Web application bundler

### Web Frameworks
- **[Yew](https://github.com/yewstack/yew)** - Modern Rust web framework
- **[Leptos](https://github.com/leptos-rs/leptos)** - Full-stack Rust framework
- **[Dioxus](https://github.com/dioxuslabs/dioxus)** - Cross-platform GUI library
- **[Sycamore](https://github.com/sycamore-rs/sycamore)** - Reactive web library

### Related Tools
- **[Wasmrun](https://github.com/anistark/wasmrun)** - Universal WebAssembly runtime
- **[WasmGo](https://github.com/anistark/wasmgo)** - Go WebAssembly plugin
- **[binaryen](https://github.com/WebAssembly/binaryen)** - WebAssembly optimizer

## ๐Ÿ“„ License

[MIT License](./LICENSE) - see the LICENSE file for details.

## ๐Ÿค Contributing & Community

### Getting Help

- **GitHub Issues**: [Report bugs or request features](https://github.com/anistark/wasmrust/issues)
- **Discussions**: [Community discussions](https://github.com/anistark/wasmrust/discussions)
- **Wasmrun Discord**: [Join the community](https://discord.gg/wasmrun)

### Contributing

We welcome contributions! See [CONTRIBUTING.md](./CONTRIBUTING.md) for:
- Code contribution guidelines
- Development setup instructions
- Testing requirements
- Documentation standards

**Made with โค๏ธ for the Rust and WebAssembly communities**

*โญ If you find WasmRust useful, please consider starring the repository!*