#!/usr/bin/env bash # Minify and gzip the web UI into v2/www_dist for the `www` LittleFS partition. # Usage: tools/build_web.sh (needs node/npx; WWW_MAX = partition size in bytes) set -euo pipefail cd "$(dirname "$0")/.." SRC=../src OUT=www_dist MAX=${WWW_MAX:-1048576} rm -rf "$OUT"; mkdir -p "$OUT/static" # Legacy files the v2 UI does not load SKIP=" app.js main.js rgb-slider.js styles.css " for f in "$SRC"/static/*; do [[ -f $f ]] || continue b=$(basename "$f"); [[ $SKIP == *" $b "* ]] && continue case $b in *.min.js) cp "$f" "$OUT/static/$b" ;; *.js|*.css) npx --yes esbuild@0.25.0 "$f" --minify --target=es2019 --legal-comments=none \ --outfile="$OUT/static/$b" --log-level=warning ;; *) cp "$f" "$OUT/static/$b" ;; esac done # The Pi does not serve /audio, so the Audio menu entries are added for this build only # (desktop button row and the mobile dropdown, both just before Help). tmp=$(mktemp -d) sed -e "s||\n&|" \ -e "s||\n&|" \ "$SRC/templates/index.html" > "$tmp/index.html" cp "$SRC/templates/settings.html" "$tmp/settings.html" grep -q 'id="audio-btn"' "$tmp/index.html" && [ "$(grep -c 'audio-btn' "$tmp/index.html")" -eq 2 ] \ || { echo "ERROR: could not add the Audio menu entries to index.html" >&2; exit 1; } for p in index settings; do npx --yes html-minifier-terser@7.2.0 --collapse-whitespace --remove-comments \ --minify-css true --minify-js true -o "$OUT/$p.html" "$tmp/$p.html" done rm -rf "$tmp" # Live audio and beat page (served at /audio) npx --yes html-minifier-terser@7.2.0 --collapse-whitespace --remove-comments \ --minify-css true --minify-js true -o "$OUT/audio.html" web/audio.html # Ship only the .gz copies; the server sends Content-Encoding: gzip find "$OUT" -type f -exec gzip -9 -n -f {} \; find "$OUT" -name '*.gz' -exec gzip -t {} \; total=$(find "$OUT" -name '*.gz' -printf '%s\n' | paste -sd+ | bc) echo "www_dist: $(find "$OUT" -name '*.gz' | wc -l) files, $total bytes (limit $MAX)" (( total <= MAX )) || { echo "ERROR: web UI exceeds www partition" >&2; exit 1; }