forked from fatso83/gulp-kit
/
index.js
47 lines (38 loc) · 1.23 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
var kit = require('node-kit');
var through2 = require('through2');
var PluginError = gutil.PluginError;
var path = require('path');
var partialPrefix = '_';
var replaceExt = require('replace-ext');
function isPartial(filepath) {
return path.basename(filepath)[0] === partialPrefix;
}
module.exports = function (options) {
options = options || {};
options.variables = options.variables || {};
options.forbiddenPaths = options.forbiddenPaths || [];
function transform (file, enc, next) {
var self = this;
if (file.isNull()) {
this.push(file); // pass along
return next();
}
if(isPartial(file.path) && !options.compilePartials) {
return next();
}
if (file.isStream()) {
this.emit('error', new PluginError('gulp-kit', 'Streaming not supported'));
return next();
}
try {
var html = new kit.Kit(file.path, options.variables, options.forbiddenPaths).toString();
file.contents = new Buffer.from(html);
file.path = (options.fileExtension) ? replaceExt(file.path, options.fileExtension) : replaceExt(file.path, '.html');
self.push(file);
} catch( e ) {
self.emit('error', new PluginError('gulp-kit', e));
}
next();
}
return through2.obj(transform);
};