Squeezit Image Optimizer

1.17.0 · active · verified Tue Apr 21

Squeezit (current stable version 1.17.0) is a versatile, lossless-first image optimization utility offered as a command-line interface (CLI), a JavaScript/TypeScript API, and a comprehensive suite of bundler plugins. It targets a wide array of modern and legacy image formats including PNG, JPEG, GIF, WebP, SVG, AVIF, HEIC, JXL, ICO, and BMP, focusing on reducing file sizes without noticeable degradation. The project exhibits active development, frequently integrating with contemporary build tools. Its key differentiators include broad format support, a user-friendly CLI with clear output, robust pattern matching for file selection (supporting shell-style and glob expressions), and a safe, threshold-based replacement mechanism to prevent unnecessary file churn. Squeezit integrates directly with ecosystems like Gulp, Grunt, Vite, Webpack, Rollup, Parcel, Astro, Next.js, esbuild, and Babel, making it suitable for a diverse range of development workflows and CI/CD pipelines.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates programmatic image optimization using the core `optimizeFile` API, creating a temporary file and reporting optimization results.

import { optimizeFile } from 'squeezit';
import { promises as fs } from 'fs';
import path from 'path';

async function runOptimization() {
  const imagePath = path.join(process.cwd(), 'example.png');
  const outputPath = path.join(process.cwd(), 'optimized-example.png');

  // Create a dummy image file for demonstration if it doesn't exist
  try {
    await fs.access(imagePath);
  } catch (error) {
    console.log(`Creating dummy file at ${imagePath}`);
    // Minimal PNG base64 representation (1x1 transparent PNG)
    const dummyPngBuffer = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=', 'base64');
    await fs.writeFile(imagePath, dummyPngBuffer);
  }

  console.log(`Optimizing ${imagePath}...`);
  const result = await optimizeFile(imagePath, { 
    output: outputPath,
    overwrite: true, // Overwrite if target path is the same
    dryRun: false // Set to true to preview without saving
  });

  if (result.success) {
    console.log(`Optimization successful:`);
    console.log(`  Original size: ${result.originalSize} bytes`);
    console.log(`  Optimized size: ${result.optimizedSize} bytes`);
    console.log(`  Reduction: ${((1 - result.optimizedSize / result.originalSize) * 100).toFixed(2)}%`);
    console.log(`  Output to: ${outputPath}`);
  } else {
    console.error(`Optimization failed: ${result.error}`);
  }

  // Clean up the dummy file
  await fs.unlink(imagePath);
  if (result.success) {
    await fs.unlink(outputPath);
  }
}

runOptimization().catch(console.error);

view raw JSON →