packages feed

nano-ui-sdl-web-0.2.0.0: scripts/build-web.sh

#!/usr/bin/env bash
# Optional bundler for nano-ui-sdl-web programs: compiles a wasm32 target with
# GHC's wasm backend and writes a self-contained static directory containing
# the HTML shell, JS runtime, wasm module, JSFFI glue, browser_wasi_shim, and
# fallback font.
#
#   source ~/.ghc-wasm/env
#   export PKG_CONFIG_PATH=~/.nano-ui-wasi/lib/pkgconfig   # build-wasi-deps.sh
#   bash path/to/nano-ui-sdl-web/scripts/build-web.sh [out-dir [cabal-target]]
#   python3 -m http.server -d <out-dir> 8000
#
# Works both inside the nano-ui repository and in downstream projects (using
# this script from `cabal unpack nano-ui-sdl-web` or from nano-ui-sdl-web's
# installed Cabal data-dir).
#
# Inside the nano-ui repository, out-dir defaults to <repo>/dist-web, target
# defaults to nano-ui-sdl-web:exe:nano-ui-sdl-web-demo, and cabal.project.wasm
# is selected automatically. In a downstream project, cabal runs in the current
# directory, out-dir defaults to ./dist-web, and cabal-target names your
# executable (or may be omitted if the project has a single executable). Set
# CABAL_PROJECT to pass an explicit --project-file to wasm32-wasi-cabal.
#
# The target must be an executable built as a reactor module that exports
# nanoMain (see the nano-ui-sdl-web README). Needs node and npm (both come
# with ghc-wasm-meta) for the JSFFI glue and the WASI shim.
#
# WASM_OPT_FLAGS are wasm-opt's optimisation flags (default -O2; -Oz makes a
# smaller, somewhat slower module). The script ends by printing the module's
# size as linked, as served, and under gzip, which is how a web server sends
# it.
set -euo pipefail

script_dir="$(cd "$(dirname "$0")" && pwd)"
pkg="$(cd "$script_dir/.." && pwd)"
cwd="$(pwd)"

# Detect whether we are running inside the nano-ui monorepo (from its root
# or any subdirectory) rather than a separate downstream project.
in_monorepo=0
repo=""
if [ -f "$pkg/../../cabal.project.wasm" ]; then
  repo="$(cd "$pkg/../.." && pwd)"
  case "$cwd/" in
    "$repo/"*) in_monorepo=1 ;;
  esac
fi

project_args=()
target_args=()
if [ -n "${CABAL_PROJECT:-}" ]; then
  project_args=(--project-file="$CABAL_PROJECT")
elif [ "$in_monorepo" -eq 1 ]; then
  project_args=(--project-file="$repo/cabal.project.wasm")
elif [ -f "$cwd/cabal.project.wasm" ]; then
  project_args=(--project-file="$cwd/cabal.project.wasm")
fi

if [ "$in_monorepo" -eq 1 ]; then
  work_dir="$repo"
  out="${1:-$repo/dist-web}"
  target="${2:-nano-ui-sdl-web:exe:nano-ui-sdl-web-demo}"
  target_args=("$target")
else
  work_dir="$cwd"
  out="${1:-$cwd/dist-web}"
  if [ -n "${2:-}" ]; then
    target="$2"
    target_args=("$target")
  elif [ "$cwd" = "$pkg" ]; then
    target="nano-ui-sdl-web:exe:nano-ui-sdl-web-demo"
    target_args=("$target")
  else
    target="default executable"
  fi
fi
shim_version="0.4.1"

command -v wasm32-wasi-cabal >/dev/null || { echo "no wasm32-wasi-cabal: source ~/.ghc-wasm/env first" >&2; exit 1; }

# -j1 keeps one GHC process at a time, as the native CI build does.
(cd "$work_dir" && wasm32-wasi-cabal build -j1 ${project_args[@]+"${project_args[@]}"} ${target_args[@]+"${target_args[@]}"})
wasm="$(cd "$work_dir" && wasm32-wasi-cabal list-bin ${project_args[@]+"${project_args[@]}"} ${target_args[@]+"${target_args[@]}"})"

mkdir -p "$out"
out="$(cd "$out" && pwd)"
cp "$pkg"/web/index.html "$pkg"/web/favicon.svg "$pkg"/web/main.js "$pkg"/web/nano-ui-sdl-web.js "$out"/
# The fallback font the default options load, with its licence.
mkdir -p "$out/fonts"
cp "$pkg"/web/fonts/*.ttf "$out/fonts/"
if [ -f "$pkg/LICENSE-Inter" ]; then
  cp "$pkg/LICENSE-Inter" "$out/fonts/"
fi
# The glue the module's JavaScript imports need, generated from the module
# before wasm-opt drops the custom section it is read from.
"$(wasm32-wasi-ghc --print-libdir)/post-link.mjs" -i "$wasm" -o "$out/ghc_wasm_jsffi.js"
if command -v wasm-opt >/dev/null; then
  # shellcheck disable=SC2086
  wasm-opt --strip-debug ${WASM_OPT_FLAGS:--O2} "$wasm" -o "$out/main.wasm"
else
  cp "$wasm" "$out/main.wasm"
fi

# The WASI shim main.js imports, from npm, with its licences.
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
(cd "$tmp" && npm pack --silent "@bjorn3/browser_wasi_shim@$shim_version" >/dev/null && tar xzf ./*.tgz)
rm -rf "$out/browser_wasi_shim"
mkdir -p "$out/browser_wasi_shim"
cp "$tmp"/package/dist/*.js "$tmp"/package/LICENSE-* "$out/browser_wasi_shim/"

echo "Size of $target: $(wc -c <"$wasm") bytes linked," \
  "$(wc -c <"$out/main.wasm") served, $(gzip -9 -c "$out/main.wasm" | wc -c) under gzip."
echo "Built $out. Serve it with:"
echo "  python3 -m http.server -d $out 8000"