— Tutorial, Javascript, Git, Hooks — 2 min read
This story is kind of story and tutorial mix. It consist following parts:
So without delay let’s get started. WHY,WHAT AND HOW :-)
This tutorial is not about why we need to compress images. There are already ton of good resources over the internet of this. In summary:
That’s it there is a lot more information about this. But there is no point of explaining here. You can find all this information on this internet.
Whenever I am doing my personal project or in my organisation. I need to organise my images and have to go to manually some compressing site and then convert it into compressed one and replaced. My lead asked me one day why don’t we automate this stuff. I don’t find any good source or anything to automate this. So I thought It’s worth sharing also.
Note: There are already 3 party services which done this for you. But again you have to buy that service. This tutorial is all about automating compression using hooks.
This is the interesting part. Let’s start it. Initially the images size is this:
Note: I took this image for demo purpose only
So for automating this stuff I use husky(for git hooks) and lint-staged.
For compression of images i use sharp (it’s open source). If you have this question why this package why not other package there are so many good packages. I totally agree with this. But all this question is already answered sharp. You can check its performance guide. It’s already giving answer to those question.
So logic is that before committing to the code check of images and if images find then compress them before committing. You can do other things (like post commit, pre build etc).
Here is complete code:
1{2 "hooks": {3 "pre-commit": "lint-staged"4 }5}
1/**2 A precommit script to compress image before commiting file3 Q: Why Sharp package used?4 A: https://sharp.pixelplumbing.com/performance56 */78const fs = require("fs");9const sharp = require("sharp");//https://sharp.pixelplumbing.com/1011/*12Function: Update existing file to new compress file13 */14const minifyFile = filename => {15 new Promise((resolve, reject) => {16 /*Read upcomimg file*/17 fs.readFile(filename, function(err, sourceData) {18 if (err) throw err;19 /*If file buffer data is present convert it into new compressed file*/20 sharp(sourceData).toFile(filename, (err, info) => {21 err ? reject(err) : resolve();22 });23 });24 });25};2627/*28Fetch images maps from args and compress all.29Compressing is asynchronuous process.30So wait for all image to compress and return.31*/32Promise.resolve(process.argv)/*Find more. here: https://nodejs.org/en/knowledge/command-line/how-to-parse-command-line-arguments/*/33 .then(x => x.slice(2))34 .then(x => x.map(minifyFile))35 .then(x => Promise.all(x))36 .catch(e => {37 process.exit(1);38 });
1{2 "name": "image-compression",3 "version": "1.0.0",4 "description": "Pre commit script to compress images",5 "main": "index.js",6 "scripts": {7 "test": "echo \"Error: no test specified\" && exit 1"8 },9 "keywords": [],10 "author": {11 "name": "Shubham Verma"12 },13 "license": "ISC",14 "devDependencies": {15 "husky": "^4.2.5",16 "lint-staged": "^10.2.7",17 "sharp": "^0.25.3"18 },19 "lint-staged": {20 "*.{png,jpeg,jpg,gif,svg}": [21 "node ./compress-image.js"22 ]23 }24}
I already explained everything in code. Feel free to reach me out.
That’s all folks. Feel free to ask a question if you find any doubt. The complete code is available on github. Feel free to checkout