Last active
August 29, 2015 14:11
-
-
Save lyzadanger/ef133432adfd30b7c9eb to your computer and use it in GitHub Desktop.
Template for gulp plugin using through2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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