-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
51 lines (41 loc) · 1.47 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
const through = require('through2');
const log = require('fancy-log');
const PluginError = require('plugin-error');
const pngquant = require('pngquant-bin');
const execFileSync = require('child_process').execFileSync;
const spawnSync = require('child_process').spawnSync;
const chalk = require('chalk');
// consts
const PLUGIN_NAME = 'gulp-pngquant';
// plugin level function (dealing with files)
function gulpPngquant(options, customPngquantPath) {
var opts = [];
for(key in options) {
opts.push('--' + key, options[key]);
}
opts.push('-');
// creating a stream through which each file will pass
var stream = through.obj(function(file, enc, cb) {
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!'));
return cb();
}
if (file.isBuffer()) {
console.log(chalk.blue('pngquant compressing: ') + file.relative);
file.contents = spawnSync(customPngquantPath || pngquant, opts, {
input: file.contents
}).stdout;
}
// make sure the file goes through the next gulp plugin
this.push(file);
// tell the stream engine that we are done with this file
cb();
}, function(flush) {
log('Finished', '\'' + chalk.cyan(PLUGIN_NAME));
flush();
});
// returning the file stream
return stream;
};
// exporting the plugin main function
module.exports = gulpPngquant;