Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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")
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 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);
Copy link

Manc commented Jul 5, 2019


Copy link

SteinTheRuler commented Aug 4, 2019

just what I needed. I was complicating it with arrays and streams. much cleaner this way

Copy link

Keuter commented Feb 28, 2020

Great! I wrote a little gulp-wrapper for it, see:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment