All jobs

Add a Bundle Size Check to a Vite React App

Yes. An agent reads your Vite config and CI, measures the production build, and adds a size-limit check that fails a pull request when the bundle grows past budget.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads the build before it budgets it. In an app like the apps/react-vite workspace of alan2207/bulletproof-react it opens package.json and finds "build": "tsc && vite build --base=/", Vite 5, React 18, Vitest 2 and Playwright, with yarn.lock as the lockfile and a README that asks for Node 20 and Yarn 1.22. It reads vite.config.ts, where build.rollupOptions.output.experimentalMinChunkSize is set to 3500 and fs/promises is marked external, and .gitignore, which already ignores /dist, so the check has to run against a fresh build in CI rather than a committed one.

Then it reads .github/workflows/react-vite-ci.yml. The all-cli-checks job sets working-directory: ./apps/react-vite, moves .env.example to .env, runs yarn install, yarn build, yarn test, yarn lint and yarn check-types; a second e2e job installs Playwright and runs yarn test-e2e. The build already happens in the first job, so the agent adds the size check as a step after yarn build there instead of adding a third job that builds again.

In its sandbox it runs yarn install and yarn build with the .env.example values and reads what lands in dist/assets: the entry chunk, the vendor chunk Rollup splits out, the route chunks from src/app/routes, and the CSS file. It installs size-limit with @size-limit/file as dev dependencies, adds a size-limit array to package.json with one entry for the JavaScript chunks under dist/assets and one for the CSS, each with a gzip limit set a little above what it measured, and a "size": "size-limit" script. It does not touch experimentalMinChunkSize or the externals, since the check is meant to watch the output of the config the team already chose.

Then it adds a Check bundle size step running yarn size after yarn build in react-vite-ci.yml, and when the prompt asks for a comment on each pull request it uses andresz1/size-limit-action with the repository's GITHUB_TOKEN so the diff against the base branch appears on the pull request. It runs yarn size once more in the sandbox to see the budgets pass on the current build, pushes the branch, opens the pull request and reads the React Vite CI run on it, fixing the step if the Yarn working directory or the glob for dist/assets does not match.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add a bundle size check to the Vite React app in this repository. Read package.json, vite.config.ts and the existing GitHub Actions workflow first and do not change the build config. Run the production build in your sandbox and measure the JavaScript and CSS under dist/assets. Add size-limit with @size-limit/file, a size-limit section in package.json with gzip budgets a little above what you measured, and a size script. Add a step after the build in the existing CI job that runs the check and fails the job when a budget is exceeded. Put the measured sizes and the budgets you chose in the pull request description. Open a pull request and make sure the workflow passes on it; if it fails, fix it and push again.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request on a new branch that changes package.json and yarn.lock in the app workspace, adding size-limit and @size-limit/file, a size-limit configuration and a size script, and changes the CI workflow to run that script after the build. In a repository shaped like bulletproof-react the step lands in the all-cli-checks job of react-vite-ci.yml, under the same working-directory, so the budgets are checked against the same build the tests already run on. The description lists each measured size and the budget set for it.

The proof is the workflow run on the pull request: the new step prints each file's gzip size next to its limit and passes. When the size-limit action was requested, its comment on the pull request shows the same figures against the base branch. The session keeps every yarn build and yarn size command the agent ran in its sandbox with its output, so the measurements in the description can be checked.

Before merging, a reviewer checks that the budgets leave room the team is comfortable with, that the file globs still match after the next vite build renames hashed chunks, and whether they want the check as a required status. From then on, a pull request that pulls in a heavy dependency fails this step and shows exactly which chunk grew.

Run it on every pull request that changes dependencies

Once the check is merged, every pull request runs it on its own. To have an agent explain a failure instead of leaving a red step, add an automation in the Project with a github trigger on pull_request.opened and pull_request.synchronize and a prompt that asks it to read the size step's log, find the import that grew the bundle and post one review comment naming it. The trigger adds the pull request event after the prompt, so no placeholder is needed.

To add the check to other Vite apps in the organization, have a script send this same request once per repository with its URL in resources and a fresh UUID as request_id. Each run has its own sandbox and its own pull request, and Rush POSTs run.completed or run.failed to the webhook URL set on the key when each one's turn finishes, with session_url so your server can post the pull request link.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

Choosing budgets by hand means building, reading dist/assets, guessing a margin, pushing and waiting for CI to tell you the glob missed a file. On Rush the agent does the build in its own sandbox, measures real gzip sizes, sets budgets from them and runs the check there before the first push, then reads the workflow run on its own pull request and fixes the step itself. Your laptop and your working copy are not involved.

The result is checkable rather than claimed. The size step runs on the agent's own pull request and prints each file against its budget, and the trajectory keeps the sandbox build the budgets were chosen from. Because each run is independent, the same request can add a size check to every front-end repository at once, each behind a pull request a person reviews before it becomes a required check.

Frequently asked questions

Will it change my Vite build settings?

No. The prompt tells it to leave vite.config.ts alone, so experimentalMinChunkSize, the externals and the base path stay as they are. The check watches the output of your current configuration. If you want the agent to also split a large chunk, ask for that in the prompt and it will be a separate, explained change.

Does it work with Yarn, pnpm, npm or Bun?

It uses whatever your lockfile says. In bulletproof-react that is yarn.lock, so it adds the dev dependencies with Yarn and the CI step runs yarn size under the existing working-directory. With pnpm-lock.yaml, package-lock.json or bun.lock it uses that tool's install and run commands instead.

How are the budgets chosen?

From a real production build in the agent's sandbox, using the .env.example values the CI workflow itself uses. Each budget is set a little above the measured gzip size so the check passes on day one and fails on real growth. The measured sizes and the budgets are both in the pull request description, so a reviewer can tighten or loosen them in the diff.

Will hashed file names break the check after the next build?

The size-limit entries use globs over dist/assets such as the JavaScript and CSS extensions rather than exact hashed names, so a rebuild with new hashes still matches. If your build emits chunks into another directory, the agent reads that from the build output and writes the globs to match.

Can it post the size diff as a comment on each pull request?

Yes, when you ask for it in the prompt. It adds andresz1/size-limit-action to the workflow with the repository's GITHUB_TOKEN, which builds the base branch and the pull request and comments the difference per file. Without that, the step simply passes or fails and prints the sizes in the job log.

More Vite + React jobs

The same job on other stacks

Keep reading