|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Test bundler compatibility for @googlemaps/js-api-loader |
| 4 | +# This script tests that the library works with various popular bundlers |
| 5 | +# without requiring any configuration from the user. |
| 6 | + |
| 7 | +set -e # Exit on any error |
| 8 | + |
| 9 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 10 | +ROOT_DIR="$(dirname "$SCRIPT_DIR")" |
| 11 | + |
| 12 | +echo "======================================" |
| 13 | +echo "Testing Bundler Compatibility" |
| 14 | +echo "======================================" |
| 15 | +echo "" |
| 16 | + |
| 17 | +# Colors for output |
| 18 | +GREEN='\033[0;32m' |
| 19 | +RED='\033[0;31m' |
| 20 | +NC='\033[0m' # No Color |
| 21 | + |
| 22 | +# Pack the library |
| 23 | +echo "📦 Packing library..." |
| 24 | +cd "$ROOT_DIR" |
| 25 | +TARBALL=$(npm pack --silent 2> /dev/null) |
| 26 | +echo " created: $TARBALL" |
| 27 | +echo "" |
| 28 | + |
| 29 | +for bundler in vite webpack rollup ; do |
| 30 | + dir="$SCRIPT_DIR/${bundler}-test" |
| 31 | + |
| 32 | + ( |
| 33 | + cd $dir |
| 34 | + # Install dependencies |
| 35 | + echo " Installing dependencies for $bundler..." |
| 36 | + npm install --silent |
| 37 | + npm install --silent "../../$TARBALL" |
| 38 | + ) |
| 39 | +done |
| 40 | + |
| 41 | +echo "" |
| 42 | + |
| 43 | +# Function to test a bundler in production mode |
| 44 | +test_bundler_prod() { |
| 45 | + local bundler=$1 |
| 46 | + local dir="$SCRIPT_DIR/${bundler}-test" |
| 47 | + |
| 48 | + cd "$dir" |
| 49 | + |
| 50 | + echo "Testing $bundler (production)..." |
| 51 | + |
| 52 | + # Run production build |
| 53 | + echo " Building (production)..." |
| 54 | + if npm run build > build.log 2>&1; then |
| 55 | + # Check for process.env.NODE_ENV references in actual code (not in strings/comments) |
| 56 | + # We pipe find to xargs to grep through all bundles at once. |
| 57 | + # The if condition checks the exit code of grep. |
| 58 | + if find dist -name "*.js" -type f -print0 | xargs -0 grep -qE "process\.env\.NODE_ENV|process\.env\\["; then |
| 59 | + echo -e " ${RED}✗ NUM_FAILED${NC} - Found process.env.NODE_ENV in bundle code" |
| 60 | + return 1 |
| 61 | + fi |
| 62 | + |
| 63 | + # Verify that importLibrary is still present (not tree-shaken) |
| 64 | + if ! find dist -name "*.js" -type f -exec grep -q "importLibrary" {} \;; then |
| 65 | + echo -e " ${RED}✗ NUM_FAILED${NC} - importLibrary was incorrectly tree-shaken" |
| 66 | + return 1 |
| 67 | + fi |
| 68 | + |
| 69 | + echo -e " ${GREEN}✓ PASSED${NC} - process.env replaced, bundle optimized" |
| 70 | + return 0 |
| 71 | + else |
| 72 | + echo -e " ${RED}✗ NUM_FAILED${NC} - Build error" |
| 73 | + echo "~~~~ build log:" |
| 74 | + cat build.log |
| 75 | + echo "~~~~" |
| 76 | + |
| 77 | + return 1 |
| 78 | + fi |
| 79 | +} |
| 80 | + |
| 81 | +# Function to test a bundler in development mode |
| 82 | +test_bundler_dev() { |
| 83 | + local bundler=$1 |
| 84 | + local dir="$SCRIPT_DIR/${bundler}-test" |
| 85 | + |
| 86 | + cd "$dir" |
| 87 | + |
| 88 | + echo "Testing $bundler (development)..." |
| 89 | + |
| 90 | + # Run development build |
| 91 | + echo " Building (development)..." |
| 92 | + rm -rf dist |
| 93 | + if npm run build:dev > build-dev.log 2>&1; then |
| 94 | + # Check that dev warning code is present in the bundle |
| 95 | + # We look for the actual console.warn message, not just the function definition. |
| 96 | + if find dist -name "*.js" -type f -print0 | xargs -0 grep -qE "console\.warn.*@googlemaps/js-api-loader"; then |
| 97 | + echo -e " ${GREEN}✓ PASSED${NC} - dev warnings preserved" |
| 98 | + return 0 |
| 99 | + else |
| 100 | + echo -e " ${RED}✗ NUM_FAILED${NC} - dev warning code not found (dead code eliminated incorrectly)" |
| 101 | + cat build-dev.log |
| 102 | + return 1 |
| 103 | + fi |
| 104 | + else |
| 105 | + echo -e " ${RED}✗ NUM_FAILED${NC} - Build error" |
| 106 | + cat build-dev.log |
| 107 | + return 1 |
| 108 | + fi |
| 109 | +} |
| 110 | + |
| 111 | +NUM_FAILED=0 |
| 112 | + |
| 113 | +# Production mode tests |
| 114 | +echo "========== PRODUCTION MODE ==========" |
| 115 | + |
| 116 | +for bundler in vite webpack rollup ; do |
| 117 | + echo "" |
| 118 | + test_bundler_prod $bundler || NUM_FAILED=$((NUM_FAILED + 1)) |
| 119 | +done |
| 120 | +echo "" |
| 121 | + |
| 122 | +# Development mode tests |
| 123 | +echo "========== DEVELOPMENT MODE ==========" |
| 124 | +for bundler in vite webpack rollup ; do |
| 125 | + echo "" |
| 126 | + test_bundler_dev $bundler || NUM_FAILED=$((NUM_FAILED + 1)) |
| 127 | +done |
| 128 | +echo "" |
| 129 | + |
| 130 | +# Summary |
| 131 | +echo "======================================" |
| 132 | +if [ $NUM_FAILED -eq 0 ]; then |
| 133 | + echo -e "${GREEN}✓ All bundler tests passed!${NC}" |
| 134 | + exit 0 |
| 135 | +else |
| 136 | + echo -e "${RED}✗ $NUM_FAILED bundler test(s) failed${NC}" |
| 137 | + exit 1 |
| 138 | +fi |
0 commit comments