Repository navigation
chore(examples): add bundle analysis and blocking size budgets - #1145
Conversation
Every bundled example can now inspect what ends up in its bundle: Rsdoctor for the webpack ones, vite-bundle-analyzer for the Vite ones. Both are opt-in through 'npm run build:analyze' and never affect the regular build. The production build now fails when a bundle grows, with webpack 'performance' budgets and, for Vite, a shared plugin, since chunkSizeWarningLimit only logs a warning and lets the build succeed. Each limit is the smallest value that passes, the current size rounded up to the next kB, so the budgets track the size evolution. The convention is recorded in .claude/rules/tooling/bundle-size-budgets.md. scripts/build-all-examples.bash gains --fail-at-end, used by the CI: a single run shows every example, the size table, then the failures. Dependabot groups the vite and the rsdoctor updates, so those libraries stay consistent across the examples.
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughExample packages now support production bundle analysis. Webpack and Vite builds enforce configured size budgets. Shared utilities centralize analysis, chunk splitting, and failure handling. Example builds can continue after failures and report them at the end. ChangesBundle analysis and build enforcement
Estimated code review effort: 4 (Complex) | ~45 minutes Merge Risk: 🔵 Low · up to The bundle-size guard can also fail development-mode builds, causing unexpected build failures for maintainers even though budgets are intended for production builds. This is a bounded configuration risk and the PR is otherwise mergeable with explicit owner follow-up. Sequence Diagram(s)sequenceDiagram
participant BuildCommand
participant ExampleConfig
participant Analyzer
participant SizeGuard
BuildCommand->>ExampleConfig: run production build
alt ANALYZE=true
ExampleConfig->>Analyzer: enable bundle analysis
Analyzer-->>BuildCommand: generate local bundle report
else standard production build
ExampleConfig->>SizeGuard: enable configured size limit
SizeGuard-->>BuildCommand: fail oversized output
end
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: f189e624-c711-4fb2-9850-e68842af414d
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (25)
.claude/rules/tooling/bundle-size-budgets.md.github/dependabot.yml.github/workflows/_reusable_build_examples.ymlCLAUDE.mdpackages/js-example-selected-features/README.mdpackages/js-example-selected-features/package.jsonpackages/js-example-selected-features/webpack.config.jspackages/js-example-without-defaults/README.mdpackages/js-example-without-defaults/package.jsonpackages/js-example-without-defaults/webpack.config.jspackages/js-example/README.mdpackages/js-example/package.jsonpackages/js-example/webpack.config.jspackages/ts-example-selected-features/README.mdpackages/ts-example-selected-features/package.jsonpackages/ts-example-selected-features/vite.config.jspackages/ts-example-without-defaults/README.mdpackages/ts-example-without-defaults/package.jsonpackages/ts-example-without-defaults/vite.config.jspackages/ts-example/README.mdpackages/ts-example/package.jsonpackages/ts-example/vite.config.jsscripts/build-all-examples.bashscripts/vite/chunk-size-limit.mjsscripts/vite/maxgraph-chunk.mjs
Included review availability: Your plan includes up to 4 reviews per rolling hour; 3 remain after this review.
… runs 'build:analyze' builds in production mode, so the performance hints stayed set to 'error' and an oversized bundle made the compilation report a size error during a run whose only purpose is to explain that very size. The report itself was never lost, Rsdoctor still wrote its payload and served it, but the run reported a failure and would have exited non-zero without the report server holding the process open. The Vite examples already skip their guard when analyzing, so this also aligns the two bundlers. 'npm run build', which the CI runs, remains the command that enforces the budget. Verified with a budget forced down to 10 kB: the analyze run now reports no size error and still serves its report, while the plain build still exits 1.
The ESLint configuration declares no Node environment for the JavaScript configuration files, so 'process.env.ANALYZE' was reported as no-undef. Importing process from 'node:process' makes the dependency explicit, which is also what the shared chunk size plugin does for Buffer. Note that 'npm run lint' only covers '**/*.ts', so these files are outside its scope and the error only surfaces in tooling that lints them directly.
The Rsdoctor registration, the analyze-only source map and the performance budget were copy pasted in the three webpack configurations, about fifteen identical lines each. SonarQube reported 9.2% duplication on new code, above the 3% gate, and the same line had to be fixed three times twice during the review of this pull request. Move that wiring next to the Vite equivalent, in scripts/webpack/bundle-analysis.cjs, and wrap each example configuration with it. The three configurations now differ by exactly one line, their budget. The Vite examples already scored no duplication for the same reason: their shared parts live in scripts/vite. The configuration object is named rather than nested inside the call, so its body keeps its indentation and stays out of the diff. Only the analysis and the budget move. Verified as a pure refactoring: the three bundles keep the same size to the byte and the same content hash, an analyze run still registers Rsdoctor, emits the hidden source map, reports no size error and serves its report, and the plain build still enforces the budget.
The flat configuration parses every .js file as an ES module and declares no Node environment outside packages/core/src, so each webpack configuration reported eight no-undef errors for 'require', 'module', 'process' and '__dirname', plus one no-require-imports error per import. The new shared module added five more. Declare those globals for '**/webpack.config.js' and 'scripts/**/*.cjs' and allow require there. This is what the review of this pull request asked for, and it clears the pre-existing errors as well. Nothing in the CI caught them, since 'npm run lint' only globs '**/*.ts', so they only surfaced when linting the files directly.
Reverts 86fb457. The lint script only globs '**/*.ts', and a flat configuration 'files' block narrows which rules apply to a file that ESLint was already given, it never widens the file set. So that block matched nothing during 'npm run lint' and no automated check in this repository would ever have exercised it. Its only effect was on editors and on external tools that lint these files directly. Making it meaningful would mean widening the lint script, which is a separate change: 57 JavaScript files report 615 problems today, 497 of them in the generated packages/website/.docusaurus files that the ignore list does not cover, and 118 in hand written files. Restore the explicit 'require' of node:process in the shared module, so dropping the declared globals does not reintroduce the 'process is not defined' report that this pull request was reviewed for. What remains there are the 'require' and 'module' reports inherent to a CommonJS file, exactly what every webpack configuration of the examples already had before this pull request.
Extracting the webpack wiring into scripts/webpack/bundle-analysis.cjs left the documentation describing a shape that no longer exists: a 'performance' section inside each example configuration. Update the rule to say where each limit is declared and what enforces it on each side, replace the code samples with the call the examples now make, and describe the exemptions as they are, no enforcement during a development build nor during an analyze run, so 'hints' reads 'isDevMode || isBundleAnalysisEnabled ? false : error'. Also record that the Vite guard is registered instead of the analyzer and never alongside it, since the analyzer is a post plugin and the guard would abort the build before any report is written. Point the README of the three webpack examples at the limits passed to withBundleAnalysisAndSizeBudget rather than at a 'performance' key they no longer contain.
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 006f36bb-4862-4494-bcd7-1cf3a2690842
📒 Files selected for processing (4)
.claude/rules/tooling/bundle-size-budgets.mdpackages/js-example-selected-features/README.mdpackages/js-example-without-defaults/README.mdpackages/js-example/README.md
🚧 Files skipped from review as they are similar to previous changes (3)
- packages/js-example-without-defaults/README.md
- packages/js-example/README.md
- packages/js-example-selected-features/README.md
Included review availability: Your plan provides up to 4 included reviews per hour; 2 remain after this review.
| `npm run build` is the command that enforces a budget, and it is the one the CI runs. Neither bundler enforces anything | ||
| during a development build, since those bundles are not minified and are several times larger, nor during an analyze | ||
| run, which is a diagnostic: failing it would suppress the very report needed to understand the size. |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
fd --type f 'vite.config.js' packages |
xargs rg -n -C 8 'failOnChunkSizeExceeded|apply:|generateBundle|closeBundle|mode'
rg -n -C 12 'failOnChunkSizeExceeded|apply:|generateBundle|closeBundle|command' scripts/viteRepository: maxGraph/maxGraph
Length of output: 13188
🏁 Script executed:
#!/bin/bash
set -euo pipefail
printf '%s\n' '--- package scripts ---'
fd --type f 'package.json' packages |
xargs rg -n -C 6 '"(build|build:analyze|dev|preview)"'
printf '%s\n' '--- documented commands and modes ---'
rg -n -C 8 'npm run build|build:analyze|development|production|ANALYZE|chunkSizeWarningLimit' \
.claude/rules/tooling/bundle-size-budgets.md packages scriptsRepository: maxGraph/maxGraph
Length of output: 50374
Restrict the Vite size check to production builds.
npm run dev uses Vite’s serve command and does not run generateBundle, but vite build --mode development still registers failOnChunkSizeExceeded because apply: 'build' is independent of mode. Register the plugin only for production builds, or narrow the documentation to dev-server runs.
There was a problem hiding this comment.
Partly right, and it caught a real error in the documentation, thanks.
You are correct that apply: 'build' is independent of mode, so vite build --mode development does register failOnChunkSizeExceeded. The rule text was wrong to claim that neither bundler enforces anything "during a development build": that holds for webpack, whose hints are disabled unless --mode=production is passed, but not for vite.
The suggested code change is not needed though, because that build is checked against the right thing. build.minify resolves to 'oxc' for the client consumer whatever the mode is (minify: consumer === "server" || isBundledDev ? false : "oxc" in vite 8.2.1), so a development-mode build emits the same minified chunks. Verified in packages/ts-example-without-defaults:
$ ./node_modules/.bin/vite build --mode development --base ./
dist/assets/maxgraph-91pZLIgH.js 220.85 kB | gzip: 61.68 kB
built in 212ms, exit 0
220 859 B with the same content hash as the production build, so the guard measures the same bundle and passes. Gating the plugin on the mode would therefore guard a case that cannot misfire, and no script in the repository runs vite build --mode development in the first place.
So I took the second option you offered and fixed the wording instead, in 1f2bf36: the section now states how each bundler reaches the exemption, webpack through isDevMode and vite through the dev server emitting no bundle at all, and says explicitly that a --mode development build is checked as well.
The README of the three Vite examples described the limit as applying to the `maxgraph` chunk, while the shared plugin compares it against every emitted chunk. The difference is invisible today, the other chunks weigh under 3 kB, but the error message would name whichever chunk crossed the limit and would then contradict the documentation. Say what is checked, and keep the useful part of the previous wording: the `maxgraph` chunk is the one that sets the value, being by far the largest.
The rule claimed that neither bundler enforces a budget during a development build. That is accurate for webpack, which disables `hints` unless `--mode=production` is passed, but not for vite: `apply: 'build'` is independent of the mode, so `vite build --mode development` runs the guard too. It still measures the right thing, since `build.minify` stays on whatever the mode says, and such a build was verified to emit a chunk byte identical to the production one, so the plugin needs no mode condition. Spell that out instead of stating an exemption vite does not have. Also soften the claim that sharing one constant keeps the warning and the error from drifting apart: it guarantees that both thresholds hold the same value, nothing about how each side measures a chunk.
|



Why
The bundled example packages had no way to tell what ends up in their bundle, and no guard against it growing.
The Vite examples declared a
chunkSizeWarningLimit, but it only makes Vite log a warning and the build still exits 0, so a bundle regression showed up as a single line in a CI log that nobody reads (upstream request: vitejs/vite#18496). The webpack examples had no budget at all.What
Bundle analysis, opt-in, in the six bundled examples. Rsdoctor for the three webpack examples, vite-bundle-analyzer for the three Vite ones, since Rsdoctor supports only webpack and Rspack. Both are enabled by a dedicated
npm run build:analyzescript, which setsANALYZE=truethroughcross-envso the command also works in Windows shells. The regularnpm run buildis untouched: no analyzer, no report server, no source map. A source map is generated for the analyze run only,hidden-source-mapfor webpack andsourcemap: 'hidden'for Vite, so the analyzed bundle stays byte for byte the one the regular build produces, content hash included; Rsdoctor needs it to attribute bytes to individual modules.The production build now fails when a bundle grows. The webpack examples get a
performancebudget withhints: 'error'. Vite cannot do this on its own, so the examples pass their limit to a shared plugin,scripts/vite/chunk-size-limit.mjs, which fails the build naming the chunk and both sizes. Each limit is declared once per example and feeds bothchunkSizeWarningLimitand the plugin, so the warning and the error cannot drift apart.Each value is the smallest one that passes, that is the current size rounded up to the next kB. The intent is to follow the size evolution rather than to leave room to grow into, so the incidental headroom is accepted; the convention is recorded in
.claude/rules/tooling/bundle-size-budgets.md.Nothing is enforced during a development build or an analyze run. Development bundles are not minified, and an analyze run is a diagnostic: failing it would suppress the very report needed to understand the size. On the Vite side the guard and the analyzer are never registered together, because
vite-bundle-analyzeris a post plugin, so the guard's error would abort the build before any report was written.--fail-at-endinscripts/build-all-examples.bash, used by the CI. Once a build can fail on size, the first failure would abort the script and hide both the other examples and the size table, so getting the full picture would take one CI run per failing example. With the option, every example is built, the file listing, the markdown table and the CSV are still printed, and the failures are reported after them. The default behavior is unchanged: without the option the script still stops at the first failing build.What the examples share lives in
scripts/.scripts/vite/maxgraph-chunk.mjsholds thecodeSplittinggroup that isolates@maxgraph/corein a dedicatedmaxgraphchunk, andscripts/webpack/bundle-analysis.cjsholds the Rsdoctor registration, the analyze-only source map and theperformanceblock. Each family of configurations is therefore identical apart from its calibrated limit: the threevite.config.jsdiffer by one line, and so do the threewebpack.config.js.Dependabot. New
vitegroup forviteandvite-*, and@rsdoctor/*added to the existingwebpackgroup, so those libraries move together across the examples.@storybook/html-viteis deliberately left in thestorybookgroup, which owns the Vite version Storybook builds against.Sizes and budgets
No bundle size changed in this PR. The
js-example-without-defaultsbudget was initially calibrated against a stalepackages/core/lib; rebuilding core brings that bundle to 239,112 B, and the budget follows.Decisions worth flagging
--list-size-only --fail-at-endis a documented no-op, not an error: nothing is built, so nothing can fail, and a caller passing flags generically should not be rejected for a harmless combination.if: ${{ !cancelled() }}. Skipping it on failure would defeat the purpose of--fail-at-end, since thedistof the examples that did build is what one wants to inspect. This does not weaken the gate: the job still fails, so the jobs depending on it, the release included, do not run.dist, so a failing example disappears from the size table and the CSV instead of showing a stale number.../../scripts/, so an example folder is no longer standalone. That was accepted to keep the six configurations in sync and to avoid fixing the same line three times, which happened twice during the review of this PR.Validation
./scripts/build-all-examples.bashand./scripts/build-all-examples.bash --fail-at-end: exit 0, the six examples below their limits, no webpack performance warning and no Vite chunk warning. Green on ubuntu-24.04, macos-14 and windows-2022.RolldownError: Chunk "assets/maxgraph-Be5ReXNy.js" is 361.55 kB, above the 10 kB limit.and exit 1.--fail-at-endproven: with two limits lowered, both failures are listed after the size table, the third example still builds, exit code 1.npm run lintclean. Note it only covers**/*.ts, so the configuration files and the two newscripts/modules are outside its scope; they were linted explicitly.Summary by CodeRabbit
New Features
maxgraphchunking and configurable production bundle-size limits.Bug Fixes
Documentation