This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
81 lines (68 loc) · 2.21 KB
/
index.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const fs = require("fs");
const filesize = require("filesize");
const globby = require("globby");
const boxen = require("boxen");
const { red, green, bold } = require("chalk");
const { table, getBorderCharacters } = require("table");
const imagemin = require("imagemin-keep-folder");
const gifsicle = require("imagemin-gifsicle");
const optipng = require("imagemin-optipng");
const pngquant = require("imagemin-pngquant");
const svgo = require("imagemin-svgo");
const jpegtran = require("imagemin-jpegtran");
module.exports = {
onPostBuild: async config => {
const files = {};
const glob = `${config.constants.PUBLISH_DIR}/**/*.{gif,jpg,jpeg,png,svg}`;
const paths = await globby(glob);
paths.map(path => {
const stats = fs.statSync(path);
files[path] = {
pre: stats.size
};
return null;
});
const optimizedFiles = await imagemin([glob], {
plugins: [gifsicle(), optipng(), pngquant(), svgo(), jpegtran()]
});
optimizedFiles.map(file => {
const { path } = file;
const stats = fs.statSync(path);
files[path].post = stats.size;
files[path].diff = files[path].pre - files[path].post;
return null;
});
const totalSaved = Object.keys(files).reduce((total, filename) => {
return files[filename].diff + total;
}, 0);
const formattedData = Object.keys(files).map(filename => {
return [
filename.replace(config.constants.PUBLISH_DIR, ""),
red(filesize(files[filename].pre)),
green(filesize(files[filename].post)),
green.bold(filesize(files[filename].diff))
];
});
const data = [
[bold("File"), bold("Before"), bold("After"), bold("Reduction")],
...formattedData
];
console.log(
table(data, {
border: getBorderCharacters(`norc`),
drawHorizontalLine: index => {
return index === 1;
}
})
);
console.log(
boxen(bold(`Images optimized - ${green(filesize(totalSaved))} saved`), {
padding: {
left: 3,
right: 3
},
borderColor: "green"
})
);
}
};