Bundle size audit
Goal
Know exactly which modules make up the shipped JavaScript and remove the ones that do not earn their bytes. A good result lists the top contributors, the fixes applied and the measured size before and after.
When to use
- The user complains about bundle size or slow first load from script.
- A new dependency may have inflated the build.
- For setting limits that prevent regressions, use
use_skillwithweb-performance-budget. - For library upgrades that change size,
use_skillwithdependency-upgradecovers the upgrade work. - A new feature added a chart, editor or date library and the build jumped.
- The team wants a first look at dependency weight before choosing between two libraries.
Steps
- Identify the bundler and build script from
package.jsonwithread_file. Run the production build withrun_commandand record the emitted file sizes, raw and gzipped. - Produce a breakdown. Use the bundler's analysis output or a visualizer plugin, installing it only with the user's consent. Check the current plugin name and usage with
search_weborread_urlrather than recalling it. - List the top ten modules by size, grouped by package. Mark the ones loaded on the first screen versus those needed later.
- For each large package, ask three questions: is it needed at all, is there a lighter alternative, is the app importing the whole library when it uses one function.
- Fix imports. Replace namespace or barrel imports with direct imports, switch to ESM builds, and check that
sideEffectsin the package allows tree shaking. - Split by route and by interaction: dynamic
import()for pages, charts, editors, and modals that most visitors never open. - Look for duplicates, such as two versions of the same package or two libraries doing one job, using
run_commandwith the package manager's dependency tree command. - Rebuild, compare sizes with the baseline, and run the tests with
run_commandto confirm nothing broke. - Check polyfills and build targets: an overly old browser target ships transpiled code and helpers that modern browsers never need.
- Look at source maps and CSS too: unused CSS and duplicated global styles can rival script size on content sites.
Gotchas
- Measuring a development build is useless. It has no minification and extra helper code.
- Gzip size is what travels, parsed size is what costs CPU on a phone. Report both and say which one a number is.
- A barrel file that re-exports everything can defeat tree shaking when modules have side effects. Import from the specific path.
- Lazy loading something needed for first render only delays it and adds a waterfall. Split what is not needed immediately.
- Moment-style libraries with bundled locales or data tables dominate bundles. Check for locale and timezone data before anything else.
- Removing a dependency used by a dynamic import or a config file breaks at runtime, not at build. Run the app and do not stop at the build.
- A dependency listed as small on a registry page may pull a large tree of its own. Measure in your build, not by package page.
- Importing a locale or icon set dynamically with a wildcard makes the bundler include every file. Use explicit paths.
- Minifier settings and compression differ between local and the host. Compare sizes with the same compression the server applies.
Output
- A table of the top contributors: package, size, used where, action.
- The changes made and the before and after totals, raw and gzipped.
- What you recommend but did not do, with the risk.
- Note which findings need a product decision, such as dropping a feature, versus a pure engineering change.
