Skip to content

Instantly share code, notes, and snippets.

@brianbancroft
Forked from CITguy/custom-task.js
Created April 16, 2019 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianbancroft/83f9af65cfe34572c6686223dee53d96 to your computer and use it in GitHub Desktop.
Save brianbancroft/83f9af65cfe34572c6686223dee53d96 to your computer and use it in GitHub Desktop.
Basic pattern for creating a custom Transform stream for use with gulp tasks.
var gulp = require('gulp');
var myTransform = require('./myTransform');
gulp.task('foobar', function (){
return gulp.src("foobar.js")
.pipe(myTransform())
.pipe(gulp.dest('.'));
});
var through = require('through2');
module.exports = function () {
// return a `through2` stream for `pipe()` compatibility at the node level
return through.obj(function (vinylFile, encoding, callback) {
// 1. clone new vinyl file for manipulation
// (See https://github.com/wearefractal/vinyl for vinyl attributes and functions)
var transformedFile = vinylFile.clone();
// 2. set new contents
// * contents can only be a Buffer, Stream, or null
// * This allows us to modify the vinyl file in memory and prevents the need to write back to the file system.
transformedFile.contents = new Buffer("whatever");
// 3. pass along transformed file for use in next `pipe()`
callback(null, transformedFile);
});
}//myTransform()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment