A plugin for Trunk that allows in-line Rust scripts declared using RFC 3424.
This is just a proof of concept and is riddled with bugs and untested edge cases. Please don't actually use this!
- Compile this project using
cargo build
and place the executable wherever is desirable. - Add a post-build hook launching the executable to your
Trunk.toml
file.
[[hooks]]
stage = "post_build"
command = "trunk_inline_rust"
- Add a
<script type="rust">
element to your HTML document and start writing Rust!
<script type="rust">
//! ```cargo
//! [dependencies]
//! wasm-bindgen = "0.2.92"
//! ```
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}
#[wasm_bindgen(start)]
fn run() {
alert("Hello World!");
}
</script>
Each script block has access to declaring its own Cargo.toml
using RFC 3424 syntax. No dependencies are included by default, so wasm-bindgen
is recommended!
Any elements marked as pub
within a script block will be made available on the Window
context, allowing for immediate use in the rest of the document:
<script type="rust">
//! ```cargo
//! [dependencies]
//! wasm-bindgen = "0.2.92"
//! ```
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn rusty_alert(input: &str) {
alert(format!("Rusty Alert: {}", input).as_str());
}
</script>
<button onclick="rusty_alert('Clicked!')">Click me</button>