Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

Commit 772c71f

Browse files
initial commit
0 parents  commit 772c71f

File tree

6 files changed

+3138
-0
lines changed

6 files changed

+3138
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

index.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const fs = require("fs");
2+
const filesize = require("filesize");
3+
const globby = require("globby");
4+
const boxen = require("boxen");
5+
const { red, green, bold } = require("chalk");
6+
const { table, getBorderCharacters } = require("table");
7+
const imagemin = require("imagemin-keep-folder");
8+
const gifsicle = require("imagemin-gifsicle");
9+
const optipng = require("imagemin-optipng");
10+
const pngquant = require("imagemin-pngquant");
11+
const svgo = require("imagemin-svgo");
12+
13+
module.exports = () => {
14+
return {
15+
name: "netlify-plugin-image-optim",
16+
17+
postBuild: async config => {
18+
const files = {};
19+
const glob = `${config.constants.BUILD_DIR}/**/*.{gif,jpg,jpeg,png,svg}`;
20+
const paths = await globby(glob);
21+
22+
paths.map(path => {
23+
const stats = fs.statSync(path);
24+
25+
files[path] = {
26+
pre: stats.size
27+
};
28+
29+
return null;
30+
});
31+
32+
const optimizedFiles = await imagemin([glob], {
33+
plugins: [gifsicle(), optipng(), pngquant(), svgo()]
34+
});
35+
36+
optimizedFiles.map(file => {
37+
const { path } = file;
38+
const stats = fs.statSync(path);
39+
40+
files[path].post = stats.size;
41+
files[path].diff = files[path].pre - files[path].post;
42+
43+
return null;
44+
});
45+
46+
const totalSaved = Object.keys(files).reduce((total, filename) => {
47+
return files[filename].diff + total;
48+
}, 0);
49+
50+
const formattedData = Object.keys(files).map(filename => {
51+
return [
52+
filename.replace(config.constants.BUILD_DIR, ""),
53+
red(filesize(files[filename].pre)),
54+
green(filesize(files[filename].post)),
55+
green.bold(filesize(files[filename].diff))
56+
];
57+
});
58+
59+
const data = [
60+
[bold("File"), bold("Before"), bold("After"), bold("Reduction")],
61+
...formattedData
62+
];
63+
64+
console.log(
65+
table(data, {
66+
border: getBorderCharacters(`norc`),
67+
drawHorizontalLine: index => {
68+
return index === 1;
69+
}
70+
})
71+
);
72+
73+
console.log(
74+
boxen(bold(`Images optimized - ${green(filesize(totalSaved))} saved`), {
75+
padding: {
76+
left: 3,
77+
right: 3
78+
},
79+
borderColor: "green"
80+
})
81+
);
82+
}
83+
};
84+
};

license

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2019 Chris Draycott-Wheatley
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)