forked from open-sauced/open-sauced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.squoosh.mjs
53 lines (47 loc) · 1.12 KB
/
.squoosh.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { dirname } from "path";
import execa from "execa";
import glob from "glob";
import pLimit from "p-limit";
const limit = pLimit(3);
const [globs] = process.argv.slice(2);
const algo = {
'jpg': '--mozjpeg',
'jpeg': '--mozjpeg',
'png': '--oxipng',
'webp': '--webp',
'avif': '--avif',
'jxl': '--jxl',
'wp2': '--wp2',
};
const promises = [];
const errors = [];
const imgs = glob.sync(globs, {nonull: false});
imgs && imgs.map(async (img) => promises.push(
limit(async () => {
try {
const dir = dirname(img);
const ext = /\.([^\.]+)$/ig.exec(img)[1];
await execa("squoosh-cli", [
algo[ext],
"auto",
"--output-dir",
dir,
img
], {
stderr: process.stdout,
stdout: process.stdout
});
} catch (e) {
console.log(e);
// errors.push(e.message || e);
}
})
));
await Promise.all(promises);
if (errors.length > 0) {
console.error('Optimising images failed with errors:\n\n', errors.join('\n\n'));
process.exit(1);
} else {
console.log('Optimised all images, running additional commands now');
process.exit(0);
}