Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 49 additions & 43 deletions scripts/bundle.macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,64 @@ set -e
# Run common bundling steps
bash ./scripts/bundle.sh

export PLAYWRIGHT_BROWSERS_PATH=$PWD/bundle/Cache/
export PLAYWRIGHT_SKIP_BROWSER_GC=1

# Final cache location used by the bundle at runtime
FINAL_CACHE="$PWD/bundle/Cache"

pushd bundle/

# --- Step 1: Install ARM64 browsers for macOS 14/15 (Apple Silicon) ---
# Use mac14-arm64 override instead of native mac15-arm64 so that webkit is
# compiled against the macOS 14 SDK. This binary is forward-compatible with
# macOS 15 but the reverse is not true (mac15 webkit uses symbols absent on 14).
# NOTE: Firefox is deliberately excluded here. Both arm64 and x64 Firefox
# extract to the same directory path (firefox-<rev>/firefox/), so only one
# can exist. We install x64 Firefox in Step 3, which runs natively on Intel
# Macs and via Rosetta 2 on Apple Silicon.
PLAYWRIGHT_HOST_PLATFORM_OVERRIDE=mac14-arm64 npx playwright install chromium chromium-headless-shell webkit
PLAYWRIGHT_HOST_PLATFORM_OVERRIDE=mac14-arm64 npx playwright install-deps chromium webkit
# =============================================================================
# Multi-arch bundling strategy: Install ARM64 and x64 browsers into separate
# isolated directories, then merge into the final Cache/.
#
# This avoids fragile INSTALLATION_COMPLETE marker manipulation and the
# backup/restore dance needed when both architectures share a single cache.
#
# - Chromium & Headless Shell: arm64 and x64 extract to different subdirectory
# names (chrome-mac-arm64/ vs chrome-mac-x64/), so they coexist after merge.
# - Firefox: Both architectures extract to the same path (firefox/), so only
# x64 is kept — it runs on Apple Silicon via Rosetta 2.
# - WebKit: Only arm64 is installed (mac14-arm64 for macOS 14/15 compat).
# Playwright 1.58+ dropped webkit support for mac13.
# =============================================================================

# --- Step 1: Install ARM64 browsers into isolated directory ---
echo "--- Step 1: Installing ARM64 browsers (mac14-arm64) ---"
export PLAYWRIGHT_BROWSERS_PATH="$PWD/Cache-arm64"
export PLAYWRIGHT_HOST_PLATFORM_OVERRIDE=mac14-arm64
npx playwright install chromium chromium-headless-shell webkit
npx playwright install-deps chromium webkit
unset PLAYWRIGHT_HOST_PLATFORM_OVERRIDE

# --- Step 2: Install x64 browsers into isolated directory ---
echo "--- Step 2: Installing x64 browsers (mac13) ---"
export PLAYWRIGHT_BROWSERS_PATH="$PWD/Cache-intel"
export PLAYWRIGHT_HOST_PLATFORM_OVERRIDE=mac13
npx playwright install chromium chromium-headless-shell firefox
npx playwright install-deps chromium firefox
unset PLAYWRIGHT_HOST_PLATFORM_OVERRIDE

# --- Step 3: Merge both caches into the final Cache/ directory ---
echo "--- Step 3: Merging ARM64 and x64 browsers into final cache ---"
rm -rf "$FINAL_CACHE"
mkdir -p "$FINAL_CACHE"

# --- Step 2: Back up ARM64 Chromium files ---
# Playwright deletes the entire browser revision directory (e.g. chromium-1208/)
# when INSTALLATION_COMPLETE is missing. We must preserve the arm64 subdirectories
# before the x64 install wipes them out.
ARM64_BACKUP=$(mktemp -d)
find "$PLAYWRIGHT_BROWSERS_PATH" -type d -name "chrome-mac-arm64" -exec cp -a {} "$ARM64_BACKUP/chrome-mac-arm64" \;
find "$PLAYWRIGHT_BROWSERS_PATH" -type d -name "chrome-headless-shell-mac-arm64" -exec cp -a {} "$ARM64_BACKUP/chrome-headless-shell-mac-arm64" \;
# Use x64 (Intel) as the base layer
cp -a Cache-intel/* "$FINAL_CACHE/"

# --- Step 3: Remove INSTALLATION_COMPLETE markers ---
# Playwright checks for this marker and silently skips if present — even when
# the needed platform variant (e.g. x64) is missing. Removing the markers
# forces the x86 install to re-download.
find "$PLAYWRIGHT_BROWSERS_PATH" -name "INSTALLATION_COMPLETE" -delete
# Overlay ARM64 on top — rsync --ignore-existing adds arm64 subdirectories
# (chrome-mac-arm64/, webkit-*/) without overwriting x64 files or markers.
rsync -a --ignore-existing Cache-arm64/ "$FINAL_CACHE/"

# --- Step 4: Install x86 browsers for macOS 12/13 (Intel) ---
# Firefox and ffmpeg share the same subdirectory for both architectures, so
# this step overwrites the ARM64 copies with x64 — which is intentional since
# x64 binaries run on Apple Silicon via Rosetta 2.
PLAYWRIGHT_HOST_PLATFORM_OVERRIDE=mac13 npx playwright install chromium chromium-headless-shell firefox
PLAYWRIGHT_HOST_PLATFORM_OVERRIDE=mac13 npx playwright install-deps chromium firefox
# Clean up temporary caches
rm -rf Cache-arm64 Cache-intel

# --- Step 5: Restore ARM64 Chromium files ---
# Copy arm64 subdirectories back into the browser revision directories so both
# architectures coexist (chrome-mac-arm64/ alongside chrome-mac-x64/).
for chromium_dir in "$PLAYWRIGHT_BROWSERS_PATH"/chromium-*/; do
if [ -d "$ARM64_BACKUP/chrome-mac-arm64" ]; then
cp -a "$ARM64_BACKUP/chrome-mac-arm64" "$chromium_dir/"
fi
done
for headless_dir in "$PLAYWRIGHT_BROWSERS_PATH"/chromium_headless_shell-*/; do
if [ -d "$ARM64_BACKUP/chrome-headless-shell-mac-arm64" ]; then
cp -a "$ARM64_BACKUP/chrome-headless-shell-mac-arm64" "$headless_dir/"
fi
done
rm -rf "$ARM64_BACKUP"
export PLAYWRIGHT_BROWSERS_PATH="$FINAL_CACHE"

# --- Verify ---
echo "--- Verification: final cache contents ---"
find "$FINAL_CACHE" -maxdepth 2 -type d | sort
npx playwright --version

popd
Expand Down
Loading