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