-
Notifications
You must be signed in to change notification settings - Fork 73
/
index.js
35 lines (26 loc) · 801 Bytes
/
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
'use strict';
const gzip = require('zlib').gzip;
const defaults = {
threshold: false,
options: {},
ext: 'gz'
};
module.exports = function (task, utils) {
const zipper = utils.promisify(gzip);
task.plugin('gzip', {}, function * (file, opts) {
opts = Object.assign({}, defaults, opts);
// if there is a threshold && we don't meet it, exit
if (typeof opts.threshold === 'number' && Buffer.byteLength(file.data) < opts.threshold) {
return;
}
// clone the file object
// @todo: `opts.replace`
let clone = Object.assign({}, file);
// modify the file extension
clone.base += (opts.ext.charAt(0) === '.') ? opts.ext : `.${opts.ext}`;
// compress & set data
clone.data = yield zipper(clone.data, opts.options);
// add to files array
this._.files.push(clone);
});
};