Skip to content

skillFrontendjavascript

Bundle Size Reduction

Analyzes a JavaScript bundle, splits code and replaces heavy dependencies, and reports byte counts before and after every change it makes.

npx workspacetemplates add bundle-size-reduction --tool claude-code

Writes 1 file to .claude/skills/bundle-size-reduction/

.claude/skills/bundle-size-reduction/SKILL.md30 lines · 1.7 KBFile
---
name: bundle-size-reduction
description: Analyzes a JavaScript bundle, splits code and replaces heavy dependencies, and reports byte counts before and after every change it makes.
---

# Bundle size reduction

Numbers first. Do not change anything until you have a baseline and a ranked list.

## Steps

1. Build for production and record total, gzip and brotli size per chunk. Generate a treemap with the project's bundler (`npx vite-bundle-visualizer`, `webpack-bundle-analyzer`, `npx source-map-explorer dist/*.js`, or `ANALYZE=true` for Next).
2. List the ten largest modules and who imports them. Look specifically for: full-library imports (`import _ from 'lodash'`, `import * as`), moment or other locale-heavy date code, duplicate versions of one package (`npm ls <pkg>`), polyfills for browsers you do not support, and icon sets imported whole.
3. Fix in order of bytes saved:
   - Replace heavy libraries with native APIs or small ones (`Intl.DateTimeFormat`, `structuredClone`, `date-fns` named imports).
   - Use named, tree-shakeable imports and check `sideEffects: false` in the package.
   - Dedupe versions with the package manager.
   - Lazy-load what is not needed on first paint: routes, modals, charts, editors, with `import()`.
4. Rebuild after every change and note the delta.
5. Check the result still works: run the tests and load the lazy parts once.

## Rules

- Do not remove a dependency still used elsewhere. Search before you delete.
- Never trade a size win for broken browser support listed in the project's browserslist.
- Stop when a change saves under 2 KB gzipped.

## Report

A table of change, bytes before, bytes after. Then what remains and why you left it.

What it does

Bundles grow one import at a time until a date library is bigger than the app. This skill opens the bundle analyzer first, ranks what is actually in the output, and attacks the largest items in order instead of tweaking config at random.

It prefers removing code over compressing it, and it stops when the remaining gains are smaller than the effort they cost.

Example

you › Our main bundle is 640 KB gzipped. Make it smaller.

agent › Main chunk 640 KB to 288 KB gzipped. Moment (72 KB) replaced with `Intl` calls, full lodash import (24 KB) replaced with three named imports, chart library (120 KB) lazy-loaded on the reports route, and two copies of `zod` deduped. Tests pass. Left the 40 KB rich-text editor: it is needed on the landing page.

Questions

Which bundlers does it support?
Vite, webpack, Rollup, esbuild and Next.js builds all work. If none is found, it asks which build command to use rather than guessing.
Will it swap my libraries without asking?
It replaces a dependency only when the swap keeps behavior the same and the tests still pass. Larger API changes are listed in the report for you to approve.
  • Frontend i18n

    SkillFrontend

    Free