Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated examples, added build script #1165

Merged
merged 21 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ members = [
"examples/dashboard",
"examples/file_upload",
"examples/fragments",
"examples/futures",
"examples/futures_wp",
"examples/game_of_life",
"examples/inner_html",
"examples/js_callback",
"examples/large_table",
"examples/minimal",
"examples/minimal_wp",
"examples/mount_point",
"examples/multi_thread",
"examples/nested_list",
Expand Down
23 changes: 23 additions & 0 deletions ci/build_examples.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

# Script to build all examples in yew/examples
#
# Not building yew-router nor yewtil examples

# src: https://gist.github.com/fbucek/f986da3cc3a9bbbd1573bdcb23fed2e1
set -e # error -> trap -> exit
function info() { echo -e "[\033[0;34m $@ \033[0m]"; } # blue: [ info message ]
function fail() { FAIL="true"; echo -e "[\033[0;31mFAIL\033[0m] $@"; } # red: [FAIL]
trap 'LASTRES=$?; LAST=$BASH_COMMAND; if [[ LASTRES -ne 0 ]]; then fail "Command: \"$LAST\" exited with exit code: $LASTRES"; elif [ "$FAIL" == "true" ]; then fail finished with error; else echo -e "[\033[0;32m Finished $@ \033[0m]";fi' EXIT
SRCDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" # this source dir

cd $SRCDIR/../examples # switch to examples folder

for EXAMPLE in *
do
if [[ $EXAMPLE == static ]] || [[ $EXAMPLE == server ]] || [[ $EXAMPLE == target ]]; then
echo -e "Skipping folder: $EXAMPLE"
elif [ -d ${EXAMPLE} ]; then
./build.sh ${EXAMPLE} $@
fi
done
44 changes: 44 additions & 0 deletions examples/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash

# src: https://gist.github.com/fbucek/f986da3cc3a9bbbd1573bdcb23fed2e1
set -e # error -> trap -> exit
function info() { echo -e "[\033[0;34m $@ \033[0m]"; } # blue: [ info message ]
function fail() { FAIL="true"; echo -e "[\033[0;31mFAIL\033[0m] $@"; } # red: [FAIL]
trap 'LASTRES=$?; LAST=$BASH_COMMAND; if [[ LASTRES -ne 0 ]]; then fail "Command: \"$LAST\" exited with exit code: $LASTRES"; elif [ "$FAIL" == "true" ]; then fail finished with error; else echo -e "[\033[0;32m Finished! Run $@ by serving the generated files in examples/static/ \033[0m]";fi' EXIT
SRCDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" # this source dir

cd $SRCDIR # ensure this script can be run from anywhere

# When using $CARGO_TARGET_DIR -> binary is located in different folder
# Necessary to locate build files for wasm-bindgen
TARGET_DIR=$SRCDIR/../target/wasm32-unknown-unknown/debug
if [ ! -z "$CARGO_TARGET_DIR" ]; then
TARGET_DIR=$CARGO_TARGET_DIR/wasm32-unknown-unknown/debug
fi

EXAMPLE=$1
cd $EXAMPLE

# wasm-pack build
if [[ $EXAMPLE == *_wp ]]; then
info "Building: $EXAMPLE using wasm-pack"
# wasm-pack overwrites .gitignore -> save -> restore
cp $SRCDIR/static/.gitignore $SRCDIR/static/.gitignore.copy
wasm-pack build --debug --target web --out-name wasm --out-dir $SRCDIR/static/
rm $SRCDIR/static/.gitignore; mv $SRCDIR/static/.gitignore.copy $SRCDIR/static/.gitignore # restore .gitignore

# multi_thrread build -> two binary/wasm files
elif [[ $EXAMPLE == multi_thread ]]; then
info "Building: $EXAMPLE app using wasm-bindgen"
cargo build --target wasm32-unknown-unknown --bin multi_thread_app
wasm-bindgen --target web --no-typescript --out-dir $SRCDIR/static/ --out-name wasm $TARGET_DIR/multi_thread_app.wasm

info "Building: $EXAMPLE worker using wasm-bindgen"
cargo build --target wasm32-unknown-unknown --bin multi_thread_worker
wasm-bindgen --target no-modules --no-typescript --out-dir $SRCDIR/static/ --out-name worker $TARGET_DIR/multi_thread_worker.wasm

else # Default wasm-bindgen build
info "Building: $EXAMPLE using wasm-bindgen"
cargo build --target wasm32-unknown-unknown
wasm-bindgen --target web --no-typescript --out-dir $SRCDIR/static/ --out-name wasm $TARGET_DIR/$EXAMPLE.wasm
fi
3 changes: 0 additions & 3 deletions examples/counter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ version = "0.1.1"
authors = ["Denis Kolodin <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
js-sys = "0.3"
yew = { path = "../../yew" }
7 changes: 0 additions & 7 deletions examples/crm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,3 @@ serde = "1"
serde_derive = "1"
yew = { path = "../../yew" }
pulldown-cmark = { version = "0.7.0", default-features = false }

[features]
std_web = ["yew/std_web"]
web_sys = ["yew/web_sys"]

[lib]
crate-type = ["cdylib", "rlib"]
7 changes: 0 additions & 7 deletions examples/custom_components/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,3 @@ edition = "2018"

[dependencies]
yew = { path = "../../yew" }

[features]
std_web = ["yew/std_web"]
web_sys = ["yew/web_sys"]

[lib]
crate-type = ["cdylib", "rlib"]
7 changes: 0 additions & 7 deletions examples/dashboard/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,3 @@ anyhow = "1"
serde = "1"
serde_derive = "1"
yew = { path = "../../yew", features = ["toml"] }

[features]
std_web = ["yew/std_web"]
web_sys = ["yew/web_sys"]

[lib]
crate-type = ["cdylib", "rlib"]
3 changes: 0 additions & 3 deletions examples/file_upload/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ version = "0.1.0"
authors = ["Denis Kolodin <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
js-sys = "0.3"
yew = { path = "../../yew" }
7 changes: 0 additions & 7 deletions examples/fragments/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,3 @@ edition = "2018"

[dependencies]
yew = { path = "../../yew" }

[features]
std_web = ["yew/std_web"]
web_sys = ["yew/web_sys"]

[lib]
crate-type = ["cdylib", "rlib"]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "futures"
name = "futures_wp"
version = "0.1.0"
authors = ["Henry Zimmerman <[email protected]>"]
edition = "2018"
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 0 additions & 7 deletions examples/game_of_life/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,3 @@ rand = "0.7.3"
log = "0.4"
web_logger = "0.2"
yew = { path = "../../yew" }

[features]
std_web = ["rand/stdweb", "yew/std_web"]
web_sys = ["rand/wasm-bindgen", "yew/web_sys"]

[lib]
crate-type = ["cdylib", "rlib"]
3 changes: 0 additions & 3 deletions examples/inner_html/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ version = "0.1.0"
authors = ["Garrett Berg <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
yew = { path = "../../yew" }

Expand Down
3 changes: 0 additions & 3 deletions examples/js_callback/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ version = "0.1.0"
authors = ["Scott Steele <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
wasm-bindgen = "0.2.60"
yew = { path = "../../yew" }
7 changes: 0 additions & 7 deletions examples/large_table/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,3 @@ edition = "2018"

[dependencies]
yew = { path = "../../yew" }

[features]
std_web = ["yew/std_web"]
web_sys = ["yew/web_sys"]

[lib]
crate-type = ["cdylib", "rlib"]
6 changes: 0 additions & 6 deletions examples/minimal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,3 @@ edition = "2018"
[dependencies]
yew = { path = "../../yew" }

[features]
std_web = ["yew/std_web"]
web_sys = ["yew/web_sys"]

[lib]
crate-type = ["cdylib", "rlib"]
2 changes: 1 addition & 1 deletion examples/minimal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Component for Model {
fn view(&self) -> Html {
html! {
<div>
<button onclick=self.link.callback(|_| Msg::Click)>{ "Click" }</button>
<button onclick=self.link.callback(|_| Msg::Click)>{ "Click ( wasm-bindgen )" }</button>
</div>
}
}
Expand Down
15 changes: 15 additions & 0 deletions examples/minimal_wp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "minimal_wp"
version = "0.1.0"
authors = ["Denis Kolodin <[email protected]>"]
edition = "2018"

[dependencies]
yew = { path = "../../yew" }
wasm-bindgen = "0.2"


# requirement of wasm-pack which builds crates as libraries
# it is required to have `#[wasm_bindgen(start)]` in lib.rs as starting point
[lib]
fbucek marked this conversation as resolved.
Show resolved Hide resolved
crate-type = ["cdylib", "rlib"]
43 changes: 43 additions & 0 deletions examples/minimal_wp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use wasm_bindgen::prelude::*;
use yew::{html, Component, ComponentLink, Html, ShouldRender};

pub struct Model {
link: ComponentLink<Self>,
}

pub enum Msg {
Click,
}

impl Component for Model {
type Message = Msg;
type Properties = ();

fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
Model { link }
}

fn change(&mut self, _: Self::Properties) -> bool {
false
}

fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::Click => {}
}
true
}

fn view(&self) -> Html {
html! {
<div>
<button onclick=self.link.callback(|_| Msg::Click)>{ "Click ( wasm-pack )" }</button>
</div>
}
}
}

#[wasm_bindgen(start)]
pub fn run_app() {
yew::start_app::<Model>();
}
3 changes: 0 additions & 3 deletions examples/mount_point/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ version = "0.1.0"
authors = ["Ben Berman <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
wasm-bindgen = "0.2"
yew = { path = "../../yew" }
Expand Down
3 changes: 0 additions & 3 deletions examples/multi_thread/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ version = "0.1.0"
authors = ["Denis Kolodin <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[[bin]]
name = "multi_thread_app"
path = "src/bin/app.rs"
Expand Down
7 changes: 0 additions & 7 deletions examples/nested_list/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,3 @@ edition = "2018"
log = "0.4"
web_logger = "0.2"
yew = { path = "../../yew" }

[features]
std_web = ["yew/std_web"]
web_sys = ["yew/web_sys"]

[lib]
crate-type = ["cdylib", "rlib"]
3 changes: 0 additions & 3 deletions examples/node_refs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ version = "0.1.0"
authors = ["Justin Starry <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
yew = { path = "../../yew" }
web-sys = { version = "0.3", features = ["HtmlElement", "HtmlInputElement", "Node"] }
3 changes: 0 additions & 3 deletions examples/npm_and_rest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ version = "0.1.0"
authors = ["Denis Kolodin <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
anyhow = "1"
js-sys = "0.3"
Expand Down
7 changes: 0 additions & 7 deletions examples/pub_sub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,3 @@ log = "0.4"
web_logger = "0.2"
serde = { version = "1.0", features = ["derive"] }
yew = { path = "../../yew" }

[features]
std_web = ["yew/std_web"]
web_sys = ["yew/web_sys"]

[lib]
crate-type = ["cdylib", "rlib"]
39 changes: 39 additions & 0 deletions examples/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Yew Examples

Use `build.sh` script to build examples.

Examples are prepared for `wasm-bindgen` except folders ending with `_wp` which are prepared for `wasm-pack`.

- The examples do not use external bundlers and all use the same `static/index.html` after being built.
- `wasm-bindgen` builds projects as binary crates (`main.rs`)
- `wasm-pack` builds projects as library crates (`lib.rs`)

For your own project, you may wish to check out the Yew [starter templates](https://yew.rs/docs/getting-started/starter-templates)


## How to run

```sh
git clone https://github.com/yewstack/yew.git
cd yew/examples
./build.sh minimal # example subfolder
python3 -m http.server --directory static # open localhost:8000 in browser
```


Note: [Visual Studio Code IDE](https://code.visualstudio.com/) has the [Live Server](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer) extension which can be used to run examples in the browser with automatic reload. After installing, open the context menu on `index.html` -> `Open with Live Server`

## Requirements

Default way to build is using `wasm-bindgen` which comes with `wasm-pack`

- Install using cargo: `cargo install wasm-pack`

Install guides: [Rust](https://www.rust-lang.org/learn/get-started) and [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/)

```bash
# rust install
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# wasm-pack install
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh;
```
3 changes: 3 additions & 0 deletions examples/static/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
*.wasm
*.js
*.json
*.ts
*.md
snippets/
7 changes: 0 additions & 7 deletions examples/textarea/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,3 @@ edition = "2018"

[dependencies]
yew = { path = "../../yew" }

[features]
std_web = ["yew/std_web"]
web_sys = ["yew/web_sys"]

[lib]
crate-type = ["cdylib", "rlib"]
Loading