Skip to content

Commit a03b8f6

Browse files
Copilotadamziel
andcommitted
Switch to browser-based bundle size measurement using Playwright
Co-authored-by: adamziel <205419+adamziel@users.noreply.github.com>
1 parent e6fb3d3 commit a03b8f6

File tree

5 files changed

+573
-520
lines changed

5 files changed

+573
-520
lines changed

.github/workflows/bundle-size-check.yml

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,20 @@ jobs:
2323
- name: Build website
2424
run: npx nx build playground-website
2525

26-
- name: Analyze current bundle size
27-
run: node tools/scripts/analyze-bundle-size.mjs
26+
- name: Start preview server
27+
run: |
28+
npx nx preview playground-website &
29+
# Wait for server to be ready
30+
timeout 60 bash -c 'until curl -s http://localhost:5400 > /dev/null; do sleep 1; done'
31+
32+
- name: Install Playwright browsers
33+
run: npx playwright install chromium --with-deps
34+
35+
- name: Measure current bundle size
36+
run: node tools/scripts/measure-bundle-size-browser.mjs
37+
38+
- name: Stop preview server
39+
run: pkill -f "nx preview" || true
2840

2941
- name: Save current report
3042
run: cp bundle-size-report.json bundle-size-report-current.json
@@ -41,8 +53,17 @@ jobs:
4153
- name: Build base branch website
4254
run: npx nx build playground-website
4355

44-
- name: Analyze base bundle size
45-
run: node tools/scripts/analyze-bundle-size.mjs
56+
- name: Start preview server for base branch
57+
run: |
58+
npx nx preview playground-website &
59+
# Wait for server to be ready
60+
timeout 60 bash -c 'until curl -s http://localhost:5400 > /dev/null; do sleep 1; done'
61+
62+
- name: Measure base bundle size
63+
run: node tools/scripts/measure-bundle-size-browser.mjs
64+
65+
- name: Stop preview server
66+
run: pkill -f "nx preview" || true
4667

4768
- name: Save base report
4869
run: cp bundle-size-report.json bundle-size-report-base.json

tools/scripts/README.md

Lines changed: 107 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,161 @@
11
# Bundle Size Tracking
22

3-
This directory contains scripts for tracking and reporting bundle size changes in WordPress Playground.
3+
This directory contains scripts for tracking and reporting bundle size changes in WordPress Playground using real browser measurements.
44

55
## Overview
66

7-
The bundle size tracking system helps ensure that changes to the codebase don't significantly increase the download size required for:
7+
The bundle size tracking system uses Playwright to measure actual download sizes at key stages during page load:
88

9-
1. **First Paint**: Assets needed to display the initial WordPress Playground interface
10-
2. **Offline Mode**: Assets cached for offline functionality
9+
1. **First Paint**: Assets downloaded until the progress bar is visible
10+
2. **WordPress Loaded**: Assets downloaded until WordPress site is ready (nested iframes loaded)
11+
3. **Offline Mode Ready**: All assets downloaded after network activity settles
12+
13+
This approach provides real-world measurements instead of static file analysis.
1114

1215
## Scripts
1316

14-
### `analyze-bundle-size.mjs`
17+
### `measure-bundle-size-browser.mjs`
1518

16-
Analyzes the build output and generates a detailed report of asset sizes.
19+
Uses Playwright to measure bundle size by monitoring actual browser network requests.
1720

1821
**Usage:**
19-
2022
```bash
21-
npm run build:website
22-
node tools/scripts/analyze-bundle-size.mjs
23-
```
23+
# Start the development server
24+
npm run dev
2425

25-
**Output:**
26-
27-
- `bundle-size-report.json`: Detailed JSON report with size information for all assets
26+
# In another terminal, run the measurement
27+
node tools/scripts/measure-bundle-size-browser.mjs
28+
```
2829

2930
**What it measures:**
31+
- Total bytes transferred at each stage
32+
- Number of files loaded
33+
- Time to each milestone
34+
- Top 10 largest files at each stage
35+
- Breakdown by resource type (script, stylesheet, image, etc.)
3036

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
37+
**Output:**
38+
- `bundle-size-report.json`: Detailed JSON report with measurements
3539

3640
### `compare-bundle-size.mjs`
3741

3842
Compares two bundle size reports and generates a markdown report suitable for GitHub PR comments.
3943

4044
**Usage:**
41-
4245
```bash
4346
node tools/scripts/compare-bundle-size.mjs [base-report] [current-report]
4447
```
4548

4649
**Default paths:**
47-
48-
- `base-report`: `bundle-size-report-base.json`
49-
- `current-report`: `bundle-size-report.json`
50+
- `base-report`: `bundle-size-report-base.json`
51+
- `current-report`: `bundle-size-report.json`
5052

5153
**Output:**
52-
53-
- `bundle-size-comment.md`: Markdown-formatted comparison report
54-
- GitHub Actions outputs for workflow automation
54+
- `bundle-size-comment.md`: Markdown-formatted comparison report
55+
- GitHub Actions outputs for workflow automation
5556

5657
## CI Workflow
5758

5859
The bundle size check runs automatically on pull requests via the `.github/workflows/bundle-size-check.yml` workflow.
5960

6061
### How it works
6162

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
63+
1. **Build & Start Current Branch**:
64+
- Builds the website from the PR branch
65+
- Starts the preview server
66+
- Installs Playwright
67+
- Measures bundle size with real browser
6668

67-
### Comment Threshold
69+
2. **Build & Start Base Branch**:
70+
- Checks out and builds the base branch (usually `trunk`)
71+
- Starts the preview server
72+
- Measures its bundle size with real browser
6873

69-
A PR comment is posted when:
74+
3. **Compare**:
75+
- Generates a comparison report showing size changes at each stage
76+
- Includes time delta as well as size delta
7077

71-
- First paint assets change by more than ±50 KB (gzipped), OR
72-
- Offline mode assets change by more than ±50 KB (gzipped)
78+
4. **Comment**:
79+
- If any stage changes by more than 50 KB, posts a comment on the PR
80+
81+
### Comment Threshold
82+
83+
A PR comment is posted when any of these change by more than ±50 KB:
84+
- First paint downloads
85+
- WordPress loaded downloads
86+
- Offline mode ready downloads
7387

7488
### Comment Format
7589

76-
The PR comment includes:
90+
The PR comment includes three sections:
91+
92+
#### 🎨 First Paint (Progress Bar Visible)
93+
- Current vs. base size and load time
94+
- Delta in bytes and time
95+
- Top 10 largest files
96+
97+
#### ✅ WordPress Loaded (Site Ready)
98+
- Current vs. base size and load time
99+
- Delta in bytes and time
100+
- Top 10 largest files
101+
102+
#### 💾 Offline Mode Ready (All Downloads Settled)
103+
- Current vs. base size and load time
104+
- Delta in bytes and time
105+
- Top 10 largest files
77106

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
107+
**Status Indicators**:
108+
- 📈 Size increased
109+
- 📉 Size decreased
110+
- ➡️ No change
88111

89-
## First Paint Assets
112+
## Measurement Stages
90113

91-
Files considered critical for the first paint include:
114+
### First Paint (Progress Bar Visible)
92115

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
116+
Measures all downloads until the progress bar becomes visible. This represents the minimum assets needed for users to see that the page is loading.
98117

99-
**Excluded from first paint:**
118+
**Key signals:**
119+
- Progress bar element visible
120+
- Falls back to DOMContentLoaded if no progress bar found
100121

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
122+
### WordPress Loaded (Site Ready)
106123

107-
## Offline Mode Assets
124+
Measures all downloads until WordPress is fully loaded in the nested iframe, indicating the site is interactive and ready to use.
108125

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.
126+
**Key signals:**
127+
- WordPress iframe body element is attached
128+
- Falls back to window load event if iframe not found
110129

111-
See `packages/vite-extensions/vite-list-assets-required-for-offline-mode.ts` for details on how this manifest is generated.
130+
### Offline Mode Ready (All Downloads Settled)
131+
132+
Measures all downloads after network activity settles (5 seconds of no new requests). This represents all assets that would be cached for offline use.
133+
134+
**Key signals:**
135+
- No network requests for 5 consecutive seconds
136+
- Includes all lazy-loaded assets
112137

113138
## Local Development
114139

115140
To test bundle size changes locally:
116141

117142
```bash
118-
# Build the website
119-
npm run build:website
143+
# Terminal 1: Start the dev server
144+
npm run dev
120145

121-
# Analyze current build
122-
node tools/scripts/analyze-bundle-size.mjs
146+
# Terminal 2: Measure current build
147+
node tools/scripts/measure-bundle-size-browser.mjs
123148

124149
# Save as base for comparison
125150
cp bundle-size-report.json bundle-size-report-base.json
126151

127152
# Make your changes...
128153

129-
# Build again
130-
npm run build:website
154+
# Restart dev server if needed
155+
npm run dev
131156

132-
# Analyze new build
133-
node tools/scripts/analyze-bundle-size.mjs
157+
# Measure new build
158+
node tools/scripts/measure-bundle-size-browser.mjs
134159

135160
# Compare
136161
node tools/scripts/compare-bundle-size.mjs
@@ -143,13 +168,24 @@ If your PR triggers a bundle size increase:
143168
1. **Check for new dependencies**: Large libraries can significantly increase bundle size
144169
2. **Use code splitting**: Move non-critical code to lazy-loaded chunks
145170
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
171+
4. **Review network tab**: Use browser DevTools to see what's being loaded
147172
5. **Consider alternatives**: Look for lighter-weight alternatives to heavy dependencies
173+
6. **Analyze resource types**: Check if images, scripts, or styles are the main contributor
174+
175+
## Browser-Based Measurement Benefits
176+
177+
Using real browser measurements instead of static file analysis provides:
178+
179+
- **Realistic data**: Measures what users actually download
180+
- **Network behavior**: Captures caching, compression, and HTTP/2 multiplexing effects
181+
- **Load timing**: Shows when assets are downloaded relative to page milestones
182+
- **Resource prioritization**: Reflects browser's actual loading strategy
183+
- **Accurate offline assets**: Measures what's actually cached, not estimates
148184

149185
## Artifacts
150186

151187
The workflow uploads the following artifacts for debugging:
152188

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
189+
- `bundle-size-report.json`: Current branch measurements
190+
- `bundle-size-report-base.json`: Base branch measurements
191+
- `bundle-size-comment.md`: Generated PR comment

0 commit comments

Comments
 (0)