Skip to content

Instantly share code, notes, and snippets.

@just-boris
Last active May 13, 2022 11:16
Show Gist options
  • Save just-boris/bd81cbc77f0b208df9c9b1aafb8ebc6f to your computer and use it in GitHub Desktop.
Save just-boris/bd81cbc77f0b208df9c9b1aafb8ebc6f to your computer and use it in GitHub Desktop.
Use lint-staged to check file sizes

File size checker

Prevents you from accidentially commenting very big assets. Make sure that you have optimized all your assets.

Installation

  1. Add the script to your repo
  2. Configure lint-staged
"lint-staged": {
  "*": ["node ./scripts/check-files-size.js"],
}

Done! Now your repository will not bloat from unexpectedly big files.

#!/usr/bin/env node
const fs = require("fs");
const filesize = require("filesize");
const files = process.argv.slice(2);
const SIZE_LIMIT = 500 * 1024;
const filesOverLimit = [];
for (const file of files) {
const { size } = fs.statSync(file);
if (size > SIZE_LIMIT) {
filesOverLimit.push({ name: file, size });
}
}
if (filesOverLimit.length > 0) {
console.error(
`The following files are over the size limit (${filesize(SIZE_LIMIT)}):`
);
filesOverLimit.forEach(file =>
console.error(`\t* ${filesize(file.size)} ${file.name}`)
);
console.error("Please optimize the assets using https://imageoptim.com/mac");
process.exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment