Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add minification precommit hook #6059

Draft
wants to merge 1 commit into
base: vnext
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ node_modules
.idea

*.sublime-project
*.sublime-workspace
*.sublime-workspace

build/imagemin/already-minified.json
46 changes: 46 additions & 0 deletions build/imagemin/dirty.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// @ts-check
import imagemin from "imagemin";
import { promises as fs } from "fs";

import gifsicle from "imagemin-gifsicle";

This comment was marked as spam.

import pngquant from "imagemin-pngquant";
import mozjpeg from "imagemin-mozjpeg";
import { getAlreadyMinified, getDirtyImages, saveAlreadyMinified } from "./lib.mjs";

const files =
process.argv.length > 2 ? process.argv.slice(2) : getDirtyImages();

(async () => {
const alreadyMinified = await getAlreadyMinified();
for (const [i, file] of files.entries()) {
if (alreadyMinified.has(file)) {
console.log("Skipping", file, ", in already-minified.json");
files.splice(i, 1);
}
}

const minified = await imagemin(files, {
plugins: [
gifsicle(),
pngquant({ quality: [0.6, 0.8] }),
mozjpeg({ quality: 80 }),
],
});

for (const file of minified) {
const prev = await fs.stat(file.sourcePath);
alreadyMinified.add(file.sourcePath);
console.log(
`${file.sourcePath} => ${prev.size} to ${file.data.length} (${(
(file.data.length / prev.size) *
100
).toFixed(2)}%)`
);
await fs.writeFile(file.sourcePath, file.data);
}

await saveAlreadyMinified(alreadyMinified);
})().catch((e) => {
console.error(e);
process.exit(1);
});
33 changes: 33 additions & 0 deletions build/imagemin/lib.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// @ts-check
import { resolve } from "path";
import { execSync } from "child_process";
import { fileURLToPath } from "url";
import { promises as fs } from "fs";

const extensions = [".png", ".jpg", ".gif"];
const __dirname = fileURLToPath(new URL(".", import.meta.url));

export const alreadyMinifiedPath = resolve(__dirname, "already-minified.json");
export const repoPath = resolve(__dirname, "..", "..");

export const getDirtyImages = () =>
execSync("git ls-files --others")
.toString()
.split("\n")
.map((l) => l.trim())
.filter((l) => extensions.some((e) => l.endsWith(e)))
.map((l) => resolve(repoPath, l));

export const getAlreadyMinified = async () =>
new Set(
JSON.parse(
await fs.readFile(alreadyMinifiedPath, "utf-8").catch(() => "[]")
)
);

/** @param {Set<string>} alreadyMinified */
export const saveAlreadyMinified = (alreadyMinified) =>
fs.writeFile(
alreadyMinifiedPath,
JSON.stringify([...alreadyMinified], null, 2)
);
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,25 @@
},
"scripts": {
"check-lfs": "node ./build/check-lfs.js",
"minify": "node ./build/imagemin/dirty.mjs",
"prepare": "husky install"
},
"devDependencies": {
"gulp": "^4.0.2",
"husky": "^7.0.0",
"lint-staged": "^12.3.3",
"shelljs": "^0.8.5"
"shelljs": "^0.8.5",
"imagemin": "^8.0.1",
"imagemin-pngquant": "^9.0.2",
"imagemin-gifsicle": "^7.0.0",
"imagemin-mozjpeg": "^10.0.0"
},
"lint-staged": {
"*.{gif,mp4,jpg,png,GIF,MP4,JPG,PNG}": [
"yarn check-lfs"
],
"*.{gif,jpg,png,GIF,JPG,PNG}": [
"yarn minify"
]
},
"resolutions": {
Expand Down
Loading