-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulp-entwine.js
35 lines (30 loc) · 988 Bytes
/
gulp-entwine.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 through = require('through2');
const PluginError = require('gulp-util').PluginError;
function entwine(transformer, options) {
const PLUGIN_NAME = options.pluginName || 'entwine';
return through.obj(function(file, encoding, callback) {
try {
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
let completeString = '';
let chunk;
while (null !== (chunk = file.contents.read())) {
completeString += chunk;
}
const transformed = transformer(completeString);
file.contents = through();
file.contents.write(transformed);
return callback(null, file);
} else if (file.isBuffer()) {
file.contents = new Buffer(transformer(file.contents.toString('utf-8')));
return callback(null, file);
}
} catch (e) {
this.emit('error', new PluginError(PLUGIN_NAME, e.message));
}
});
}
module.exports = entwine;