該貨物子命令旨在使構建,開發和部署用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許可證中定義)應為雙重許可,如上所述,沒有任何其他條款或條件。