Automated image compression made easy with Merlin
Image compression is an undisputed critical part of performance optimization. For compression there are many tools, whether editor plugins (like Image Optimizer), standalone apps (like ImageOptim) or scripts (like the Node packages of the imagemin family). Yet two problems persist: Tooling doesn’t solve the context problem of image compression, and automation often is unnecessarily complicated.
The context problem is interesting and also something we still work on at sum.cumo – finding a solution for it is important to get the maximum out of compression.
The automation problem, then, we tackled by looking for another, simpler way. That way now allows us to quickly set up a minimum of compression for GIFs, JPG and PNGs in very different projects, with literally one and a half commands and a line of configuration: automated lossless compression that is easy on resources (only images that are being added or changed are checked, and then compressed without regression) and that also updates and feeds back to the source files.
Assuming a Node project, image compression through Merlin is set up as follows:
npm i -D @sum.cumo/imagemin-merlin(ensure the following
"imagemin": "imagemin-merlin", so to allow for manual and automatic execution).
Set up a Git hook for
pre-commit(for example, through Husky):
npm run imagemin -- --staged.
The documentation describes this in a little more detail, and it explains additional parameters. The point is that after installation and setup of a hook, Merlin guarantees GIFs, JPGs and PNGs to always be compressed at a minimum level. This has benefited our projects in the last few months and closed the flank that some graphics had not been compressed at all, or required someone with special technical knowledge to set everything up in more exotic cases. We hope the same applies to you when you try Merlin in your projects.