backend-diet
v1.0.1
Published
Scan your source code, find heavy library usage, and get native alternatives with code snippets — right in your terminal.
Downloads
2
Maintainers
Readme
backend-diet
Scan your source code. Find the bloat. Get native alternatives.
Most tools tell you a package is large.
backend-diet tells you exactly what to replace and how.
What is this?
Tools like depcheck or bundle-analyzer flag large packages — but leave you to figure out the migration yourself. backend-diet goes further:
- Parses your source code using a real AST (Babel) — no regex guessing
- Identifies which specific functions you're importing from heavy packages
- Suggests modern, native Node.js or Web API alternatives with copy-paste code snippets
- Gives your project a Refactor Score and tracks it over time
No config. No setup. Works on any JS/TS/JSX/TSX project.
Quick Start
# Run instantly — no install needed
npx backend-diet scan
# Or install globally
npm install -g backend-diet
backend-diet scanUsage
backend-diet scan [directory] # Scan current dir or a specific path
backend-diet scan --fix # Preview auto-patches (dry run)
backend-diet scan --fix --yes # Apply trivial patches in-place
backend-diet scan --badge # Generate a README badge
backend-diet scan --since HEAD~10 # Show score trend across commits
backend-diet scan --format json # JSON output for CI pipelines
backend-diet scan --ignore "vendor/**" # Exclude pathsRefactor Score
Every scan produces a 0–100 score based on how much of your dependency weight could be eliminated with native alternatives.
Score = 100 − round((removable_bytes / total_dep_bytes) × 100)| Score | Grade | Badge Color |
|---|---|---|
| 90 – 100 | SVELTE | |
| 70 – 89 | HEALTHY |
|
| 40 – 69 | NEEDS WORK |
|
| 0 – 39 | CRITICAL |
|
A CRITICAL score causes the process to exit with code 1, making it useful as a CI gate.
Supported Packages
| Package | Functions Covered | Gzipped Size |
|---|:---:|---:|
| lodash | 19 | ~24 KB |
| moment | 9 | ~72 KB |
| axios | 7 | ~14 KB |
| bluebird | 9 | ~17 KB |
| underscore | 12 | ~6.8 KB |
| uuid | 2 | ~3.7 KB |
Features
AST-Accurate Detection
Uses @babel/parser to walk the real syntax tree — handles all four import patterns with zero false positives:
import { cloneDeep } from 'lodash' // named import
import cloneDeep from 'lodash/cloneDeep' // sub-path import
const { cloneDeep } = require('lodash') // CommonJS destructure
_.cloneDeep(obj) // namespace callAuto-Patch --fix
For simple 1-to-1 replacements (difficulty: trivial), backend-diet can rewrite your source files automatically. It always shows a diff preview first.
backend-diet scan --fix # preview only
backend-diet scan --fix --yes # apply in-placeExample patches applied:
- import { v4 as uuidv4 } from 'uuid';
- const id = uuidv4();
+ const id = crypto.randomUUID();- import { cloneDeep } from 'lodash';
- const copy = cloneDeep(obj);
+ const copy = structuredClone(obj);- import { get } from 'lodash';
- const val = get(obj, 'a.b.c', defaultVal);
+ const val = obj?.a?.b?.c ?? defaultVal;Badge Generator --badge
Paste your project's live diet score directly into your README.
backend-diet scan --badgeOutput (paste into your README.md):
[](https://github.com/1iPluto/backend-diet)Time Machine --since
Track how your project's weight has changed over time. A .backend-diet-history.json file is written on every scan, recording the score, grade, and git SHA.
backend-diet scan --since HEAD~20
# Score trend: ▁▂▃▃▄▅▅▆▅▄▃ ▼ -8 (getting fatter!)Commit the history file to let your whole team see the trend.
CI Integration --format json
backend-diet scan --format jsonOutputs a structured JSON report:
{
"summary": {
"filesScanned": 42,
"matchesFound": 9,
"totalBytesSaved": 89400,
"totalBytesSavedFormatted": "89.4 KB",
"refactorScore": 34,
"grade": "CRITICAL"
},
"matches": [ ... ]
}Add it to your GitHub Actions workflow:
- name: Dependency diet check
run: npx backend-diet scan --format json
# Exits 1 if grade is CRITICAL — blocks the mergeContributing
The diet database is the core of this tool and the easiest place to contribute. Every new package entry helps thousands of developers.
To add a new package:
- Fork the repository
- Create
src/database/packs/<package-name>.ts - Implement the
PackageEntryinterface (seesrc/types.ts) - Register it in
src/database/index.ts - Open a pull request titled
feat: add <package-name> to diet database
See CONTRIBUTING.md for the full guide, difficulty levels, and code snippet rules.
Tech Stack
| Concern | Tool | Why |
|---|---|---|
| AST parsing | @babel/parser + @babel/traverse | Handles JS/TS/JSX/TSX in one pass |
| File walking | fast-glob | 3–5× faster than glob on monorepos |
| CLI | commander | Zero dependencies, excellent TS types |
| Terminal UI | chalk, boxen, ora, cli-table3 | Composable, well-maintained |
| Build | tsup | Single-file CJS output in <100ms |
License
MIT © Marwan Said
