Why I stopped using online image compressors and built a CLI instead
说明与用途
开发者构建了一个 CLI 工具 (pixcrush) 来自动将重型图像转换为 WebP 并更新代码引用,取代了繁琐的手动过程。
问题
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.
结果
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.
工作流
- 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.
证据
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';
- 记录日期: 2026-09-02T14:27:37.490Z