diff --git a/ci/build_examples.sh b/ci/build_examples.sh deleted file mode 100755 index 9a49eef1c19..00000000000 --- a/ci/build_examples.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/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 diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 00000000000..3dd4131ef4b --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1,5 @@ +*/static/package.json +*/static/wasm_bg.wasm +*/static/wasm_bg.d.ts +*/static/wasm.d.ts +*/static/wasm.js diff --git a/examples/README.md b/examples/README.md index 997b94c9201..c794b30ba5c 100644 --- a/examples/README.md +++ b/examples/README.md @@ -16,8 +16,7 @@ Have a look at Yew's [starter templates](https://yew.rs/docs/getting-started/sta ```sh git clone https://github.com/yewstack/yew.git cd yew/examples -./build.sh minimal # example subfolder -cd static && python3 -m http.server # open localhost:8000 in browser +./run_example.sh minimal # builds and opens the "minimal" example in browser ``` diff --git a/examples/js_callback/static/index.html b/examples/js_callback/static/index.html index 8e47c484fcc..71c70493589 100644 --- a/examples/js_callback/static/index.html +++ b/examples/js_callback/static/index.html @@ -2,10 +2,13 @@ - (Asynchronous) callback from JavaScript + Yew • (Asynchronous) callback from JavaScript - + diff --git a/examples/keyed_list/static/index.html b/examples/keyed_list/static/index.html index 43316a8e3e0..bc46533d2c9 100644 --- a/examples/keyed_list/static/index.html +++ b/examples/keyed_list/static/index.html @@ -2,10 +2,13 @@ - Keyed list example + Yew • Keyed list - + diff --git a/examples/large_table/static/index.html b/examples/large_table/static/index.html index f8b3189590a..20960ab02f4 100644 --- a/examples/large_table/static/index.html +++ b/examples/large_table/static/index.html @@ -2,10 +2,13 @@ - Table100x100 Test + Yew • Table 100x100 - + diff --git a/examples/multi_thread/static/index.html b/examples/multi_thread/static/index.html index 86775e39e04..5c452a8fab4 100644 --- a/examples/multi_thread/static/index.html +++ b/examples/multi_thread/static/index.html @@ -3,7 +3,11 @@ Yew • Multi-Thread - - + + + diff --git a/examples/nested_list/static/index.html b/examples/nested_list/static/index.html index 0d34f497c81..68db027c7b4 100644 --- a/examples/nested_list/static/index.html +++ b/examples/nested_list/static/index.html @@ -6,6 +6,9 @@ - + diff --git a/examples/npm_and_rest/static/index.html b/examples/npm_and_rest/static/index.html index 91c8c3688d6..adcec3b02db 100644 --- a/examples/npm_and_rest/static/index.html +++ b/examples/npm_and_rest/static/index.html @@ -6,6 +6,9 @@ - + diff --git a/examples/build.sh b/examples/run_example.sh similarity index 50% rename from examples/build.sh rename to examples/run_example.sh index 27d1c202f43..345c71573d9 100755 --- a/examples/build.sh +++ b/examples/run_example.sh @@ -2,15 +2,63 @@ # The example to build. EXAMPLE=${1%\/} +shift + # Optimization level. Can be either "--debug" or "--release". Defaults to debug. -PROFILE=${2:---debug} +PROFILE="--debug" + +# Whether to open a browser window after building +START_BROWSER=1 + +while (("$#")); do + case "$1" in + --release) + PROFILE="--release" + shift + ;; + --debug) + PROFILE="--debug" + shift + ;; + --build-only) + START_BROWSER=0 + shift + ;; + -*) # unsupported flags + echo "Error: Unsupported flag $1" >&2 + exit 1 + ;; + esac +done # 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 $EXAMPLE 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 + +info() { + # blue: [ info message ] + echo -e "[\033[0;34m $* \033[0m]" +} +fail() { + FAIL="true" + # red: [FAIL] + echo -e "[\033[0;31mFAIL\033[0m] $*" +} + +on_exit() { + 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 + elif [[ $START_BROWSER != 1 ]]; then + echo -e "[\033[0;32m Finished! Run $EXAMPLE by serving the generated files in examples/static/ \033[0m]" + fi +} + +trap on_exit EXIT + +SRCDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" # this source dir cd "$SRCDIR/$EXAMPLE" # "$SRCDIR" ensures that this script can be run from anywhere. @@ -38,23 +86,30 @@ cargo_build() { # 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 "$PROFILE" --target web --out-name wasm --out-dir "$SRCDIR/static/" - rm "$SRCDIR/static/.gitignore"; mv "$SRCDIR/static/.gitignore.copy" "$SRCDIR/static/.gitignore" # restore .gitignore + wasm-pack build "$PROFILE" --target web --out-name wasm --out-dir "$SRCDIR/$EXAMPLE/static/" # multi_thread build -> two binary/wasm files elif [[ $EXAMPLE == multi_thread ]]; then info "Building: $EXAMPLE app using wasm-bindgen" cargo_build --bin multi_thread_app - wasm-bindgen --target web --no-typescript --out-dir "$SRCDIR/static/" --out-name wasm "$TARGET_DIR/multi_thread_app.wasm" + wasm-bindgen --target web --no-typescript --out-dir "$SRCDIR/$EXAMPLE/static/" --out-name wasm "$TARGET_DIR/multi_thread_app.wasm" info "Building: $EXAMPLE worker using wasm-bindgen" cargo_build --bin multi_thread_worker - wasm-bindgen --target no-modules --no-typescript --out-dir "$SRCDIR/static/" --out-name worker "$TARGET_DIR/multi_thread_worker.wasm" + wasm-bindgen --target no-modules --no-typescript --out-dir "$SRCDIR/$EXAMPLE/static/" --out-name worker "$TARGET_DIR/multi_thread_worker.wasm" else # Default wasm-bindgen build info "Building: $EXAMPLE using wasm-bindgen" cargo_build - wasm-bindgen --target web --no-typescript --out-dir "$SRCDIR/static/" --out-name wasm "$TARGET_DIR/$EXAMPLE.wasm" + wasm-bindgen --target web --no-typescript --out-dir "$SRCDIR/$EXAMPLE/static/" --out-name wasm "$TARGET_DIR/$EXAMPLE.wasm" +fi + +cd static +if [[ $START_BROWSER == 1 ]]; then + if ! [ -x "$(command -v python3)" ]; then + echo "WARNING: python3 not found! Please manually start a web server for the $SRCDIR/$EXAMPLE/static directory." + echo " Use '--build-only' to suppress this message." + exit 1 + fi + python3 ../../start_example_server.py $FLAGS fi diff --git a/examples/start_example_server.py b/examples/start_example_server.py new file mode 100644 index 00000000000..6bfa8349929 --- /dev/null +++ b/examples/start_example_server.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 +from http.server import BaseHTTPRequestHandler, HTTPServer + +import os +import sys +import logging +import webbrowser + +PORT = 8080 +STATIC_RESOURCES = { "/wasm.js", "/wasm_bg.wasm", "/favicon.ico" } +DEFAULT_INDEX_HTML = b""" + + + + + Yew example + + + + +""" + +class S(BaseHTTPRequestHandler): + def _set_response(self, code, mime, charset="utf-8"): + self.send_response(code) + self.send_header("Content-type", f"{mime}; charset={charset}") + self.send_header("Cache-Control", "private, max-age=0, must-revalidate") + self.end_headers() + + def do_GET(self): + path = self.path + if path in ("", "/"): + path = "/index.html" + relative_path = f".{path}" + + if os.path.isfile(relative_path): + logging.info("is a file: %s", relative_path) + with open(relative_path, "rb") as f: + mime = "text/plain" + if relative_path.endswith(".html"): + mime = "text/html" + elif relative_path.endswith(".wasm"): + mime = "application/wasm" + elif relative_path.endswith(".js"): + mime = "text/javascript" + elif relative_path.endswith(".json"): + mime = "application/json" + elif relative_path.endswith(".css"): + mime = "text/css" + elif relative_path.endswith(".toml"): + mime = "application/toml" + self._set_response(200, mime) + self.wfile.write(f.read()) + elif path == "/index.html": + self._set_response(200, "text/html") + self.wfile.write(DEFAULT_INDEX_HTML) + else: + self._set_response(404, mime="text/plain") + self.wfile.write(b"404 file not found") + +def run(server_class=HTTPServer, handler_class=S): + logging.basicConfig(level=logging.INFO) + server_address = ("", PORT) + httpd = server_class(server_address, handler_class) + webbrowser.open(f"http://localhost:{PORT}") + logging.info("Starting web server...\n") + try: + httpd.serve_forever() + except KeyboardInterrupt: + pass + httpd.server_close() + logging.info("Stopping web server...\n") + +if __name__ == "__main__": + from sys import argv + + run() diff --git a/examples/static/.gitignore b/examples/static/.gitignore deleted file mode 100644 index 4625e257bf3..00000000000 --- a/examples/static/.gitignore +++ /dev/null @@ -1,6 +0,0 @@ -*.wasm -*.js -*.json -*.ts -*.md -snippets/ diff --git a/examples/static/index.html b/examples/static/index.html deleted file mode 100644 index e1925eb8819..00000000000 --- a/examples/static/index.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - - Yew example - - - - diff --git a/examples/todomvc/static/index.html b/examples/todomvc/static/index.html index ace3fb6e986..0c23f1fa28b 100644 --- a/examples/todomvc/static/index.html +++ b/examples/todomvc/static/index.html @@ -1,7 +1,7 @@ - + Yew • TodoMVC diff --git a/examples/two_apps/static/index.html b/examples/two_apps/static/index.html index 10b17a87142..c9454dcc222 100644 --- a/examples/two_apps/static/index.html +++ b/examples/two_apps/static/index.html @@ -3,11 +3,13 @@ Yew • Two Apps -
- +