Skip to content

Instantly share code, notes, and snippets.

@venning
Created October 25, 2015 20:40
Show Gist options
  • Save venning/3346d78d1e6a27a2ebdd to your computer and use it in GitHub Desktop.
Save venning/3346d78d1e6a27a2ebdd to your computer and use it in GitHub Desktop.
Handlebars-based version of gulp-template
'use strict';
var through = require('through2');
var PluginError = require('gulp-util').PluginError;
var Handlebars = require('handlebars');
var PLUGIN_NAME = 'gulp-handlebars-template';
// partly taken from: https://github.com/gulpjs/gulp/blob/master/docs/writing-a-plugin/guidelines.md
// partly taken from: https://github.com/sindresorhus/gulp-template/blob/master/index.js
module.exports = function (context) {
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
return cb(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
}
var template = Handlebars.compile(file.contents.toString());
file.contents = new Buffer(template(context));
cb(null, file);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment