forked from romeovs/gulp-zopfli
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
94 lines (82 loc) · 2.34 KB
/
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
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
'use strict';
var through2 = require('through2');
var PluginError = require('gulp-util').PluginError;
var bufferMode = require('./lib/buffer');
var streamMode = require('./lib/stream');
var defaults = require('defaults');
var bytes = require('bytes');
var PLUGIN_NAME = 'gulp-zopfli';
/**
*
* @param {Object} opts
* @param {String} opts.format gzip || deflate || zlib
* @param {Number} opts.thresholdRatio if compression ratio does not reach the threshold,
* does not compress, default to 0
* @param {String} opts.thresholdBehavior if compression ratio does not reach the threshold,
* "original" to output original file, or
* "blank" (default) to output nothing
* @returns {Stream}
*/
module.exports = function(options) {
options = options || {};
defaults(options, {
format: 'gzip',
append: true,
threshold: false,
zopfliOptions: {}
});
if(options.threshold) {
if(typeof options.threshold != 'number') {
if(typeof options.threshold == 'string') {
options.threshold = bytes(options.threshold) || 150;
} else {
options.threshold = 150;
}
}
options.threshold = Math.max(1, options.threshold);
}
var ext = '';
if(options.append) {
if(options.format === 'gzip') {
ext = '.gz';
} else if(options.format === 'deflate') {
ext = '.deflate';
} else if(options.format === 'zlib') {
ext = '.zz';
}
}
var stream = through2.obj(compress);
stream.options = options;
function compress(file, enc, done) {
/*jshint validthis: true */
var self = this;
// Check for empty file
if (file.isNull()) {
// Pass along the empty file to the next plugin
this.push(file);
done();
return;
}
// Call when finished with compression
var finished = function(err, contents, wasCompressed) {
if (err) {
var error = new PluginError(PLUGIN_NAME, err, { showStack: true });
self.emit('error', error);
done();
return;
}
if (options.append && wasCompressed) file.path += ext;
file.contents = contents;
self.push(file);
done();
return;
};
// Check if file contents is a buffer or a stream
if(file.isBuffer()) {
bufferMode(file.contents, options, finished);
} else {
streamMode(file.contents, options, finished);
}
}
return stream;
};