-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
41 lines (33 loc) · 1.06 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
var through = require('through2'),
gutil = require('gulp-util'),
rasterize = require('./lib/converter'),
PluginError = gutil.PluginError,
phridge = require('phridge');
const PLUGIN_NAME = 'gulp-raster';
module.exports = function (opt) {
'use strict';
opt = opt || {};
opt.scale = opt.scale || 1;
opt.format = opt.format || 'png';
var phantomProcess = phridge.spawn();
return through.obj(function (file, enc, cb) {
var that = this;
// Do nothing if no contents
if (file.isNull()) { return cb(); }
if (file.isBuffer()) {
rasterize(phantomProcess, file.contents.toString(), opt.format, opt.scale, function (err, data) {
if (err) { that.emit('error', new PluginError(PLUGIN_NAME, err)); }
file.contents = data;
that.push(file);
return cb();
});
}
}).on('end', function () {
phantomProcess
.then(phridge.disposeAll)
.catch(function (err) {
phridge.disposeAll();
throw err;
});
});
};