-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
125 lines (113 loc) · 3.24 KB
/
gulpfile.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
125
/*Gulp JS code*/
const gulp = require("gulp"),
del = require("del"),
util = require("util"),
gulpif = require("gulp-if"),
exec = util.promisify(require("child_process").exec),
imageminMozjpeg = require("imagemin-mozjpeg"),
zopfli = require("imagemin-zopfli"),
parallel = require("concurrent-transform"),
combiner = require("stream-combiner2"),
filesize = require("filesize"),
fs = require("fs"),
os = require("os"),
$ = require("gulp-load-plugins")(), //load all gulp plugins
cfg = "./config.json",
defConfig = {
in_dir: "./in",
out_dir: "./out",
width: 1024,
height: 1024,
createWebP: false,
watermark: "./watermark.png",
};
function fileExists(path) {
try {
return fs.statSync(path).isFile();
} catch (e) {
if (e.code == "ENOENT") {
// no such file or directory. File really does not exist
//console.log("File does not exist.");
return false;
}
//console.log("Exception fs.statSync (" + path + "): " + e);
throw e; // something else went wrong, we don't have rights, ...
}
}
let userConfig = {},
config = {};
//Load config from config file
if (fileExists(cfg)) {
userConfig = JSON.parse(fs.readFileSync(cfg));
}
Object.assign(config, defConfig, userConfig);
//special check for watermark file
if (config.watermark.length && !fileExists(config.watermark)) {
//skip watermark step
config.watermark = "";
}
let SRC = config.in_dir + "/**/*.{jpg,jpeg,png,svg,gif}",
DEST = config.out_dir;
/*image processing pipeline
- Resize the image.
- Minify the image with imagemin
- Mozjpeg compression is used for jpg files
*/
const processImages = combiner.obj(
$.imageResize({
width: config.width,
height: config.height,
}),
gulpif(
config.watermark.length,
$.watermark({
image: config.watermark,
resize: "100x100",
})
),
$.imagemin([
$.imagemin.gifsicle({ interlaced: true }),
$.imagemin.mozjpeg({ progressive: true }),
$.imagemin.optipng({ optimizationLevel: 5 }),
$.imagemin.svgo({ plugins: [{ removeViewBox: true }] }),
imageminMozjpeg({ progressive: true }),
zopfli({ more: true }),
])
);
/*eslint-disable no-console */
processImages.on("error", console.error.bind(console));
/*eslint-enable no-console */
gulp.task("testGM", () => {
//GraphicsMagick is one of the required binary/library. Check for it's presence
return exec("gm version");
});
gulp.task("clean", () => {
return del(config.out_dir + "/**/*"); //delete all destination files, just to be clean
});
gulp.task("processImg", () => {
// place code here
return gulp
.src(SRC, { nocase: true, base: config.in_dir })
.pipe($.plumber()) //node stream related error handling
.pipe($.changed(DEST))
.pipe($.sizediff.start())
.pipe(parallel(processImages, os.cpus().length))
.pipe(gulp.dest(DEST))
.pipe(gulpif(config.createWebP, $.webp()))
.pipe(gulp.dest(DEST))
.pipe(
$.sizediff.stop({
title: "Saved",
formatFn: (data) => {
return (
filesize(data.diff) +
" (" +
(100 - Math.round((data.diffPercent || 1) * 100)) +
"%)"
);
},
})
)
.pipe($.plumber.stop());
});
gulp.task("default", gulp.series("testGM", "processImg"));