#!/usr/bin/env bash # Suramadu compresses nothing on the fly - AbstractResourceHandler serves a # precompressed sibling when one is already next to the file, and the plain file # otherwise. So a missing sibling is not a slow path, it is an uncompressed one, # and the only thing standing between our webFolder and ~70KB on every cold load # is that these files get written at build time. # # What is checked: the siblings appear, they are smaller than their source, a # file that does not compress gets none rather than a bigger one, and the # already-compressed assets are left alone. set -uo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" SCRIPT="$ROOT/suramadu/precompress-web.sh" TMP="$(mktemp -d)" trap 'rm -rf "$TMP"' EXIT fail=0 check() { if "${@:2}"; then echo "ok $1"; else echo "FAIL $1"; fail=1; fi; } not() { ! "$@"; } [ -x "$SCRIPT" ] || { echo "FAIL $SCRIPT is missing or not executable"; exit 1; } for tool in gzip brotli; do command -v "$tool" >/dev/null || { echo "SKIP test-precompress-web: no $tool in this environment"; exit 0; } done W="$TMP/web" mkdir -p "$W/fonts" # Compressible, and long enough that the gzip header cannot eat the win. rep() { local i; for ((i = 0; i < 400; i++)); do printf '%s\n' "$2"; done > "$1"; } rep "$W/big.js" 'function hexIsVisible(a,b){return a===b;}' rep "$W/big.css" '.hex { color: #4c8dff; }' rep "$W/page.html" '

lorem hex

' # Incompressible, in a file the extension list matches: random bytes, so the # sibling comes out bigger than the source and has to be dropped. head -c 4096 /dev/urandom > "$W/noise.svg" # Already compressed, and not in the extension list either. head -c 4096 /dev/urandom > "$W/fonts/a.woff2" DIR="$W" bash "$SCRIPT" > "$TMP/out" 2>&1 check "the script succeeds" test $? -eq 0 for f in big.js big.css page.html; do check "$f gets a .gz" test -f "$W/$f.gz" check "$f gets a .br" test -f "$W/$f.br" check "$f.gz is smaller than $f" \ bash -c '[ "$(wc -c < "$1.gz")" -lt "$(wc -c < "$1")" ]' _ "$W/$f" check "$f.br is smaller than $f" \ bash -c '[ "$(wc -c < "$1.br")" -lt "$(wc -c < "$1")" ]' _ "$W/$f" done # A sibling the handler would serve to send *more* bytes than the plain file is # worse than none, so it is deleted rather than shipped. check "an incompressible file gets no .gz" not test -f "$W/noise.svg.gz" check "an incompressible file gets no .br" not test -f "$W/noise.svg.br" # woff2 is already compressed and is not in the extension list; a .gz of it # would be bigger than the font. check "fonts are left alone" not test -f "$W/fonts/a.woff2.gz" # Running twice must not compound - siblings are rebuilt from the source, and # are never themselves treated as input. before="$(wc -c < "$W/big.js.gz")" DIR="$W" bash "$SCRIPT" >/dev/null 2>&1 check "a second run is idempotent" test "$(wc -c < "$W/big.js.gz")" -eq "$before" check "siblings are not compressed again" not test -f "$W/big.js.gz.gz" check "it reports what it did" grep -q "precompressed 4 file(s)" "$TMP/out" # An empty directory is a build that silently shipped nothing. mkdir -p "$TMP/empty" DIR="$TMP/empty" bash "$SCRIPT" >/dev/null 2>&1 check "an empty web folder fails the build" test $? -ne 0 exit "$fail"