You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
yew = { version = "0.15", package = "yew-stdweb" }
23
+
yew = "0.15"
24
+
wasm-bindgen = "0.2"
21
25
```
22
26
{% endcode %}
23
27
24
-
Copy the following template into your `src/main.rs` file:
28
+
Copy the following template into your `src/lib.rs` file:
25
29
26
-
{% code title="src/main.rs" %}
30
+
{% code title="src/lib.rs" %}
27
31
```rust
32
+
usewasm_bindgen::prelude::*;
28
33
useyew::prelude::*;
29
34
30
35
structModel {
@@ -70,22 +75,53 @@ impl Component for Model {
70
75
}
71
76
}
72
77
73
-
fnmain() {
74
-
yew::initialize();
78
+
#[wasm_bindgen(start)]
79
+
pubfnrun_app() {
75
80
App::<Model>::new().mount_to_body();
76
81
}
77
82
```
78
83
{% endcode %}
79
84
80
85
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(..)`.
81
86
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
+
<htmllang="en">
97
+
<head>
98
+
<metacharset="utf-8">
99
+
<title>Yew Sample App</title>
100
+
<scripttype="module">
101
+
importinitfrom"./wasm.js"
102
+
init()
103
+
</script>
104
+
</head>
105
+
<body></body>
106
+
</html>
107
+
```
108
+
{% endcode %}
109
+
82
110
## Run your App!
83
111
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:
85
114
86
115
```bash
87
-
cargo web start
116
+
wasm-pack build --target web --out-name wasm --out-dir ./static
88
117
```
89
118
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:
0 commit comments