| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | |
| 4 | # Compare bundle size for explicitly selected entry files against a base ref. |
| 5 | # |
| 6 | # Usage: |
| 7 | # scripts/bundle-compare-selected.sh [--base <ref>] [--keep-base] <file...> |
| 8 | # |
| 9 | # The selected files are copied into the base worktree before measurement so the |
| 10 | # report isolates source changes instead of fixture content changes. |
| 11 | |
| 12 | usage() { |
| 13 | echo "Usage: scripts/bundle-compare-selected.sh [--base <ref>] [--keep-base] <file...>" |
| 14 | } |
| 15 | |
| 16 | BASE_REF="main" |
| 17 | KEEP_BASE_WORKTREE=false |
| 18 | FILES=() |
| 19 | |
| 20 | while [ "$#" -gt 0 ]; do |
| 21 | case "$1" in |
| 22 | -b | --base) |
| 23 | if [ "$#" -lt 2 ]; then |
| 24 | usage |
| 25 | exit 1 |
| 26 | fi |
| 27 | BASE_REF="$2" |
| 28 | shift 2 |
| 29 | ;; |
| 30 | --keep-base) |
| 31 | KEEP_BASE_WORKTREE=true |
| 32 | shift |
| 33 | ;; |
| 34 | -h | --help) |
| 35 | usage |
| 36 | exit 0 |
| 37 | ;; |
| 38 | --) |
| 39 | shift |
| 40 | FILES+=("$@") |
| 41 | break |
| 42 | ;; |
| 43 | -*) |
| 44 | echo "Unknown option: $1" >&2 |
| 45 | usage |
| 46 | exit 1 |
| 47 | ;; |
| 48 | *) |
| 49 | FILES+=("$1") |
| 50 | shift |
| 51 | ;; |
| 52 | esac |
| 53 | done |
| 54 | |
| 55 | if [ "${#FILES[@]}" -eq 0 ]; then |
| 56 | usage |
| 57 | exit 1 |
| 58 | fi |
| 59 | |
| 60 | ROOT="$(git rev-parse --show-toplevel)" |
| 61 | BASE_DIR="$ROOT/tmp/bundle-base" |
| 62 | BASE_STAMP="$BASE_DIR/.bundle-build-stamp" |
| 63 | |
| 64 | cleanup() { |
| 65 | if [ "$KEEP_BASE_WORKTREE" = "false" ] && [ -d "$BASE_DIR" ]; then |
| 66 | git worktree remove --force "$BASE_DIR" |
| 67 | fi |
| 68 | } |
| 69 | |
| 70 | trap cleanup EXIT |
| 71 | |
| 72 | cd "$ROOT" |
| 73 | |
| 74 | echo "Building current checkout..." |
| 75 | pnpm build |
| 76 | |
| 77 | BASE_COMMIT="$(git rev-parse "$BASE_REF")" |
| 78 | |
| 79 | if [ -d "$BASE_DIR/packages/effect/dist" ] && |
| 80 | [ -f "$BASE_STAMP" ] && |
| 81 | [ "$(git -C "$BASE_DIR" rev-parse HEAD)" = "$BASE_COMMIT" ] && |
| 82 | [ "$(cat "$BASE_STAMP")" = "$BASE_COMMIT" ]; then |
| 83 | echo "Reusing built base worktree at $BASE_DIR ($BASE_REF @ ${BASE_COMMIT:0:8})" |
| 84 | else |
| 85 | if [ -d "$BASE_DIR" ]; then |
| 86 | git worktree remove --force "$BASE_DIR" |
| 87 | fi |
| 88 | echo "Creating base worktree at $BASE_DIR ($BASE_REF @ ${BASE_COMMIT:0:8})" |
| 89 | git worktree add --detach "$BASE_DIR" "$BASE_COMMIT" |
| 90 | echo "Building base checkout..." |
| 91 | (cd "$BASE_DIR" && pnpm install && pnpm build) |
| 92 | printf "%s\n" "$BASE_COMMIT" > "$BASE_STAMP" |
| 93 | fi |
| 94 | |
| 95 | node packages/tools/bundle/src/bin.ts compare-selected \ |
| 96 | --base-dir "$BASE_DIR" \ |
| 97 | "${FILES[@]}" |