|
| 1 | +name: Bundle Size Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [main, next, develop] |
| 6 | + push: |
| 7 | + branches: [main, next, develop] |
| 8 | + |
| 9 | +jobs: |
| 10 | + check-bundle-size: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + steps: |
| 14 | + - name: Checkout code |
| 15 | + uses: actions/checkout@v4 |
| 16 | + with: |
| 17 | + fetch-depth: 0 # Full history for comparison |
| 18 | + |
| 19 | + - name: Setup Node.js |
| 20 | + uses: actions/setup-node@v4 |
| 21 | + with: |
| 22 | + node-version: '18' |
| 23 | + cache: 'npm' |
| 24 | + |
| 25 | + - name: Install dependencies |
| 26 | + run: npm ci |
| 27 | + |
| 28 | + - name: Build current version |
| 29 | + run: npm run build |
| 30 | + |
| 31 | + - name: Get current bundle size |
| 32 | + id: current-size |
| 33 | + run: | |
| 34 | + # Get total gzipped size |
| 35 | + TOTAL_SIZE=$(find dist -name '*.js' -o -name '*.css' | xargs gzip -c | wc -c) |
| 36 | + TOTAL_SIZE_KB=$((TOTAL_SIZE / 1024)) |
| 37 | + |
| 38 | + # Get individual chunk sizes |
| 39 | + echo "📦 Current Bundle Sizes:" |
| 40 | + echo "" |
| 41 | + echo "| File | Size (gzipped) |" |
| 42 | + echo "|------|----------------|" |
| 43 | + |
| 44 | + find dist/assets -name '*.js' -type f | while read file; do |
| 45 | + FILENAME=$(basename "$file") |
| 46 | + GZIP_SIZE=$(gzip -c "$file" | wc -c) |
| 47 | + GZIP_SIZE_KB=$((GZIP_SIZE / 1024)) |
| 48 | + echo "| $FILENAME | ${GZIP_SIZE_KB} KB |" |
| 49 | + done |
| 50 | + |
| 51 | + echo "" |
| 52 | + echo "**Total Bundle Size: ${TOTAL_SIZE_KB} KB (gzipped)**" |
| 53 | + echo "" |
| 54 | + |
| 55 | + # Save for comparison |
| 56 | + echo "size=$TOTAL_SIZE_KB" >> $GITHUB_OUTPUT |
| 57 | + echo "${TOTAL_SIZE_KB}" > current-size.txt |
| 58 | + |
| 59 | + - name: Check against target |
| 60 | + run: | |
| 61 | + CURRENT_SIZE=${{ steps.current-size.outputs.size }} |
| 62 | + TARGET_SIZE=350 |
| 63 | + STRETCH_TARGET=300 |
| 64 | + |
| 65 | + echo "### 🎯 Bundle Size Report" |
| 66 | + echo "" |
| 67 | + echo "- **Current:** ${CURRENT_SIZE} KB" |
| 68 | + echo "- **Target:** ${TARGET_SIZE} KB" |
| 69 | + echo "- **Stretch Goal:** ${STRETCH_TARGET} KB" |
| 70 | + echo "" |
| 71 | + |
| 72 | + if [ $CURRENT_SIZE -le $STRETCH_TARGET ]; then |
| 73 | + echo "✅ **Excellent!** Bundle is under stretch goal!" |
| 74 | + echo "🏆 Savings: $((TARGET_SIZE - CURRENT_SIZE)) KB under target" |
| 75 | + elif [ $CURRENT_SIZE -le $TARGET_SIZE ]; then |
| 76 | + echo "✅ **Good!** Bundle meets target size!" |
| 77 | + echo "📊 Savings: $((TARGET_SIZE - CURRENT_SIZE)) KB under target" |
| 78 | + else |
| 79 | + echo "⚠️ **Warning:** Bundle exceeds target!" |
| 80 | + echo "📈 Over by: $((CURRENT_SIZE - TARGET_SIZE)) KB" |
| 81 | + |
| 82 | + # Don't fail the build, just warn |
| 83 | + if [ $CURRENT_SIZE -gt 450 ]; then |
| 84 | + echo "❌ **Error:** Bundle is critically large (>450 KB)!" |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | + fi |
| 88 | + |
| 89 | + - name: Compare with base branch (PR only) |
| 90 | + if: github.event_name == 'pull_request' |
| 91 | + run: | |
| 92 | + # Checkout base branch |
| 93 | + git fetch origin ${{ github.base_ref }} |
| 94 | + git checkout origin/${{ github.base_ref }} |
| 95 | + |
| 96 | + # Install and build base |
| 97 | + npm ci --prefer-offline |
| 98 | + npm run build |
| 99 | + |
| 100 | + # Get base size |
| 101 | + BASE_SIZE=$(find dist -name '*.js' -o -name '*.css' | xargs gzip -c | wc -c) |
| 102 | + BASE_SIZE_KB=$((BASE_SIZE / 1024)) |
| 103 | + |
| 104 | + # Checkout PR branch again |
| 105 | + git checkout ${{ github.head_ref }} |
| 106 | + |
| 107 | + # Compare |
| 108 | + CURRENT_SIZE=${{ steps.current-size.outputs.size }} |
| 109 | + DIFF=$((CURRENT_SIZE - BASE_SIZE_KB)) |
| 110 | + |
| 111 | + echo "### 📊 Bundle Size Comparison" |
| 112 | + echo "" |
| 113 | + echo "| Metric | Size |" |
| 114 | + echo "|--------|------|" |
| 115 | + echo "| Base (${{ github.base_ref }}) | ${BASE_SIZE_KB} KB |" |
| 116 | + echo "| Current (PR) | ${CURRENT_SIZE} KB |" |
| 117 | + echo "| **Difference** | **${DIFF} KB** |" |
| 118 | + echo "" |
| 119 | + |
| 120 | + if [ $DIFF -lt 0 ]; then |
| 121 | + echo "✅ **Bundle size decreased by $((-DIFF)) KB!** 🎉" |
| 122 | + elif [ $DIFF -eq 0 ]; then |
| 123 | + echo "✅ **No change in bundle size**" |
| 124 | + else |
| 125 | + PERCENT=$(echo "scale=1; $DIFF * 100 / $BASE_SIZE_KB" | bc) |
| 126 | + echo "⚠️ **Bundle size increased by ${DIFF} KB (+${PERCENT}%)**" |
| 127 | + |
| 128 | + if (( $(echo "$PERCENT > 5" | bc -l) )); then |
| 129 | + echo "❌ **Error:** Bundle size increased by more than 5%!" |
| 130 | + exit 1 |
| 131 | + fi |
| 132 | + fi |
| 133 | + |
| 134 | + - name: Upload bundle analysis |
| 135 | + uses: actions/upload-artifact@v4 |
| 136 | + with: |
| 137 | + name: bundle-analysis |
| 138 | + path: | |
| 139 | + dist/bundle-analysis.html |
| 140 | + current-size.txt |
| 141 | + retention-days: 30 |
| 142 | + |
| 143 | + - name: Comment PR (if applicable) |
| 144 | + if: github.event_name == 'pull_request' |
| 145 | + uses: actions/github-script@v7 |
| 146 | + with: |
| 147 | + script: | |
| 148 | + const fs = require('fs'); |
| 149 | + const size = fs.readFileSync('current-size.txt', 'utf8').trim(); |
| 150 | + |
| 151 | + const comment = `### 📦 Bundle Size Report |
| 152 | + |
| 153 | + **Current Size:** ${size} KB (gzipped) |
| 154 | + **Target:** 350 KB |
| 155 | + **Stretch Goal:** 300 KB |
| 156 | + |
| 157 | + [View detailed bundle analysis](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) |
| 158 | + `; |
| 159 | + |
| 160 | + github.rest.issues.createComment({ |
| 161 | + issue_number: context.issue.number, |
| 162 | + owner: context.repo.owner, |
| 163 | + repo: context.repo.repo, |
| 164 | + body: comment |
| 165 | + }); |
| 166 | +
|
0 commit comments