|
| 1 | +# Bundle Size Tracking |
| 2 | + |
| 3 | +This directory contains scripts for tracking and reporting bundle size changes in WordPress Playground. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The bundle size tracking system helps ensure that changes to the codebase don't significantly increase the download size required for: |
| 8 | + |
| 9 | +1. **First Paint**: Assets needed to display the initial WordPress Playground interface |
| 10 | +2. **Offline Mode**: Assets cached for offline functionality |
| 11 | + |
| 12 | +## Scripts |
| 13 | + |
| 14 | +### `analyze-bundle-size.mjs` |
| 15 | + |
| 16 | +Analyzes the build output and generates a detailed report of asset sizes. |
| 17 | + |
| 18 | +**Usage:** |
| 19 | + |
| 20 | +```bash |
| 21 | +npm run build:website |
| 22 | +node tools/scripts/analyze-bundle-size.mjs |
| 23 | +``` |
| 24 | + |
| 25 | +**Output:** |
| 26 | + |
| 27 | +- `bundle-size-report.json`: Detailed JSON report with size information for all assets |
| 28 | + |
| 29 | +**What it measures:** |
| 30 | + |
| 31 | +- Total size and gzipped size for first paint assets |
| 32 | +- Total size and gzipped size for offline mode assets |
| 33 | +- Individual file sizes |
| 34 | +- Top 10 largest files in each category |
| 35 | + |
| 36 | +### `compare-bundle-size.mjs` |
| 37 | + |
| 38 | +Compares two bundle size reports and generates a markdown report suitable for GitHub PR comments. |
| 39 | + |
| 40 | +**Usage:** |
| 41 | + |
| 42 | +```bash |
| 43 | +node tools/scripts/compare-bundle-size.mjs [base-report] [current-report] |
| 44 | +``` |
| 45 | + |
| 46 | +**Default paths:** |
| 47 | + |
| 48 | +- `base-report`: `bundle-size-report-base.json` |
| 49 | +- `current-report`: `bundle-size-report.json` |
| 50 | + |
| 51 | +**Output:** |
| 52 | + |
| 53 | +- `bundle-size-comment.md`: Markdown-formatted comparison report |
| 54 | +- GitHub Actions outputs for workflow automation |
| 55 | + |
| 56 | +## CI Workflow |
| 57 | + |
| 58 | +The bundle size check runs automatically on pull requests via the `.github/workflows/bundle-size-check.yml` workflow. |
| 59 | + |
| 60 | +### How it works |
| 61 | + |
| 62 | +1. **Build Current Branch**: Builds the website from the PR branch and analyzes the bundle size |
| 63 | +2. **Build Base Branch**: Checks out and builds the base branch (usually `trunk`) and analyzes its bundle size |
| 64 | +3. **Compare**: Generates a comparison report showing size changes |
| 65 | +4. **Comment**: If size changes exceed 50 KB (gzipped) in either category, posts a comment on the PR |
| 66 | + |
| 67 | +### Comment Threshold |
| 68 | + |
| 69 | +A PR comment is posted when: |
| 70 | + |
| 71 | +- First paint assets change by more than ±50 KB (gzipped), OR |
| 72 | +- Offline mode assets change by more than ±50 KB (gzipped) |
| 73 | + |
| 74 | +### Comment Format |
| 75 | + |
| 76 | +The PR comment includes: |
| 77 | + |
| 78 | +- **Size Comparison**: Current vs. base size with delta |
| 79 | +- **File Count**: Number of files in each category |
| 80 | +- **Files with Largest Changes**: Top 10 files with the biggest size deltas |
| 81 | +- **Top 10 Largest Files**: Current largest files in each category |
| 82 | +- **Status Indicators**: |
| 83 | + - 🆕 New file |
| 84 | + - 🗑️ Removed file |
| 85 | + - 📈 Size increased |
| 86 | + - 📉 Size decreased |
| 87 | + - ➡️ No change |
| 88 | + |
| 89 | +## First Paint Assets |
| 90 | + |
| 91 | +Files considered critical for the first paint include: |
| 92 | + |
| 93 | +- `index.html` and `remote.html` |
| 94 | +- Core JavaScript bundles in `/assets/` (excluding optional chunks) |
| 95 | +- Core CSS files |
| 96 | +- Service worker |
| 97 | +- Manifest files |
| 98 | + |
| 99 | +**Excluded from first paint:** |
| 100 | + |
| 101 | +- Optional chunks (e.g., CodeMirror extensions in `/assets/optional/`) |
| 102 | +- PHP WASM files (loaded on demand) |
| 103 | +- WordPress build ZIPs (loaded on demand) |
| 104 | +- SQLite integration (loaded on demand) |
| 105 | +- Demos and builder assets |
| 106 | + |
| 107 | +## Offline Mode Assets |
| 108 | + |
| 109 | +Files required for offline functionality are determined by the `assets-required-for-offline-mode.json` manifest, which is automatically generated during the build process by the `listAssetsRequiredForOfflineMode` Vite plugin. |
| 110 | + |
| 111 | +See `packages/vite-extensions/vite-list-assets-required-for-offline-mode.ts` for details on how this manifest is generated. |
| 112 | + |
| 113 | +## Local Development |
| 114 | + |
| 115 | +To test bundle size changes locally: |
| 116 | + |
| 117 | +```bash |
| 118 | +# Build the website |
| 119 | +npm run build:website |
| 120 | + |
| 121 | +# Analyze current build |
| 122 | +node tools/scripts/analyze-bundle-size.mjs |
| 123 | + |
| 124 | +# Save as base for comparison |
| 125 | +cp bundle-size-report.json bundle-size-report-base.json |
| 126 | + |
| 127 | +# Make your changes... |
| 128 | + |
| 129 | +# Build again |
| 130 | +npm run build:website |
| 131 | + |
| 132 | +# Analyze new build |
| 133 | +node tools/scripts/analyze-bundle-size.mjs |
| 134 | + |
| 135 | +# Compare |
| 136 | +node tools/scripts/compare-bundle-size.mjs |
| 137 | +``` |
| 138 | + |
| 139 | +## Optimization Tips |
| 140 | + |
| 141 | +If your PR triggers a bundle size increase: |
| 142 | + |
| 143 | +1. **Check for new dependencies**: Large libraries can significantly increase bundle size |
| 144 | +2. **Use code splitting**: Move non-critical code to lazy-loaded chunks |
| 145 | +3. **Optimize assets**: Compress images, minify code |
| 146 | +4. **Review bundle composition**: Use tools like `vite-bundle-visualizer` to understand what's taking up space |
| 147 | +5. **Consider alternatives**: Look for lighter-weight alternatives to heavy dependencies |
| 148 | + |
| 149 | +## Artifacts |
| 150 | + |
| 151 | +The workflow uploads the following artifacts for debugging: |
| 152 | + |
| 153 | +- `bundle-size-report.json`: Current branch analysis |
| 154 | +- `bundle-size-report-base.json`: Base branch analysis |
| 155 | +- `bundle-size-comment.md`: Generated PR comment |
0 commit comments