All notes

I Built an Image Tool, Then Made It Refuse to Touch Your Project

A 26.96 MB PNG became 1.17 MB. The compression was the easy part. The hard part was building a tool that's allowed to edit your project and still behaves when things go wrong.

5 min readrusttypescripttooling
On this page
  1. Rules before features
  2. Two things that broke
  3. The command that edits your code
  4. Why Rust, when sharp exists
  5. Where it stands

On one of the Next.js projects I tested it on, Vysk turned a 26.96 MB PNG into a 1.17 MB file. That's about 96% smaller.

Plenty of tools can do that, though. What I kept stopping to think about was something else: what happens when you give a tool permission to touch somebody's project? Making a file smaller is one thing. Changing someone's source code is another, and if the tool gets it wrong, I don't want a developer staring at their repo wondering what just happened.

That shaped how I built it.

Rules before features#

Vysk is an npm package. You point it at a project, it finds PNG and JPG files, converts them to WebP where that makes sense, and prints a report of what it saved. Before adding options, I wrote down what it should never do.

It never touches your originals. hero.png stays, and hero.webp appears beside it. If the optimiser makes a bad call, you can compare the two, delete the output and undo everything.

It skips any output that isn't smaller. Conversion doesn't guarantee a smaller file. If a 400 KB image becomes 650 KB, calling that "optimised" would be a lie, so the output is dropped.

It remembers what it did. A hash-based cache means unchanged images aren't decoded and compressed again. Running it a second time over all 14 images in that project finished instantly from the cache, with identical totals.

--dry-run is the real pipeline minus the writing. Real scan, real analysis, real decisions, nothing written. You see what Vysk thinks it's about to do before it does it.

Two things that broke#

WebP has a size limit. It can't encode an image larger than 16,383 pixels on either side, and at first those images were simply skipped. That technically worked, but silently leaving them alone wasn't useful. So I added an AVIF fallback using the ravif crate. It pulled a feature forward from a later phase, and it also brought a new build requirement, nasm. You find costs like that only after you've decided you need the feature.

The other one was embarrassing. Vysk refuses to modify a project unless the Git tree is clean, so you can always undo what it did without wondering which changes were yours. My first check looked for a .git folder in the project directory. But a project can sit inside a larger repository:

some-repository/
├── .git/
└── projects/
    └── my-next-app/

Standing in my-next-app, there's no .git there, so Vysk said "no Git here" and refused to run. Git was there. I was asking the wrong question.

The fix was simpler than the code it replaced:

git status --porcelain

If that prints nothing, the tree is clean. Git already knows how to find its own repository, so now Vysk asks it. It works at the repository root or anywhere inside. The bug had nothing to do with images, but it taught me something about building tools: don't guess about a system when the system already knows the answer.

The command that edits your code#

Optimising an image is only half the job. If hero.png becomes hero.webp but your code still says <Image src="/hero.png" />, nothing has improved. So there's a rewrite command that finds image references in your code and updates them.

That's the nervous part, because now Vysk is changing source files. So I built it around refusals instead of speed:

  1. It refuses to run on a dirty Git tree, so it never edits on top of changes it doesn't know about.
  2. It shows the complete plan: which files, which references, what changes.
  3. It writes nothing until you explicitly say yes.
  4. It backs up the originals first.

It has 25 passing tests, and a lot of them don't check "did it change the file?" They check that it refuses to change the file when it shouldn't.

The compression is only one part of the problem. The rest is a trust model: what if the project is already modified, the output is bigger, the image can't be encoded, the reference format is unusual, the tool crashes halfway or someone runs it in the wrong directory? That part turned out to be the more interesting one.

Why Rust, when sharp exists#

sharp is excellent, and I won't pretend JavaScript couldn't do this. Part of the reason is simply that I wanted to build a real Rust project, and decoding and encoding is exactly the kind of work a native core suits. The job splits along a clean line:

CLI → Node + TypeScript → napi-rs → Rust core → WebP / AVIF

Node and TypeScript handle the project side: scanning, config, caching, reporting. Rust handles decoding, compression and encoding. napi-rs joins them, so whoever installs Vysk gets one npm package and never needs a Rust toolchain. That's my problem, not theirs.

Where it stands#

Vysk is on npm now, but it's early. I haven't tested it properly on Windows, and so far I've mostly run it against projects I have access to. It works on the ones I've tried and the tests pass, but it needs more machines, more operating systems and more weird directory structures. Sooner or later somebody will run npx vysk inside a monorepo they built in 2019, and I'd rather find out how it copes now.

The 26.96 MB to 1.17 MB result is nice. But it isn't what I'm proudest of. I'm prouder of the originals it leaves alone, the outputs it throws away, the dry run, the Git check, the backups, and the tests that mostly ask whether it correctly refused. Anyone can write a script that changes files. The harder question is whether you can make one that knows when it shouldn't.