该货物子命令旨在使构建,开发和部署用Rust编写的客户端Web应用程序变得容易且方便。
由于这些出色的人,该软件被带给您:
谢谢你!
目前,它支持以下功能:
cargo web build - 将使用Rust的三个网络后端之一构建您的项目:--target=wasm32-unknown-unknown ;默认值)--target=wasm32-unknown-emscripten )--target=asmjs-unknown-emscripten )cargo web check - 将打字您的项目cargo web test - 将运行您的测试以下:--nodejs时)cargo web start - 将构建您的项目,启动嵌入式网络服务器,并在必要时不断重建;支持--auto-reload自动重新加载。cargo web deploy - 将构建您的项目并发出所有必要的文件,以便您可以轻松地静态地提供它们。rustup自动安装相关的锈目标还强烈建议您查看stdweb板条箱,如果您想与项目中的JavaScript世界进行互动。 (实际上, cargo-web是可以在Rust的本机WebAssembly后端使用stdweb的js!宏的原因。)
$ cargo install cargo-web
升级:
$ cargo install --force cargo-web
或克隆并用$ cargo build --release然后放在您的$路径中。
在Linux上,安装可能会失败,因为它找不到OpenSSL,在这种情况下,您很可能需要从发行版的存储库中安装openSSL的-dev软件包。 (在Ubuntu上,这就是称为libssl-dev 。)
Web.toml cargo-web有自己的配置文件,您可以将其放在cargo的Cargo.toml旁边。
这是一个示例配置,显示了每个受支持的密钥:
# The default value of `--target` used when building this crate
# in cases where it's not specified on the command line.
default-target = " wasm32-unknown-unknown "
# This will prepend a given JavaScript file to the resulting `.js` artifact.
# You can put any initialization code here which you'd like to have executed
# when your `.js` file first loads.
#
# This accepts either a string (as shown here), or an array of strings,
# in which case it will prepend all of the specified files in their
# order of appearance.
prepend-js = " src/runtime.js "
[ cargo-web ]
# Asserts the minimum required version of `cargo-web` necessary
# to compile this crate; supported since 0.6.0.
minimum-version = " 0.6.0 "
# These will only take effect on *-emscripten targets.
[ target . emscripten ]
# You can have a target-specific `prepend-js` key.
prepend-js = " src/emscripten_runtime.js "
# This will enable Emscripten's SDL2 port. Consult Emscripten's documentation
# for more details.
link-args = [ " -s " , " USE_SDL=2 " ]
# You can also specify the target by its full name.
[ target . wasm32-unknown-unknown ]
prepend-js = " src/native_runtime.js "如果您使用任何具有Web.toml的外部板条箱,则cargo-web将加载并使用它。
有关Web.toml的一些限制:
prepend-js键。您可以定义单个全局prepend-js或多个每个目标。link-args当前没有任何空间。cargo-web将处理Web.toml文件是确定性但未指定的。这意味着您不应以任何方式依赖此顺序。 运行cargo web start或运行cargo web deploy时部署时要使用的任何静态文件都可以放入板条箱根的static中。默认情况下不需要静态工件;如果丢失了index.html文件,将为您自动生成。当然,您可以将自己的static/index.html文件放置,在这种情况下将使用它代替自动化的文件。
cargo-web如果在编译期间,您想检测到您的项目是使用cargo-web构建的,则可以检查COMPILING_UNDER_CARGO_WEB环境变量,该变量将设置为1 。
cargo-web您可以使用以下脚本下载并安装最新的cargo-web :
CARGO_WEB_RELEASE= $( curl -L -s -H ' Accept: application/json ' https://github.com/koute/cargo-web/releases/latest )
CARGO_WEB_VERSION= $( echo $CARGO_WEB_RELEASE | sed -e ' s/.*"tag_name":"([^"]*)".*/1/ ' )
if [ " $( uname -s ) " == " Darwin " ] ; then
CARGO_WEB_HOST_TRIPLE= " x86_64-apple-darwin "
else
CARGO_WEB_HOST_TRIPLE= " x86_64-unknown-linux-gnu "
fi
CARGO_WEB_URL= " https://github.com/koute/cargo-web/releases/download/ $CARGO_WEB_VERSION /cargo-web- $CARGO_WEB_HOST_TRIPLE .gz "
echo " Downloading cargo-web from: $CARGO_WEB_URL "
curl -L $CARGO_WEB_URL | gzip -d > cargo-web
chmod +x cargo-web
mkdir -p ~ /.cargo/bin
mv cargo-web ~ /.cargo/bin默认情况下, cargo web test将在无头铬的下进行测试。为了能够在Travis上使用它,您需要将类似的内容添加到您的.travis.yml :
addons :
chrome : stable wasm32-unknown-unknown仅)默认情况下构建项目时, cargo-web为您生成独立的运行时运行时。这意味着可以立即将生成的.js文件放入<script>标签的内部,也可以使用node.js启动,而无需手动加载它或做任何额外的操作,但是这确实限制了您的自定义性。
如果您想对模块的加载方式有更多的控制权,则可以告诉cargo-web使用--runtime library-es6选项为您生成一个非标准的,类似图书馆的模块。这将导致一个.js文件,该文件以以下接口导出出厂功能:
export default function ( ) {
return {
imports : { ... } ,
initialize : function ( instance ) { ... }
} ;
}在这里,您必须自己实例化WebAssembly模块;在这种情况下,您必须将imports作为导入,然后在实例化之后立即将其调用initialize 。
例如,假设您将cargo-web生成的模块命名为my-module.mjs和my-module.wasm 。
import fs from "fs" ;
import factory from "my-module.mjs" ;
// We read in the `.wasm` module.
const bytecode = fs . readFileSync ( "my-module.wasm" ) ;
const wasm = new WebAssembly . Module ( bytecode ) ;
// We instantiate it.
const instance = factory ( ) ;
const compiled = new WebAssembly . Instance ( wasm , instance . imports ) ;
// This will initialize the module and call your `main`, if you have one.
const exports = instance . initialize ( compiled ) ;
// In the object it returns you can find any functions which
// you've exported with `stdweb`'s `#[js_export]` macro.
console . log ( exports . add ( 1 , 2 ) ) ;然后,您可以使用node --experimental-modules run.mjs运行它。
如果您想从自定义URL加载.wasm文件,或者想将输出与JavaScript Bundler或其他任何需要您自己加载模块加载的东西。
0.6.26--no-default-features标志mime-guess板条箱0.6.25cargo web start现在将尝试在快速连续修改项目文件时不要触发多余的重建0.6.24[target.'cfg(...)'.dependencies]现在得到适当支持cfg(cargo_web)来检测何时在cargo-web下汇编板条箱target/wasm32-unknown-unknown/*/deps/*.wasm这应该防止cargo-web处理cdylib的.wasmstructopt的界面作为库作为cargo-web0.6.23cargo web checkwasm32-unknown-unknown目标现在是默认值0.6.22-o / --output参数deploy子命令Access-Control-Allow-Origin: *现在始终由嵌入式Web服务器发送stdweb ,则支持基于wasm32-unknown-unknown insport0.6.211.38.19 ;基于Emscripten的目标现在应该在每晚再次工作wasm32-unknown-unknown进行了生成的JS摘要和进口wasm32-unknown-unknown兼容性与真正的旧夜间的兼容性0.6.20cargo install安装现在应该再次工作deploy不应恐慌0.6.19cargo install现在应该编译,而不是在某些环境中失败1.26.20.6.18index.html在其Doctype之前没有新线0.6.171.25.00.6.16wasm32-unknown-unknown的运行时间现在使用WebAssembly.instantiateStreaming (如果可用)wasm32-unknown-unknown目标,在无头铬下进行测试cargo-web的输出时,不再发出颜色代码根据任何一个
可以选择。
除非您另有明确说明,否则任何有意提交的捐款(如Apache-2.0许可证中定义)应为双重许可,如上所述,没有任何其他条款或条件。