Skip to content

MaulingMonkey/cargo-html

Repository files navigation

cargo html

create self-contained HTML programs

GitHub crates.io rust: stable License
Build Status bugs / missing functionality polish enhancement

Want to run basic console programs in the browser?
Too lazy to configure CORS properly to fetch/share WASM?
Hate firing up a webserver when you'd rather just double click an HTML file?
Easily confused by advanced concepts such as "I have multiple files"?

cargo html solves all of this, by generating .html files which embed and encode their javascript, WASM, and WASI implementations directly into said HTML. No CORS, no --allow-file-access-from-files flags, no sidecar files, 100% self contained.

Quickstart

# install
cargo install cargo-html

# create a project
cargo new hello-world
cd hello-world

# build a project
cargo html

# view/run said project in the browser
start "" target/cargo-html/debug/hello-world.html

Examples

rust-mini-games

Game Issues
asylum
guess
knights #6 File I/O not implemented (panic on exit)
lord #7 Colors not supported (some terminal escape garbage)
rps
tictactoe

To reproduce the gh-pages of my fork of that repository from scratch:

# init
cargo install cargo-html
git clone --branch cargo-html-demo https://github.com/MaulingMonkey/rust-mini-games
cd rust-mini-games

# build
cargo html --release
robocopy /S target/cargo-html/release . *.html

# create branch
git checkout -b gh-pages
git add -A .
git commit -m "Updated examples"

Portability: Generated HTML

Requires:

Browser Supported Version
Chrome ✅ 67+
Chrome for Android ✅ 88+
Firefox ✅ 68+
Firefox for Android ✔️ 85+
Opera ✔️ 54+
Opera Mobile ✔️ 59+
Opera Mini ❌ None (missing WASM, Async Functions, TextEncoder, BigInt)
Safari ✔️ 14+
iOS Safari ✔️ 14.4+ (webserver required, can't load local files)
iOS Files ❌ None? (JavaScript disabled? #43)
Edge ✅ 88+
IE ❌ None (missing WASM, Async Functions, TextEncoder, BigInt)
Android Browser ✔️ 81+
UC Browser for Android ❌ None (missing WASM, BigInt)
Samsung Internet ✅ 9.2+
QQ Browser ❌ None (missing BigInt)
Baidu Browser ❌ None (missing WASM, Async Functions, BigInt)
KaiOS Browser ❌ None (missing WASM, Async Functions, BigInt)
? Legend
Tested
✔️ Should work, but untested, so probably broken - file issues!
Broken (Browsers could probably be fixed via polyfills and different codegen?)

Portability: Command Line

Build Requirements (default features):

Build OS x86_64 x86 AArch64 ARM Other
Windows ✔️ ✔️ ✔️
Linux ✔️ ✔️ ✔️ ✔️
OS X ✔️ ✔️ ✔️ ✔️ ✔️
? Legend
Tested
✔️ Should work (famous last words)

Metadata

Make cargo html ignore a package:

[package]
metadata.html           = false

Configure build behavior

[package.metadata.html]
cargo-web               = false             # default: true for bins/examples depending on stdweb
wasm-pack               = false             # default: true for cdylibs depending on wasm-bindgen
asyncify                = false             # default: true for WASI targets (bins/examples *not* depending on stdweb)
template                = "template.html"   # default: "basic", "console", "xterm", or other built-in target

WASI environment variables

[package.metadata.html.wasi.env]
CARGO_HTML      = "1"
RUST_BACKTRACE  = "1"

WASI filesystem mounts

[[package.metadata.html.mount]]
source          = "Cargo.toml"
mount           = "/Cargo.toml"

[[package.metadata.html.mount]]
source          = "../../.vscode/"
mount           = "/.vscode/"

Configure WASI behavior

[package.metadata.html.wasi]
tty.mode    = "raw", or "line-buffered"
tty.escape  = "none", or "ansi"

trap        = "fatal", "soft-debugger", "debugger", or "fatal-debugger"

determinism = "nondeterministic", or "disabled"
clock       = "nondeterministic", "disabled", or "zero"
sleep       = "nondeterministic", "disabled", "skip", or "busy-wait"
random      = "nondeterministic", "disabled", or "insecure-nondeterministic"

stdin           = "tty", "badfd", or "prompt"
stdout          = "tty", "badfd", "null", "console-log", or "console-error"
stderr          = "tty", "badfd", "null", "console-log", or "console-error"
trace_exit_0    = "tty", "badfd", "null", "console-log", or "console-error"
trace_exit_n    = "tty", "badfd", "null", "console-log", or "console-error"
trace_signal    = "tty", "badfd", "null", "console-log", or "console-error"
trace_io_error  = "tty", "badfd", "null", "console-log", or "console-error"

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.