8个版本
| 0.2.1 | 2021年3月10日 |
|---|---|
| 0.2.0 | 2021年2月28日 |
| 0.1.5 | 2021年2月15日 |
在WebAssembly中排名#968
在r-tool中使用
195KB
4K SLoC
cargohtml
创建自包含的HTML程序
想在浏览器中运行基本的控制台程序吗?
懒得正确配置CORS来获取/共享WASM吗?
讨厌启动web服务器,而你只想双击HTML文件?
被诸如“我有多个文件”等高级概念弄糊涂了吗?
cargo html通过生成直接将javascript、WASM和WASI实现嵌入并编码到HTML中的.html文件来解决所有这些问题。没有CORS,没有--allow-file-access-from-files标志,没有辅助文件,100%自包含。
快速入门
# 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
示例
rust-mini-games
- 如HN上所述:https://news.ycombinator.com/item?id=26089539
- 上游:https://github.com/Syn-Nine/rust-mini-games
- 补丁:https://github.com/MaulingMonkey/rust-mini-games
| 游戏 | 问题 |
|---|---|
| asylum | |
| guess | |
| knights | #6 文件I/O未实现(退出时恐慌) |
| lord | #7 不支持颜色(一些终端转义垃圾) |
| rps | |
| tictactoe |
从头开始重现我从中仓库分叉的gh-pages
# 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"
可移植性:生成的HTML
需要
- wasm
- async-functions
- bigint
- promises
- textencoder
- 通用酷炫功能。
| 浏览器 | 支持版本 |
|---|---|
| Chrome | ✅ 67+ |
| Chrome for Android | ✅ 88+ |
| Firefox | ✅ 68+ |
| Firefox for Android | ✔️ 85+ |
| Opera | ✔️ 54+ |
| Opera Mobile | ✔️ 59+ |
| Opera Mini | ❌ 无(缺少WASM、Async Functions、TextEncoder、BigInt) |
| Safari | ✔️ 14+ |
| iOS Safari | ✔️ 14.4+ |
| Edge | ✅ 88+ |
| IE | ❌ 无(缺少WASM、Async Functions、TextEncoder、BigInt) |
| Android Browser | ✔️ 81+ |
| UC Browser for Android | ❌ 无(缺少WASM、BigInt) |
| Samsung Internet | ✅ 9.2+ |
| QQ Browser | ❌ 无(缺少BigInt) |
| Baidu Browser | ❌ 无(缺少WASM、异步函数、BigInt) |
| KaiOS浏览器 | ❌ 无(缺少WASM、异步函数、BigInt) |
| ? | 图例 |
|---|---|
| ✅ | 已测试 |
| ✔️ | 应该可以工作,但未测试,因此可能已损坏 - 文件问题! |
| ❌ | 已损坏(浏览器可能可以通过polyfills和不同的代码生成器进行修复?) |
可移植性:命令行
需要
- rustup
- cargo(通常通过rustup安装)
- 预构建的wasm-bindgen二进制文件(将自动下载适当的版本。)
- 预构建的wasm-opt二进制文件(将自动下载。)
- wasm-pack以支持
wasm_bindgen(将从源自动安装。) - cargo-web以支持
stdweb(将从源自动安装。) - 通用酷炫功能。
| 构建操作系统 | x86_64 | x86 | AArch64 | ARM | 其他 |
|---|---|---|---|---|---|
| Windows | ✅ | ❌BO | ❌BO | ❌BO | ❌BO [...] |
| Linux | ✅ | ❌BO | ❌BO | ❌BO | ❌BO [...] |
| OS X | ✔️ | ❌BO | ❌BO | ❌BO | ❌BO [...] |
| ? | 图例 |
|---|---|
| ✅ | 已测试 |
| ✔️ | 应该可以工作 |
| ❌B | 已损坏(wasm-bindgen二进制文件不可用,无法为WASM绑定JS) |
| ❌O | 已损坏(wasm-opt二进制文件不可用,无法异步化WASM) |
许可证
根据您的选择,许可如下
- Apache许可证第2版(《LICENSE-APACHE》或https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT许可证(《LICENSE-MIT》或https://open-source.org.cn/licenses/MIT)
。
贡献
除非您明确声明,否则根据Apache-2.0许可证定义的,任何有意提交以包含在作品中的贡献,都应如上双许可,不附加任何额外条款或条件。
依赖关系
~14–25MB
~401K SLoC