Skip to content

Commit a839251

Browse files
authored
Update the sample doc app to use wasm-pack (yewstack#75)
* Update the sample doc app to use wasm-pack According to the rest of the documentation, wasm-pack and web-sys are the recommended tool to use. However, the sample app documents the usage of cargo-web and stdweb. The command lines are taken from the example folder of yew. * Some grammar fixes * Use cargo miniserve instead of python to server the files * Add wasm-bindgen dependency
1 parent 00712bf commit a839251

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2018"
66

77
[dependencies]
88
doc-comment = "0.3.3"
9+
wasm-bindgen = "0.2"
910
wasm-bindgen-test = "0.3.0"
1011
wee_alloc = "0.4"
1112
yew = { version = "0.15.0", features = ["wasm_test"] }

‎src/getting-started/build-a-sample-app.md‎

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
First create a new binary project:
44

55
```bash
6-
cargo new --bin yew-app && cd yew-app
6+
cargo new yew-app && cd yew-app
77
```
88

9-
Add `yew` to your dependencies \(refer [here](https://docs.rs/yew) for the latest version\)
9+
Add `yew` and `wasm-bindgen` to your dependencies \(refer [here](https://docs.rs/yew) for the latest version\)
1010

1111
{% code title="Cargo.toml" %}
1212
```text
@@ -16,15 +16,20 @@ version = "0.1.0"
1616
authors = ["Yew App Developer <name@example.com>"]
1717
edition = "2018"
1818
19+
[lib]
20+
crate-type = ["cdylib", "rlib"]
21+
1922
[dependencies]
20-
yew = { version = "0.15", package = "yew-stdweb" }
23+
yew = "0.15"
24+
wasm-bindgen = "0.2"
2125
```
2226
{% endcode %}
2327

24-
Copy the following template into your `src/main.rs` file:
28+
Copy the following template into your `src/lib.rs` file:
2529

26-
{% code title="src/main.rs" %}
30+
{% code title="src/lib.rs" %}
2731
```rust
32+
use wasm_bindgen::prelude::*;
2833
use yew::prelude::*;
2934

3035
struct Model {
@@ -70,22 +75,53 @@ impl Component for Model {
7075
}
7176
}
7277

73-
fn main() {
74-
yew::initialize();
78+
#[wasm_bindgen(start)]
79+
pub fn run_app() {
7580
App::<Model>::new().mount_to_body();
7681
}
7782
```
7883
{% endcode %}
7984

8085
This template sets up your root `Component`, called `Model` which shows a button that updates itself when you click it. Take special note of `App::<Model>::new().mount_to_body()` inside `main()` which starts your app and mounts it to the page's `<body>` tag. If you would like to start your application with any dynamic properties, you can instead use `App::<Model>::new().mount_to_body_with_props(..)`.
8186

87+
Finally, add an `index.html` file into a new folder named `static` in your app.
88+
89+
```bash
90+
mkdir static
91+
```
92+
93+
{% code title="index.html" %}
94+
```html
95+
<!doctype html>
96+
<html lang="en">
97+
<head>
98+
<meta charset="utf-8">
99+
<title>Yew Sample App</title>
100+
<script type="module">
101+
import init from "./wasm.js"
102+
init()
103+
</script>
104+
</head>
105+
<body></body>
106+
</html>
107+
```
108+
{% endcode %}
109+
82110
## Run your App!
83111

84-
Using [`cargo-web`](https://github.com/koute/cargo-web) is the quickest way to get up and running. If you haven't already, install the tool with `cargo install cargo-web` and then build and start a development server by running:
112+
Using [`wasm-pack`](https://rustwasm.github.io/docs/wasm-pack/) is the preferred way to get up and running.
113+
If you haven't already, install `wasm-pack` with `cargo install wasm-pack` and then build and start a development server by running:
85114

86115
```bash
87-
cargo web start
116+
wasm-pack build --target web --out-name wasm --out-dir ./static
88117
```
89118

90-
`cargo-web` will automatically add the `wasm32-unknown-unknown` target for you, build your app, and finally make it available at [http://\[::1\]:8000](http://[::1]:8000) by default. Consult `cargo web start --help` for other options.
119+
`wasm-pack` generates a bundle in the `./static` directory with your app's compiled WebAssembly along with a JavaScript wrapper which will load your application's WebAssembly binary and run it.
120+
121+
Then, use your favorite web server to server the files under `./static`. For example:
122+
123+
```bash
124+
cargo +nightly install miniserver
125+
miniserve ./static --index index.html
126+
```
91127

0 commit comments

Comments
 (0)