diff --git a/build_web.sh b/build_web.sh new file mode 100755 index 00000000..7062406a --- /dev/null +++ b/build_web.sh @@ -0,0 +1,41 @@ +#!/bin/bash +set -eu + +# ./setup_web.sh # <- call this first! + +CRATE_NAME="puffin_viewer" +CRATE_NAME_SNAKE_CASE="${CRATE_NAME//-/_}" # for those who name crates with-kebab-case + +# This is required to enable the web_sys clipboard API which egui_web uses +# https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Clipboard.html +# https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html +export RUSTFLAGS=--cfg=web_sys_unstable_apis + +# Clear output from old stuff: +rm -f docs/${CRATE_NAME_SNAKE_CASE}_bg.wasm + +echo "Building rust…" +BUILD=release +cargo build --release -p ${CRATE_NAME} --lib --target wasm32-unknown-unknown + +echo "Generating JS bindings for wasm…" +TARGET_NAME="${CRATE_NAME_SNAKE_CASE}.wasm" +wasm-bindgen "target/wasm32-unknown-unknown/${BUILD}/${TARGET_NAME}" \ + --out-dir docs --no-modules --no-typescript + +# to get wasm-opt: apt/brew/dnf install binaryen +# echo "Optimizing wasm…" +# wasm-opt docs/${CRATE_NAME_SNAKE_CASE}_bg.wasm -O2 --fast-math -o docs/${CRATE_NAME_SNAKE_CASE}_bg.wasm # add -g to get debug symbols + +echo "Finished: docs/${CRATE_NAME_SNAKE_CASE}.wasm" + +if [[ "$OSTYPE" == "linux-gnu"* ]]; then + # Linux, ex: Fedora + xdg-open http://localhost:8888/index.html +elif [[ "$OSTYPE" == "msys" ]]; then + # Windows + start http://localhost:8888/index.html +else + # Darwin/MacOS, or something else + open http://localhost:8888/index.html +fi diff --git a/docs/index.html b/docs/index.html new file mode 100644 index 00000000..44518251 --- /dev/null +++ b/docs/index.html @@ -0,0 +1,91 @@ + + + + + + + + + Puffin Viewer + + + + + + + + + + + + + + + + + + + + diff --git a/setup_web.sh b/setup_web.sh new file mode 100755 index 00000000..82679f41 --- /dev/null +++ b/setup_web.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -eu +script_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P ) +cd "$script_path" + +# Pre-requisites: +rustup target add wasm32-unknown-unknown +cargo install wasm-bindgen-cli +cargo update -p wasm-bindgen diff --git a/start_server.sh b/start_server.sh new file mode 100755 index 00000000..86130a50 --- /dev/null +++ b/start_server.sh @@ -0,0 +1,16 @@ +#!/bin/bash +set -eu +script_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P ) +cd "$script_path" + +# Starts a local web-server that serves the contents of the `doc/` folder, +# i.e. the web-version of `egui_demo_app`. + +echo "ensuring basic-http-server is installed…" +cargo install basic-http-server + +echo "starting server…" +echo "serving at http://localhost:8888" + +(cd docs && basic-http-server --addr 127.0.0.1:8888 .) +# (cd docs && python3 -m http.server 8888 --bind 127.0.0.1)