From PHP to TypeScript: a WebP encoder without native bindings
I ported my PHP WebP encoder to TypeScript. PicoWebPJS runs in Node.js and modern browsers with its own image readers, zero runtime dependencies, and no native bindings or WebAssembly.
PicoWebP started in PHP: an encoder for hosts where the usual image tools aren't available. Now it has a TypeScript counterpart.
PicoWebPJS takes the same idea into Node.js and the browser. It includes the image readers and the code that writes the WebP bitstream, so there is no native binding to build or WebAssembly module to load. The npm package has zero runtime dependencies.
The included readers handle PNG, baseline JPEG and BMP, and the encoder also accepts raw RGB pixels. Even the PNG decompression code is part of the TypeScript implementation. Transparency and embedded ICC colour profiles carry through to the output; the colour data itself is compressed lossily.
The useful change is where this can run. A browser tool can convert images on the user's device, while a Node application can use the same encoding core. I included a Web Worker example because doing CPU-heavy image work on the main browser thread is a good way to make an otherwise decent interface feel broken.
A port also needs to preserve behaviour. The test suite compares output against fixtures from the PHP implementation and can check the generated files with libwebp's independent decoder when it is installed. That decoder is used for testing; the package does not need it to encode images.
There is a staged encoding path for Node.js too. An application can save encoder state, process work in bands, and continue in another process. This helps with scheduling longer jobs, although the image still needs to fit in memory during the initial staging step.
Native encoders remain the faster option in general. PicoWebPJS is useful when avoiding native tooling or a separate WASM asset matters more than encoding speed. Progressive JPEGs still need an additional decoder, and browser applications should keep larger conversions in a worker.
Version 1.0.0 is available on npm, with the source on GitHub, under the MIT licence.
npm install picowebpjs
The PicoWebPJS project page has the overview and package links. The original PHP version remains a separate package.