Crates.io | wasmdev_server |
lib.rs | wasmdev_server |
version | 0.1.7 |
source | src |
created_at | 2023-02-23 22:52:36.940827 |
updated_at | 2023-07-07 09:42:57.040174 |
description | wasmdev server implementation |
homepage | |
repository | https://github.com/Gronis/wasmdev |
max_upload_size | |
id | 793095 |
size | 39,299 |
Simple web development for web frontends written in rust:
// src/main.rs
#[wasmdev::main]
fn main() {
let window = web_sys::window().unwrap();
let document = window.document().unwrap();
let body = document.body().unwrap();
let val = document.create_element("p").unwrap();
val.set_text_content(Some("Hello World"));
body.append_child(&val).unwrap();
}
cargo run
Compiling my-web-app
Finished dev [unoptimized + debuginfo] target(s)
Running `target/debug/my-web-app`
Building wasm target
Compiling my-web-app
Finished dev [unoptimized + debuginfo] target(s)
┏━━━━━━━━━━━━━━━━━━━━━━━┓
Serving ┃ http://127.0.0.1:8080 ┃ <= Click to open your app!
┗━━━━━━━━━━━━━━━━━━━━━━━┛
wasmdev aims to provide the most simple way to develop your rust frontend web application. The idea is to use cargo
just like you would do when developing a native/binary executable. No need to install tools like trunk
or wasm-pack
. Just add wasmdev
to your dependencies and add a macro in front of your main function, and you have yourself a web server fit for rapid development! You can also build all web assets with a simple cargo build --release
, and they will be minified and ready for distribution. How cool is that!
Note: Project is in early stage of development. Bugs or other problems might still be present, error messages might have a long way to go etc. Don't use for large or $$$ projects. Use more tested tools like trunk
instead.
Note: The server application that is used to run and test your web frontend app is NOT suitable for hosting your web-app in a production environment. It lacks many common http server features and is only intended to be a fast and simple way to test and develop your web frontend app.
Note: Since the web server and the web front-end is the same application, changing the front-end code will cause cargo to recompile the server
part of the application as well every time you run cargo build
. When a project grows, this might become a big bottleneck. When a project is large enough, cargo wasmdev
can be installed and used instead of wasmdev
macro. This command provides the same features, but will remove the extra compilation time of building the web server.
wasmdev has similar features as trunk
. Like:
It also has some features that trunk
don't have (I believe), like:
cargo build --release
and you have your dist-optimized assetsconsole_error_panic_hook
in your frontend app (can be disabled)sass
or less
. Might be implemented in the future as optional featuresThe following options can be set to wasmdev::main
macro:
// src/main.rs
#[wasmdev::main(port: 8080, path: "src")]
fn main() {
//...
}
index.html
overrideBy default, all files in src
folder is served by the web server. You can add your index.html
file here to override the default one. This is necessary to pull in additional assets like css styles.
<!doctype html>
<html>
<head><link rel="stylesheet" href="/index.css"></head>
<body></body>
</html>
Project file-tree:
├── Cargo.toml
└── src
├── index.css
├── index.html
└── main.rs
If you want to have a separate path to static assets, they can be specified in the wasmdev::main
macro as mention previously. This is recommended, since the web-server won't try to recompile your wasm-code when you change your static assets.
// src/main.rs
#[wasmdev::main(path: "www")]
fn main() {
//...
}
Project file-tree:
├── Cargo.toml
├── src
│ └── main.rs
└── www
└── index.html
console_error_panic_hook
Just add wasmdev
and ignore the default features:
cargo add wasmdev --no-default-features
When building your project with a release build, the web assets (all javascript files and wasm code) will be built and optimized for release.
cargo build --release
Compiling my-web-app
Finished release [optimized] target(s)
Finished release artifacts in: 'target/dist/my-web-app'
Finished release [optimized] target(s)
The release artifacts will be located at target/dist/{project_name}
└── target
└── dist
└── my-web-app
├── index.html
├── index.js
└── index.wasm
When building in release mode, cache invalidation of build artifacts might not always work. This can happen if:
Changing any rust file in src directory, or pre-existing static asset fixes this.
All examples can be built and executed by cargo like this:
cargo run -p <example>
# Run the simple project that outputs "Hello World"
cargo run -p simple
See examples
folder for a complete list of examples.
cargo test
with the test result in the cli window. Not sure if this is possible.The result is somewhat minimized but we can minimize it further (~10-15% improvement). So, go to target dir:
cd target/dist/<example>
This command will replace js/wasm glue code function names (in a hacky way) to minimized versions (requires wasm-opt):
../../../replace.sh index.js index.wasm
Install swc and minimize javascript one final time (requires node/npm)
npm install swc
npx swc index.js -C jsc.target=es2017 -C minify=true -C jsc.minify.compress=true -C jsc.minify.mangle=true -o index.js
Now, test your site with python http server (or similar):
python3 -m http.server 8080