Skip to content

Instantly share code, notes, and snippets.

@qrg
Created March 24, 2016 11:25
Show Gist options
  • Select an option

  • Save qrg/28a15390f92719536d02 to your computer and use it in GitHub Desktop.

Select an option

Save qrg/28a15390f92719536d02 to your computer and use it in GitHub Desktop.
'use strict';
const through = require('through2');
const yaml = require('js-yaml');
const ejs = require('ejs');
const gutil = require('gulp-util');
module.exports = (template) => {
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit(
'error',
new gutil.PluginError('gulp-ejs', 'Streaming not supported')
);
}
const data = yaml.safeLoad(file.contents.toString());
file.path = gutil.replaceExtension(file.path, '.html');
try {
file.contents = new Buffer(
ejs.render(template, data)
);
} catch (err) {
this.emit('error', new gutil.PluginError('gulp-ejs', err.toString()));
}
this.push(file);
cb();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment