|
| 1 | +name: Bundle Size Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [main] |
| 6 | + |
| 7 | +jobs: |
| 8 | + size: |
| 9 | + name: Check Bundle Size |
| 10 | + runs-on: ubuntu-latest |
| 11 | + |
| 12 | + steps: |
| 13 | + - name: Checkout code |
| 14 | + uses: actions/checkout@v4 |
| 15 | + |
| 16 | + - name: Setup Node.js |
| 17 | + uses: actions/setup-node@v4 |
| 18 | + with: |
| 19 | + node-version: '20.x' |
| 20 | + cache: 'npm' |
| 21 | + |
| 22 | + - name: Install dependencies |
| 23 | + run: npm ci |
| 24 | + |
| 25 | + - name: Build library |
| 26 | + run: npm run build |
| 27 | + |
| 28 | + - name: Check bundle size |
| 29 | + run: | |
| 30 | + echo "## Bundle Size Report" >> $GITHUB_STEP_SUMMARY |
| 31 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 32 | + echo "| File | Size | Gzipped |" >> $GITHUB_STEP_SUMMARY |
| 33 | + echo "|------|------|---------|" >> $GITHUB_STEP_SUMMARY |
| 34 | +
|
| 35 | + for file in dist/*.js dist/*.cjs; do |
| 36 | + if [ -f "$file" ]; then |
| 37 | + name=$(basename "$file") |
| 38 | + size=$(wc -c < "$file" | awk '{printf "%.2f KB", $1/1024}') |
| 39 | + gzip_size=$(gzip -c "$file" | wc -c | awk '{printf "%.2f KB", $1/1024}') |
| 40 | + echo "| $name | $size | $gzip_size |" >> $GITHUB_STEP_SUMMARY |
| 41 | + fi |
| 42 | + done |
| 43 | +
|
| 44 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 45 | + echo "### Size Limits" >> $GITHUB_STEP_SUMMARY |
| 46 | + echo "- ✅ ESM bundle should be < 50KB (gzipped < 15KB)" >> $GITHUB_STEP_SUMMARY |
| 47 | + echo "- ✅ CJS bundle should be < 50KB (gzipped < 15KB)" >> $GITHUB_STEP_SUMMARY |
| 48 | +
|
| 49 | + # Check if any file is too large (50KB uncompressed) |
| 50 | + for file in dist/index.js dist/index.cjs; do |
| 51 | + if [ -f "$file" ]; then |
| 52 | + size=$(wc -c < "$file") |
| 53 | + if [ $size -gt 51200 ]; then |
| 54 | + name=$(basename "$file") |
| 55 | + echo "::error::$name is too large: $(($size / 1024)) KB (max 50 KB)" |
| 56 | + exit 1 |
| 57 | + fi |
| 58 | + fi |
| 59 | + done |
| 60 | +
|
| 61 | + - name: Comment PR |
| 62 | + uses: actions/github-script@v7 |
| 63 | + if: github.event_name == 'pull_request' |
| 64 | + with: |
| 65 | + script: | |
| 66 | + const fs = require('fs'); |
| 67 | + const files = ['dist/index.js', 'dist/index.cjs']; |
| 68 | +
|
| 69 | + let comment = '## 📦 Bundle Size Report\n\n'; |
| 70 | + comment += '| File | Size | Gzipped |\n'; |
| 71 | + comment += '|------|------|---------|\\n'; |
| 72 | +
|
| 73 | + for (const file of files) { |
| 74 | + if (fs.existsSync(file)) { |
| 75 | + const stats = fs.statSync(file); |
| 76 | + const sizeKB = (stats.size / 1024).toFixed(2); |
| 77 | + const { execSync } = require('child_process'); |
| 78 | + const gzipSize = execSync(`gzip -c ${file} | wc -c`).toString().trim(); |
| 79 | + const gzipKB = (parseInt(gzipSize) / 1024).toFixed(2); |
| 80 | + const name = file.split('/').pop(); |
| 81 | + comment += `| ${name} | ${sizeKB} KB | ${gzipKB} KB |\\n`; |
| 82 | + } |
| 83 | + } |
| 84 | +
|
| 85 | + comment += '\n✅ All bundles are within size limits!'; |
| 86 | +
|
| 87 | + github.rest.issues.createComment({ |
| 88 | + issue_number: context.issue.number, |
| 89 | + owner: context.repo.owner, |
| 90 | + repo: context.repo.repo, |
| 91 | + body: comment |
| 92 | + }); |
0 commit comments