winzik/tools/package-web.sh
David Nurdin 070aece6b4 PWA: offline bar and "new version" bar of the page, the cached build really starts offline
- jumphp-pwa.js (pwa on, not framed by a portal): a small bar while the network is gone (also when the
  worker had to answer from its cache while navigator.onLine says true; a probe takes it away when the
  network is back) and a "new version, reload" bar when a newer build waits (Pwa::applyUpdate()),
  14 languages
- sw.js: tells the page it was opened from the cache (to the new page of a navigation, and on request:
  {type: 'jumphp:offline?'}), an unversioned file falls back to the cached one, redirects never kept
- jumphp.js hears the worker from the start (window.JUMPHP_OFFLINE, event 'jumphp:offline')
- package-web.sh: jumphp.js was missing from version.json, so the worker never cached it and a
  normal build with pwa on did not start offline
- AGENTS.md: the bars, and the playground's games installed as apps of their own and played offline

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JkYTqsvYu6AYr9yQ3Q4Nt6
2026-11-15 20:34:33 +01:00

44 lines
2 KiB
Shell
Executable file

#!/bin/sh
# Browser build, step 2 (runs in the emscripten image, called by bin/jumphp build): packs the staged game into
# php-web.data (mounted at /game in the browser), injects its loader into the engine, writes version.json.
set -eu
GAME_DIR="$1"
GAME_ID="$(basename "$GAME_DIR")"
STAGE="/workspace/.build/$GAME_ID"
ENGINE=/workspace/build/engine
OUT="$GAME_DIR/dist/web"
rm -rf "$OUT"
mkdir -p "$OUT"
cp -R "$STAGE".web/. "$OUT/"
cp "$ENGINE/php-web.mjs" "$ENGINE/php-web.wasm" "$ENGINE/php-wasm-network.js" "$ENGINE/engine.json" "$OUT/"
# OpenSSH as WebAssembly (bin/jumphp ssh:build), for applications that use $engine->ssh()
[ -f /workspace/build/ssh/ssh.wasm ] && cp /workspace/build/ssh/ssh.js /workspace/build/ssh/ssh.wasm "$OUT/"
cd "$OUT"
python3 /emsdk/upstream/emscripten/tools/file_packager.py php-web.data \
--preload "$STAGE@/game" --lz4 --use-preload-cache --no-node \
--exclude '*/.git' --exclude '*/.git*' --exclude '*/.DS_Store' --js-output=php-web.data.js --export-name=createPhpModule >/dev/null
# at -O3 emcc drops the --pre-js comment: the data loader goes right after the module's run()
MARKER='Module\["preInit"\]\.pop()()}}run();'
grep -q "$MARKER" php-web.mjs || { echo "injection marker not found in php-web.mjs (engine built with another -O?)" >&2; exit 1; }
sed "/$MARKER/r php-web.data.js" php-web.mjs > php-web.mjs.tmp && mv php-web.mjs.tmp php-web.mjs
rm php-web.data.js
hash() { sha256sum "$1" | cut -c1-12; }
cat > version.json <<JSON
{
"build": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
"php-web.mjs": "$(hash php-web.mjs)",
"php-web.wasm": "$(hash php-web.wasm)",
"php-web.data": "$(hash php-web.data)",
$(for f in jumphp-*.js; do printf ' "%s": "%s",\n' "$f" "$(hash "$f")"; done)
"jumphp.js": "$(hash jumphp.js)",
"touch.js": "$(hash touch.js)",
"php-wasm-network.js": "$(hash php-wasm-network.js)"
}
JSON
# offline cache (game.yaml pwa): a new build = a new service worker
[ -f sw.js ] && sed -i "s/__JUMPHP_BUILD__/$(hash version.json)/" sw.js
ls -la "$OUT"