forked from fmal/gulp-inline-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (38 loc) · 1.17 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
var inlineSource = require('inline-source'),
gutil = require('gulp-util'),
through = require('through2');
const PLUGIN_NAME = 'gulp-inline-source';
function gulpInlineSource (options) {
'use strict';
var stream = through.obj(function (file, enc, cb) {
var self = this;
if (file.isNull() || file.isDirectory()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
return cb();
}
var fileOptions = {
rootpath: file.base,
htmlpath: file.path
};
if (options) {
for (var i in options) {
fileOptions[i] = options[i];
}
}
inlineSource(file.contents.toString(), fileOptions, function (err, html) {
if (err) {
self.emit('error', new gutil.PluginError(PLUGIN_NAME, err));
} else {
file.contents = new Buffer(html || '');
self.push(file);
}
cb();
});
});
return stream;
}
module.exports = gulpInlineSource;