-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulp-sharp.js
39 lines (36 loc) · 993 Bytes
/
gulp-sharp.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
const through = require("through2");
const gulputil = require("gulp-util");
const sharp = require("sharp");
module.exports = (options) =>
through.obj((file, encoding, callback) => {
if (file.isNull()) {
callback(null, file);
return;
}
if (file.isStream()) {
callback(new gulputil.PluginError("gulp-sharp", "Streams un supported"));
}
if (file.isBuffer()) {
const payload = {};
if (options.width && typeof options.width === "number") {
payload.width = options.width;
}
if (options.height && typeof options.height === "number") {
payload.height = options.height;
}
sharp(file.contents)
.resize(payload)
.toBuffer()
.then((data) => {
callback(
null,
new gulputil.File({
base: file.base,
cwd: file.cwd,
path: file.path,
contents: data,
})
);
});
}
});