-
Notifications
You must be signed in to change notification settings - Fork 0
/
compressImgs.js
124 lines (114 loc) · 3.6 KB
/
compressImgs.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import imagemin from "imagemin";
import imageminGifsicle from "imagemin-gifsicle";
import imageminMozjpeg from "imagemin-mozjpeg";
import imageminPngquant from "imagemin-pngquant";
import imageminSVG from "imagemin-svgo";
import fs from "fs-extra";
import path from "path";
let inDir, outDir;
inDir = "./images";
outDir = "./compressed";
fs.removeSync(outDir);
console.log("Processing images...\n");
fs.mkdirSync(outDir);
imagemin([`${inDir}/*.{jpg,png,gif,svg,webp}`], {
destination: outDir,
plugins: [
imageminMozjpeg({ quality: 80 }),
imageminPngquant({ quality: [0.65, 0.8] }),
imageminGifsicle({ optimizationLevel: 3 }),
imageminSVG({
plugins: [
{
name: "sortAttrs",
params: {
xmlnsOrder: "alphabetical",
},
},
{
name: "preset-default",
params: {
overrides: {
cleanupAttrs: true,
inlineStyles: true,
removeDoctype: true,
removeXMLProcInst: true,
removeComments: true,
removeMetadata: true,
removeTitle: true,
removeDesc: true,
removeUselessDefs: true,
removeXMLNS: false,
removeEditorsNSData: true,
removeEmptyAttrs: true,
removeHiddenElems: true,
removeEmptyText: true,
removeEmptyContainers: true,
removeViewBox: false,
cleanupEnableBackground: true,
minifyStyles: true,
convertStyleToAttrs: true,
convertColors: true,
convertPathData: true,
convertTransform: true,
removeUnknownsAndDefaults: true,
removeNonInheritableGroupAttrs: true,
removeUselessStrokeAndFill: true,
removeUnusedNS: true,
cleanupIDs: true,
cleanupNumericValues: true,
cleanupListOfValues: true,
moveElemsAttrsToGroup: true,
moveGroupAttrsToElems: true,
collapseGroups: true,
removeRasterImages: true,
mergePaths: true,
convertShapeToPath: true,
sortAttrs: true,
removeDimensions: true,
removeAttrs: true,
removeElementsByAttr: true,
addClassesToSVGElement: true,
addAttributesToSVGElement: true,
removeStyleElement: false,
removeScriptElement: true,
},
},
},
],
}),
],
}).then((files) => {
console.log("Image Optimization Results");
console.log("==============================");
logStats(files);
});
function logStats(files) {
let totalSaved = 0;
files.forEach(function (obj) {
let stats = getStats(obj);
console.log(
Math.round(((stats.oldSize - stats.newSize) / stats.oldSize) * 100) + "%",
stats.file
);
totalSaved += stats.oldSize - stats.newSize;
});
console.log(
"===============================\n",
btokb(totalSaved) + " Saved"
);
console.log("", "Files written to " + outDir);
}
function getStats(file) {
let fileName = path.parse(file.sourcePath).base;
let fileStats = fs.statSync(file.destinationPath);
let oldFileStats = fs.statSync(path.join(inDir, fileName));
return {
file: fileName,
oldSize: oldFileStats["size"],
newSize: fileStats["size"],
};
}
function btokb(val) {
return (val / 1000).toFixed(1) + "kb";
}