Skip to content

chore(examples): add bundle analysis and blocking size budgets - #1145

Merged
redfish4ktc merged 11 commits into
mainfrom
chore/improve_build_of_examples
Aug 21, 2026
Merged

redfish4ktc merged 11 commits into
mainfrom
chore/improve_build_of_examples

Conversation

@redfish4ktc

@redfish4ktc redfish4ktc commented Aug 18, 2026 •

Copy link
Copy Markdown
Member

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:analyze script, which sets ANALYZE=true through cross-env so the command also works in Windows shells. The regular npm run build is untouched: no analyzer, no report server, no source map. A source map is generated for the analyze run only, hidden-source-map for webpack and sourcemap: '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 performance budget with hints: '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 both chunkSizeWarningLimit and 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-analyzer is a post plugin, so the guard's error would abort the build before any report was written.

--fail-at-end in scripts/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.mjs holds the codeSplitting group that isolates @maxgraph/core in a dedicated maxgraph chunk, and scripts/webpack/bundle-analysis.cjs holds the Rsdoctor registration, the analyze-only source map and the performance block. Each family of configurations is therefore identical apart from its calibrated limit: the three vite.config.js differ by one line, and so do the three webpack.config.js.

Dependabot. New vite group for vite and vite-*, and @rsdoctor/* added to the existing webpack group, so those libraries move together across the examples. @storybook/html-vite is deliberately left in the storybook group, which owns the Vite version Storybook builds against.

Sizes and budgets

Example current budget
js-example 466,905 B asset, 472,414 B entrypoint 467,000 / 473,000
js-example-selected-features 384,964 B asset, 390,473 B entrypoint 385,000 / 391,000
js-example-without-defaults 239,112 B asset, 240,095 B entrypoint 240,000 / 241,000
ts-example 428,747 B chunk 429 kB
ts-example-selected-features 361,547 B chunk 362 kB
ts-example-without-defaults 220,859 B chunk 221 kB

No bundle size changed in this PR. The js-example-without-defaults budget was initially calibrated against a stale packages/core/lib; rebuilding core brings that bundle to 239,112 B, and the budget follows.

Decisions worth flagging

  • --list-size-only --fail-at-end is 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.
  • The artifact upload runs with if: ${{ !cancelled() }}. Skipping it on failure would defeat the purpose of --fail-at-end, since the dist of 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.
  • A failed Vite build leaves no dist, so a failing example disappears from the size table and the CSV instead of showing a stale number.
  • Budgets are tight by design, between 36 B and about 900 B of headroom. A bundler patch bump can therefore turn a build red; the fix is to check the increase is intended and update the value in the same commit.
  • The shared modules are build tooling, not teaching material. They make the example configurations reference ../../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.bash and ./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.
  • Guard proven blocking: with a limit lowered to 10 kB, RolldownError: Chunk "assets/maxgraph-Be5ReXNy.js" is 361.55 kB, above the 10 kB limit. and exit 1.
  • --fail-at-end proven: with two limits lowered, both failures are listed after the size table, the third example still builds, exit code 1.
  • Analyze runs verified on both bundlers: report reachable over HTTP, no size error reported, and the emitted bundles unchanged to the byte with identical content hashes.
  • Extracting the shared modules verified as pure refactoring: same chunk sizes and same content hashes before and after, for the six examples.
  • npm run lint clean. Note it only covers **/*.ts, so the configuration files and the two new scripts/ modules are outside its scope; they were linted explicitly.

Summary by CodeRabbit

  • New Features

    • Added bundle-analysis commands for JavaScript and TypeScript examples.
    • Added dedicated maxgraph chunking and configurable production bundle-size limits.
    • Oversized production bundles now fail builds with clear size details.
  • Bug Fixes

    • Example builds now continue after individual failures, upload successful results, and provide a final failure summary.
  • Documentation

    • Expanded guidance for analyzing bundles, interpreting reports, and updating limits after intentional size increases.

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.
@redfish4ktc redfish4ktc added the chore Build, CI/CD or repository tasks (issues/PR maintenance, environments, ...) label Aug 18, 2026
@coderabbitai

coderabbitai Bot commented Aug 18, 2026 •

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It 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 reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Example 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.

Changes

Bundle analysis and build enforcement

Layer / File(s) Summary
Shared Vite chunk utilities
scripts/vite/*
Added Vite chunk-size enforcement and @maxgraph/core code-splitting utilities.
Webpack analysis and production budgets
scripts/webpack/*, packages/js-example*/webpack.config.js, packages/js-example*/package.json, packages/js-example*/README.md
Added shared Rsdoctor integration, production source-map handling, Webpack asset and entrypoint budgets, analysis scripts, and documentation.
Vite example integration
packages/ts-example*/vite.config.js, packages/ts-example*/package.json, packages/ts-example*/README.md
Added production-only analyzer commands, shared maxGraph chunk splitting, chunk-size enforcement, hidden source maps for analysis, and documentation.
Build workflow and budget documentation
scripts/build-all-examples.bash, .github/workflows/_reusable_build_examples.yml, .claude/rules/tooling/bundle-size-budgets.md, CLAUDE.md, .github/dependabot.yml
Added fail-at-end build handling, failed-build reporting, Bash 3-compatible iteration, bundle-budget guidance, documentation links, and dependency grouping changes.

Estimated code review effort: 4 (Complex) | ~45 minutes

Merge Risk: 🔵 Low · up to f71a2

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
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change and follows the Conventional Commits format.
Description check ✅ Passed The description clearly explains the motivation, implementation, scope, validation, and bundle budgets, with only minor template sections omitted.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 255ad00 and 68c733a.

⛔ Files ignored due to path filters (1)
  • package-lock.json is 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.yml
  • CLAUDE.md
  • packages/js-example-selected-features/README.md
  • packages/js-example-selected-features/package.json
  • packages/js-example-selected-features/webpack.config.js
  • packages/js-example-without-defaults/README.md
  • packages/js-example-without-defaults/package.json
  • packages/js-example-without-defaults/webpack.config.js
  • packages/js-example/README.md
  • packages/js-example/package.json
  • packages/js-example/webpack.config.js
  • packages/ts-example-selected-features/README.md
  • packages/ts-example-selected-features/package.json
  • packages/ts-example-selected-features/vite.config.js
  • packages/ts-example-without-defaults/README.md
  • packages/ts-example-without-defaults/package.json
  • packages/ts-example-without-defaults/vite.config.js
  • packages/ts-example/README.md
  • packages/ts-example/package.json
  • packages/ts-example/vite.config.js
  • scripts/build-all-examples.bash
  • scripts/vite/chunk-size-limit.mjs
  • scripts/vite/maxgraph-chunk.mjs

Included review availability: Your plan includes up to 4 reviews per rolling hour; 3 remain after this review.

redfish4ktc and others added 8 commits August 18, 2026 15:26
… 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between e241ae8 and f71a2fd.

📒 Files selected for processing (4)
  • .claude/rules/tooling/bundle-size-budgets.md
  • packages/js-example-selected-features/README.md
  • packages/js-example-without-defaults/README.md
  • packages/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.

Comment on lines +65 to +67
`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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 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/vite

Repository: 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 scripts

Repository: 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@sonarqubecloud

Copy link
Copy Markdown

@redfish4ktc
redfish4ktc merged commit 95689c7 into main Aug 21, 2026
13 checks passed
@redfish4ktc
redfish4ktc deleted the chore/improve_build_of_examples branch August 21, 2026 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore Build, CI/CD or repository tasks (issues/PR maintenance, environments, ...)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant