# zunaib/bundle-size-guardian architecture

> Architecture diagram of the GitHub repository zunaib/bundle-size-guardian, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zunaib/bundle-size-guardian
- Repository: https://github.com/zunaib/bundle-size-guardian

## Overview

bundle-size-guardian audits JavaScript/TypeScript dependencies, recommends smaller alternatives, and can rewrite imports through a CLI or MCP interface. The principal flow is analyze imports and sizes, surface alternatives, then optionally generate and apply refactor patches. Edges reflect the shown calls and documented external behavior; test/rollback behavior is visible in the refactor implementation, while some CLI branches are only partially excerpted.

Main entry points: [`src/cli.ts`](https://github.com/zunaib/bundle-size-guardian/blob/main/src/cli.ts) (CLI commands), [`src/refactor.ts`](https://github.com/zunaib/bundle-size-guardian/blob/main/src/refactor.ts) (Patch and package changes), [`src/mcp-server.ts`](https://github.com/zunaib/bundle-size-guardian/blob/main/src/mcp-server.ts) (MCP tools), [`src/suggest.ts`](https://github.com/zunaib/bundle-size-guardian/blob/main/src/suggest.ts) (Alternative selection)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interfaces["User interfaces"]
  node_cli["CLI commands<br/>[cli.ts]"]
  node_mcp["MCP tools<br/>[mcp-server.ts]"]
end

subgraph group_audit["Dependency audit"]
  node_analysis["Import analysis<br/>[analyze.ts]"]
  node_size_lookup["Bundle size lookup<br/>[bundlesize.ts]"]
  node_esbuild["Local size estimate<br/>[bundlesize.ts]"]
end

subgraph group_recommend["Alternative recommendations"]
  node_recommendations["Alternative selection<br/>[suggest.ts]"]
  node_known_map["Built-in mappings"]
end

subgraph group_rewrite["Refactor workflow"]
  node_file_discovery["Affected file discovery<br/>[refactor.ts]"]
  node_patch_generation["Patch generation<br/>[refactor.ts]"]
  node_patch_application["Patch and package changes<br/>[refactor.ts]"]
  node_test_rollback["Test and rollback<br/>[refactor.ts]"]
  node_savings_log["Savings log<br/>[refactor.ts]"]
end

node_cli_user(("CLI user"))
node_mcp_client(("MCP client"))
node_project["Target project"]
node_bundlephobia["Bundlephobia API"]
node_claude["Anthropic Claude"]
node_npm_tests["Project test command"]
node_npm_packages["npm package manager"]

node_cli_user -->|"runs commands"| node_cli
node_cli -->|"analyzes imports"| node_analysis
node_cli -->|"requests suggestions"| node_recommendations
node_cli -->|"requests refactor"| node_file_discovery
node_cli -->|"starts server"| node_mcp
node_mcp_client -->|"calls tools"| node_mcp
node_mcp -->|"analyzes imports"| node_analysis
node_mcp -->|"requests suggestions"| node_recommendations
node_mcp -->|"requests refactor"| node_file_discovery
node_project -->|"provides manifests and source"| node_analysis
node_analysis -->|"looks up sizes"| node_size_lookup
node_size_lookup -->|"queries sizes"| node_bundlephobia
node_size_lookup -->|"falls back to estimate"| node_esbuild
node_recommendations -->|"checks mappings"| node_known_map
node_recommendations -->|"queries unknown packages"| node_claude
node_project -->|"provides source files"| node_file_discovery
node_file_discovery -->|"provides affected contents"| node_patch_generation
node_patch_generation -->|"requests rewritten files"| node_claude
node_patch_generation -->|"returns patches"| node_patch_application
node_patch_application -->|"writes files"| node_project
node_patch_application -->|"installs replacement and removes old package"| node_npm_packages
node_patch_application -->|"optionally tests changes"| node_test_rollback
node_test_rollback -->|"runs test commands"| node_npm_tests
node_test_rollback -->|"restores files on failure"| node_project
node_patch_application -->|"records completed change"| node_savings_log

click node_cli "https://github.com/zunaib/bundle-size-guardian/blob/main/src/cli.ts"
click node_mcp "https://github.com/zunaib/bundle-size-guardian/blob/main/src/mcp-server.ts"
click node_analysis "https://github.com/zunaib/bundle-size-guardian/blob/main/src/analyze.ts"
click node_size_lookup "https://github.com/zunaib/bundle-size-guardian/blob/main/src/bundlesize.ts"
click node_esbuild "https://github.com/zunaib/bundle-size-guardian/blob/main/src/bundlesize.ts"
click node_recommendations "https://github.com/zunaib/bundle-size-guardian/blob/main/src/suggest.ts"
click node_known_map "https://github.com/zunaib/bundle-size-guardian/blob/main/src/known-alternatives.ts"
click node_file_discovery "https://github.com/zunaib/bundle-size-guardian/blob/main/src/refactor.ts"
click node_patch_generation "https://github.com/zunaib/bundle-size-guardian/blob/main/src/refactor.ts"
click node_patch_application "https://github.com/zunaib/bundle-size-guardian/blob/main/src/refactor.ts"
click node_test_rollback "https://github.com/zunaib/bundle-size-guardian/blob/main/src/refactor.ts"
click node_savings_log "https://github.com/zunaib/bundle-size-guardian/blob/main/src/refactor.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_mcp,node_cli_user,node_mcp_client toneBlue
class node_analysis,node_size_lookup,node_esbuild toneAmber
class node_recommendations,node_known_map,node_bundlephobia toneMint
class node_file_discovery,node_patch_generation,node_patch_application,node_test_rollback,node_savings_log toneRose
class node_project,node_claude,node_npm_tests,node_npm_packages toneIndigo
```

## Components

### User interfaces

- [CLI commands](https://github.com/zunaib/bundle-size-guardian/blob/main/src/cli.ts): `src/cli.ts`
- [MCP tools](https://github.com/zunaib/bundle-size-guardian/blob/main/src/mcp-server.ts): `src/mcp-server.ts`

### Dependency audit

- [Import analysis](https://github.com/zunaib/bundle-size-guardian/blob/main/src/analyze.ts): `src/analyze.ts`
- [Bundle size lookup](https://github.com/zunaib/bundle-size-guardian/blob/main/src/bundlesize.ts): `src/bundlesize.ts`
- [Local size estimate](https://github.com/zunaib/bundle-size-guardian/blob/main/src/bundlesize.ts): `src/bundlesize.ts`

### Alternative recommendations

- [Alternative selection](https://github.com/zunaib/bundle-size-guardian/blob/main/src/suggest.ts): `src/suggest.ts`
- [Built-in mappings](https://github.com/zunaib/bundle-size-guardian/blob/main/src/known-alternatives.ts): `src/known-alternatives.ts`

### Refactor workflow

- [Affected file discovery](https://github.com/zunaib/bundle-size-guardian/blob/main/src/refactor.ts): `src/refactor.ts`
- [Patch generation](https://github.com/zunaib/bundle-size-guardian/blob/main/src/refactor.ts): `src/refactor.ts`
- [Patch and package changes](https://github.com/zunaib/bundle-size-guardian/blob/main/src/refactor.ts): `src/refactor.ts`
- [Test and rollback](https://github.com/zunaib/bundle-size-guardian/blob/main/src/refactor.ts): `src/refactor.ts`
- [Savings log](https://github.com/zunaib/bundle-size-guardian/blob/main/src/refactor.ts): `src/refactor.ts`

### Other components

- **CLI user**
- **MCP client**
- **Target project**
- **Bundlephobia API**
- **Anthropic Claude**
- **Project test command**
- **npm package manager**

## Connections

- CLI user → CLI commands: runs commands (evidence: `README.md`)
- CLI commands → Import analysis: analyzes imports (evidence: `src/cli.ts`)
- CLI commands → Alternative selection: requests suggestions (evidence: `src/cli.ts`)
- CLI commands → Affected file discovery: requests refactor (evidence: `src/cli.ts`)
- CLI commands → MCP tools: starts server (evidence: `src/cli.ts`)
- MCP client → MCP tools: calls tools (evidence: `README.md`)
- MCP tools → Import analysis: analyzes imports (evidence: `src/mcp-server.ts`)
- MCP tools → Alternative selection: requests suggestions (evidence: `src/mcp-server.ts`)
- MCP tools → Affected file discovery: requests refactor (evidence: `src/mcp-server.ts`)
- Target project → Import analysis: provides manifests and source (evidence: `src/analyze.ts`)
- Import analysis → Bundle size lookup: looks up sizes (evidence: `src/analyze.ts`)
- Bundle size lookup → Bundlephobia API: queries sizes (evidence: `src/bundlesize.ts`)
- Bundle size lookup → Local size estimate: falls back to estimate (evidence: `src/bundlesize.ts`)
- Alternative selection → Built-in mappings: checks mappings (evidence: `src/suggest.ts`)
- Alternative selection → Anthropic Claude: queries unknown packages (evidence: `src/suggest.ts`)
- Target project → Affected file discovery: provides source files (evidence: `src/refactor.ts`)
- Affected file discovery → Patch generation: provides affected contents (evidence: `src/refactor.ts`)
- Patch generation → Anthropic Claude: requests rewritten files (evidence: `src/refactor.ts`)
- Patch generation → Patch and package changes: returns patches (evidence: `src/refactor.ts`)
- Patch and package changes → Target project: writes files (evidence: `src/refactor.ts`)
- Patch and package changes → npm package manager: installs replacement and removes old package (evidence: `src/refactor.ts`)
- Patch and package changes → Test and rollback: optionally tests changes (evidence: `src/refactor.ts`)
- Test and rollback → Project test command: runs test commands (evidence: `src/refactor.ts`)
- Test and rollback → Target project: restores files on failure (evidence: `src/refactor.ts`)
- Patch and package changes → Savings log: records completed change (evidence: `src/refactor.ts`)
