gulp-jst2
Version:
Compile lodash templates to a JST file using gulp; allow customisation of the prepended text while optionally using a RegEx of the file.path (full path, name and extension).
64 lines (39 loc) • 1.28 kB
JavaScript
;
var _ = require('lodash'),
printf = require('util').format,
gutil = require('gulp-util'),
through = require('through2'),
compile = _.template;
var PLUGIN_NAME = 'gulp-jst2';
module.exports = function(_options) {
var defaults = {
useFilePath: ['^.*/(.+?)(\.[^.]*$|$)', '$1'],
prepend: false
},
options = _.extend({}, defaults, _options);
var stream = through.obj(function(file, enc, cb) {
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isBuffer()) {
try {
var prependString = '';
if (options.prepend) {
prependString = printf(options.prepend, file.path.replace(new RegExp(options.useFilePath[0]), options.useFilePath[1]));
}
file.contents = new Buffer(prependString + compile(file.contents.toString(), null, options).source);
file.path = gutil.replaceExtension(file.path, ".js");
} catch(err) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, err));
}
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported.'));
return cb();
}
});
return stream;
};