forked from fmal/gulp-inline-source
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
47 lines (38 loc) · 1.2 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
'use strict';
const { inlineSource } = require('inline-source');
const PluginError = require('plugin-error');
const path = require('path');
const through = require('through2');
const PLUGIN_NAME = 'gulp-inline-source';
function gulpInlineSource (options) {
return 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 PluginError(PLUGIN_NAME, 'Streaming not supported'));
return cb();
}
var fileOptions = {
rootpath: path.dirname(file.path),
htmlpath: file.path
};
if (options) {
for (var i in options) {
fileOptions[i] = options[i];
}
}
inlineSource(file.contents.toString(), fileOptions)
.then(html => {
file.contents = Buffer.from(html || '');
self.push(file);
cb();
})
.catch(err => {
self.emit('error', new PluginError(PLUGIN_NAME, err));
});
});
}
module.exports = gulpInlineSource;