Compress images in a pre-commit hook and in CI A developer published a workflow for automatically compressing oversized images before they reach a repository, using the pixelvault-cli tool in a git pre-commit hook and a GitHub Actions CI check. The hook compresses only staged images and re-stages them, while CI runs the idempotent optimizer and fails via `git diff --exit-code` when any image was missed. The tool runs locally on Node 20.9+, requires no account, and never writes a file larger than the original. Somebody or some coding agent adds a screenshot to the docs, and the PNG is 4 MB. Nobody notices in review, because the diff of a binary file is just "changed". A few months later the repo is heavy, the docs page is slow, and every clone downloads the same oversized images. The fix is boring and mechanical, so automate it: compress images before they are committed, and make CI fail when one slips through. One command does both. It needs no account and uploads nothing. npx pixelvault-cli optimize ./docs/images -r --in-place It needs Node 20.9+ and runs locally. It never writes a file bigger than the original. git diff --exit-code . Any changed file is an image someone forgot, so the build fails. --in-place overwrites files with no undo. Only use it on files under version control. optimize resizes, converts and strips metadata, and prints the saving for each file. A few properties make it safe in automation: --keep-metadata if you need it. Useful variations: Preview the saving without touching anything npx pixelvault-cli optimize ./docs/images -r --dry-run Convert a folder to WebP, capped at 1600px wide, into a separate directory npx pixelvault-cli optimize ./shots -r --format webp --max-width 1600 --out-dir ./optimized Machine-readable report always a JSON array npx pixelvault-cli optimize ./docs/images -r --json The default format is keep , so a PNG stays a PNG lossless and a JPEG stays a JPEG. Converting to WebP usually saves the most, but it changes the extension, so update any references to the old name. Save this as .git/hooks/pre-commit or call it from your hook manager and make it executable. It compresses only the images you are about to commit, then re-stages them: bash /bin/sh Compress staged images before they reach the repo. staged=$ git diff --cached --name-only --diff-filter=AM | grep -Ei '\. png|jpe?g|webp|avif $' -z "$staged" && exit 0 printf '%s\n' "$staged" | tr '\n' '\0' | xargs -0 npx --yes pixelvault-cli optimize --in-place &2 printf '%s\n' "$staged" | tr '\n' '\0' | xargs -0 git add --in-place is fine here because the files are about to be committed anyway. Hooks can be skipped, so add a CI check too. Because optimize is idempotent, a repo with compressed images produces no changes. Anything it does change is exactly the set of images someone forgot: .github/workflows/images.yml name: Check images on: pull request jobs: optimize: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: 22 - name: Compress images run: npx --yes pixelvault-cli optimize ./docs ./public -r --in-place - name: Fail if any image was not optimized run: | git diff --exit-code --stat || { echo "Some images were not optimized. Run: npx pixelvault-cli optimize