libSquoosh: Squoosh's image compression, now a Node library

Squoosh has long been a go-to for client-side image compression. The PWA runs entirely in the browser, using WebAssembly to power a range of modern and legacy codecs. That means images never leave your machine, and the tool works even without a connection.

Last year's Squoosh v2 added a command-line interface, bringing those same codecs to Node for batch processing and automated optimization workflows. The natural next step: exposing that same core functionality with a JavaScript-friendly API instead of forcing developers to shell out to a CLI.

That step is now here with libSquoosh, an experimental Node library that powers the Squoosh CLI. It gives developers programmatic access to all of Squoosh's codecs so image compression can be baked into larger tooling.

Surma

The work comes courtesy of Anton (@atjn on GitHub), who split the original Squoosh CLI codebase into two distinct layers: the command-line interface itself, and the underlying core library that it calls into.

Designed for the build pipeline

The driving motivation behind libSquoosh is to make image optimization an easier, more accessible component for authors of developer tools. The team is looking toward integration with bundlers like Webpack and Rollup so that images get automatically optimized as part of the standard build process.

Usage follows a simple, promise-based pattern:

import { ImagePool } from "@squoosh/lib";

// libSquoosh uses a worker-pool under the hood
// to parallelize all image processing.
const imagePool = new ImagePool();

// Accepts both file paths and Buffers/TypedArrays.
const image = imagePool.ingestImage("./squoosh.jpeg");

// Optional.
// await image.preprocess({
//   resize: {
//     enabled: true,
//     width: 128,
//   },
// });

await image.encode({
  // All codecs are initialized with default values
  // that can be individually overwritten.
  mozjpeg: {
    quality: 10,
  },
  avif: {
    cqLevel: 10,
  },
  jxl: {},
});

const { extension, binary } = await image.encodedWith.mozjpeg;
await fs.writeFile(`output.${extension}`, binary);
// ... same for other encoders ...

await imagePool.close();

Early days, open invites

Both the CLI and libSquoosh are still young. The team describes libSquoosh explicitly as an early, experimental release, and warns that bugs are likely. Developers who run into issues or have questions are encouraged to file them via the project's issue tracker.

There's also room for contributors. Documentation around libSquoosh is admittedly sparse, and help is welcome. The project is starting a mentorship program for those interested in getting involved with Squoosh development; details are available in the tracking issue.