Skip to content

Instantly share code, notes, and snippets.

@lyzadanger
Last active August 29, 2015 14:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyzadanger/ef133432adfd30b7c9eb to your computer and use it in GitHub Desktop.
Save lyzadanger/ef133432adfd30b7c9eb to your computer and use it in GitHub Desktop.
Template for gulp plugin using through2
var through = require('through2');
var gutil = require('gulp-util');
// Anything in this scope will execute once on `require`
module.exports = function thisIsMyGulpPlugin() {
// Do some stuff here if you want
// Anything at this scope level will execute once per
// task-level stream (i.e. every time a task is invoked
// and the stream piped through this plugin)
return through.obj(function(file, enc, cb) {
// Anything at this scope level will execute once
// per file in the stream, every time
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
// If you don't support streams
cb(new gutil.PluginError('myPlugin', 'Streaming not supported'));
return;
// Otherwise do stuff
}
if (file.isBuffer()) {
// If you don't support buffers
cb(new gutil.PluginError('myPlugin', 'Buffer not supported'));
return;
// Otherwise do stuff, e.g.
// file.contents = new Buffer('something to replace my file contents with');
}
// Do stuff
this.push(file);
cb();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment