-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(scripts): add a client-bundle measurement so regressions are visible #6135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+124
−0
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /** | ||
| * Reports the size of the client JavaScript a production build ships, so a | ||
| * bundle regression is something CI can see rather than something a user | ||
| * reports as a slow page. | ||
| * | ||
| * Nothing else measures this today. Turbopack's route table prints only | ||
| * `Revalidate`/`Expire` — no `First Load JS` — and it emits flat, | ||
| * content-hashed chunk names with no `app-build-manifest.json`, so there is no | ||
| * route -> chunk mapping to attribute bytes with. `@next/bundle-analyzer` is a | ||
| * webpack plugin and is not installed. This measures what is reliably | ||
| * available: the total client payload, plus the largest chunks so a jump can be | ||
| * traced to a culprit. | ||
| * | ||
| * Deliberately not per-route. A per-route number would need either a | ||
| * route->chunk manifest Turbopack does not emit, or a browser loading each | ||
| * route; inventing an attribution from flat chunk names would produce a | ||
| * confident number that is wrong. | ||
| * | ||
| * Usage: | ||
| * bun run scripts/measure-client-bundle.ts # human-readable | ||
| * bun run scripts/measure-client-bundle.ts --json # machine-readable | ||
| * bun run scripts/measure-client-bundle.ts --baseline b.json # compare, fail on regression | ||
| * bun run scripts/measure-client-bundle.ts --json > baseline.json | ||
| */ | ||
|
|
||
| import fs from 'fs' | ||
| import path from 'path' | ||
| import { fileURLToPath } from 'url' | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url) | ||
| const rootDir = path.resolve(path.dirname(__filename), '..') | ||
| const chunkDir = path.join(rootDir, 'apps/sim/.next/static/chunks') | ||
|
|
||
| /** Fraction a total may grow past the baseline before `--baseline` fails. */ | ||
| const REGRESSION_TOLERANCE = 0.02 | ||
|
|
||
| interface Chunk { | ||
| file: string | ||
| bytes: number | ||
| } | ||
|
|
||
| interface Report { | ||
| totalBytes: number | ||
| chunkCount: number | ||
| largest: Chunk[] | ||
| } | ||
|
|
||
| function walk(dir: string, acc: Chunk[] = []): Chunk[] { | ||
| for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { | ||
| const full = path.join(dir, entry.name) | ||
| if (entry.isDirectory()) { | ||
| walk(full, acc) | ||
| continue | ||
| } | ||
| // `.map` files are excluded: they are never requested during page load, so | ||
| // counting them would let a source-map change masquerade as a bundle | ||
| // regression. | ||
| if (entry.isFile() && entry.name.endsWith('.js')) { | ||
| acc.push({ file: path.relative(chunkDir, full), bytes: fs.statSync(full).size }) | ||
| } | ||
| } | ||
| return acc | ||
| } | ||
|
|
||
| function measure(): Report { | ||
| if (!fs.existsSync(chunkDir)) { | ||
| throw new Error( | ||
| `No build found at ${path.relative(rootDir, chunkDir)}. Run \`bun run build\` in apps/sim first.` | ||
| ) | ||
| } | ||
| const chunks = walk(chunkDir) | ||
| if (chunks.length === 0) { | ||
| throw new Error(`No .js chunks under ${path.relative(rootDir, chunkDir)} — build looks empty.`) | ||
| } | ||
| return { | ||
| totalBytes: chunks.reduce((sum, c) => sum + c.bytes, 0), | ||
| chunkCount: chunks.length, | ||
| largest: [...chunks].sort((a, b) => b.bytes - a.bytes).slice(0, 15), | ||
| } | ||
| } | ||
|
|
||
| const mb = (bytes: number) => `${(bytes / 1024 / 1024).toFixed(2)} MB` | ||
|
|
||
| function main(): void { | ||
| const args = process.argv.slice(2) | ||
| const report = measure() | ||
|
|
||
| if (args.includes('--json')) { | ||
| process.stdout.write(`${JSON.stringify(report, null, 2)}\n`) | ||
| return | ||
| } | ||
|
Comment on lines
+88
to
+91
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| console.log(`Client JS: ${mb(report.totalBytes)} across ${report.chunkCount} chunks\n`) | ||
| console.log('Largest chunks:') | ||
| for (const c of report.largest) { | ||
| console.log(` ${mb(c.bytes).padStart(9)} ${c.file}`) | ||
| } | ||
|
|
||
| const baselineIdx = args.indexOf('--baseline') | ||
| if (baselineIdx === -1) return | ||
|
|
||
| const baselinePath = args[baselineIdx + 1] | ||
| if (!baselinePath) { | ||
| console.error('\n--baseline requires a path to a JSON report') | ||
| process.exit(1) | ||
| } | ||
| const baseline: Report = JSON.parse(fs.readFileSync(baselinePath, 'utf8')) | ||
| const delta = report.totalBytes - baseline.totalBytes | ||
| const pct = (delta / baseline.totalBytes) * 100 | ||
| const sign = delta >= 0 ? '+' : '' | ||
| console.log( | ||
| `\nvs baseline: ${mb(baseline.totalBytes)} -> ${mb(report.totalBytes)} (${sign}${pct.toFixed(1)}%)` | ||
| ) | ||
|
|
||
| if (delta > baseline.totalBytes * REGRESSION_TOLERANCE) { | ||
| console.error( | ||
| `\nClient JS grew more than ${(REGRESSION_TOLERANCE * 100).toFixed(0)}% over the baseline.` | ||
| ) | ||
| process.exit(1) | ||
| } | ||
| } | ||
|
|
||
| main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Misses immutable chunk directory
Medium Severity
chunkDiris hardcoded toapps/sim/.next/static/chunks, but the current build also emits a parallelstatic/immutable/chunkstree (includingapp/,pages/, andturbopack/). Client JS under that prefix is never counted, so totals and the baseline gate can miss real payload growth.Additional Locations (1)
scripts/measure-client-bundle.ts#L47-L63Reviewed by Cursor Bugbot for commit 32afd8d. Configure here.