This cargo subcommand aims to make it easy and convenient to build, develop and deploy client-side Web applications written in Rust.
This software was brought to you thanks to these wonderful people: * Embark Studios * Eduard Knyshov * Anselm Eickhoff * Ferran Pujol Camins * Isaac F Leonard * Johan Andersson * Martin KavĂk * Stephen Sugden
Thank you!
Currently it supports the following features:
cargo web build
- will build your project using one of Rust's three Web backends:
--target=asmjs-unknown-emscripten
; default)--target=wasm32-unknown-emscripten
)--target=wasm32-unknown-unknown
, requires Rust nightly)cargo web test
- will run your tests either under:
--nodejs
)cargo web start
- will build your project, start an embedded webserver and will continuously
rebuild it if necessary; supports automatic reloading with --auto-reload
.cargo web deploy
- will build your project and emit all of the necessary files so that
you can easily serve them statically.It's also highly recommended that you check out the [stdweb] crate if you want
to interact with the JavaScript world in your project. (In fact, cargo-web
is what makes it possible to use stdweb
's js!
macro on Rust's native WebAssembly
backend.)
$ cargo install cargo-web
To upgrade:
$ cargo install --force cargo-web
Or clone and build with $ cargo build --release
then place in your $PATH.
On Linux the installation can fail with a message that it can't find OpenSSL,
in which case you most likely need to install the -dev
package for OpenSSL
from your distribution's repositories. (On Ubuntu it's called libssl-dev
.)
Web.toml
cargo-web
has its own configuration file which you can put next to cargo
's [Cargo.toml
].
Here's an example configuration showing every supported key:
```toml
--target
used when building this cratedefault-target = "wasm32-unknown-unknown"
.js
artifact..js
file first loads.#
prepend-js = "src/runtime.js"
[cargo-web]
cargo-web
necessaryminimum-version = "0.6.0"
[target.emscripten]
prepend-js
key.prepend-js = "src/emscripten_runtime.js"
link-args = ["-s", "USE_SDL=2"]
[target.wasm32-unknown-unknown] prepend-js = "src/native_runtime.js" ```
If you use any external crates which have a Web.toml
then cargo-web
will load it and use it.
A few restrictions concerning the Web.toml
:
prepend-js
keys. You can either define
a single global prepend-js
, or multiple per-target ones.link-args
currently can't have any spaces in them.cargo-web
will process the Web.toml
files
from multiple crates is deterministic yet unspecified. This means
that you shouldn't depend on this order in any way.Any static files you'd like to have served when running cargo web start
or deployed
when running cargo web deploy
can be put in a directory called static
in the root
of your crate. No static artifacts are required by default; an index.html
file will
be automatically generated for you if it's missing. You can, of course, put your own static/index.html
file, in which case it will be used instead of the autogenerated one.
cargo-web
during compilationIf during compilation you'd like to detect that your project is being built with cargo-web
you can check the COMPILING_UNDER_CARGO_WEB
environment variable, which will be set to 1
.
cargo-web
on TravisYou can use the following script to download and install the latest cargo-web
:
```shell CARGOWEBRELEASE=$(curl -L -s -H 'Accept: application/json' https://github.com/koute/cargo-web/releases/latest) CARGOWEBVERSION=$(echo $CARGOWEBRELEASE | sed -e 's/."tag_name":"([^"])".*/1/') CARGOWEBURL="https://github.com/koute/cargo-web/releases/download/$CARGOWEBVERSION/cargo-web-x86_64-unknown-linux-gnu.gz"
echo "Downloading cargo-web from: $CARGOWEBURL" curl -L $CARGOWEBURL | gzip -d > cargo-web chmod +x cargo-web
mkdir -p ~/.cargo/bin mv cargo-web ~/.cargo/bin ```
By default cargo web test
will run your tests under headless Chrome. To be able to use this on Travis
you need to add something like this to your .travis.yml
:
yaml
addons:
chrome: stable
0.6.22
deploy
subcommand can now be told where to deploy using the -o
/--output
parameterAccess-Control-Allow-Origin: *
is now always sent by the embedded webserverwasm32-unknown-unknown
are now supported provided a recent enough stdweb
is used0.6.21
1.38.19
; the Emscripten-based targets should now work again on nightlywasm32-unknown-unknown
are now sortedwasm32-unknown-unknown
0.6.20
cargo install
should now work againdeploy
should not panic when it doesn't find a valid target0.6.19
cargo install
should now compile instead of failing in some environments1.26.2
0.6.18
index.html
doesn't have a newline before its doctype anymore0.6.17
1.25.0
0.6.16
wasm32-unknown-unknown
now uses WebAssembly.instantiateStreaming
when availablewasm32-unknown-unknown
targetcargo-web
is redirectedLicensed under either of
at your option.
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.