Why I stopped using online image compressors and built a CLI instead
Description and use
The developer built a CLI tool (pixcrush) to automatically convert heavy images to WebP and update code references, replacing a tedious manual process.
Problem
Unoptimized images (heavy PNG and JPG files) are common in projects, especially small and mid-sized ones, and cleaning them up manually is tedious and error-prone.
Outcome
The developer built a CLI tool that automates the migration of heavy images to WebP. The tool scans the repository, identifies used images, converts them in memory, and updates code references automatically, making the process safe and repeatable.
Workflow
- Scan the repository for supported images (PNG, JPG, JPEG) and source files (.js, .jsx, .ts, .tsx, .html, .).
- Determine which images are actually referenced by parsing source code with Babel (JSX/TS plugins) and resolving common patterns (imports, src attributes, srcset strings).
- Convert used images to WebP in memory using Sharp, comparing the output buffer size to the original to skip conversions that are larger or equal.
- Update only the code references connected to successful conversions, preserving file formatting.
- Report what changed, what was skipped, and what needs human review.
Evidence
One command to convert the images, compress them, and update their matching code references automatically.
pixcrush creates the WebP data in memory first... compare the proposed output with the original before writing anything.
The tracker resolves several common patterns: import logo from './logo.png'; <Image src="/images/hero.jpg" />; const imagePath = '@/assets/product.png';
- Record date: 2026-09-02T14:27:37.490Z