-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
37 lines (34 loc) · 941 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
36
37
var path = require('path');
var through = require('through2');
var compressor = require('easy-compressor');
var clone = function (obj) {
var result = {};
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
result[i] = obj[i];
}
}
return result;
};
module.exports = function (options) {
return through.obj(function (file, encoding, callback) {
var me = this;
if (file.isDirectory()) {
return callback();
}
var ext = path.extname(file).slice(1).toLowerCase();
var opt = clone(options);
if (ext) {
opt.type = ext;
}
opt.fromString = true;
compressor(String(file.contents), opt, function (err, result) {
if (err) {
throw new Error(err);
}
file.contents = new Buffer(result);
me.push(file);
callback();
});
});
};