Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gulp.on('data') How to pass data to next pipe

Tags:

gulp

how to pass the data back from gulp.on('data') to the next step/pipe for eg. .pipe gulp.dest Here is a sample code in coffee

gulp.src(src)
    .on 'data',->
            # make change to file object
            # pass back the changed file object ?? how to pass it back to the next stream
    .pipe gulp.dest(dest)
like image 577
coool Avatar asked Apr 03 '14 11:04

coool


1 Answers

Take a look at the Writing Plugins docs.

What you want is to create a transformation stream. Take a look at this Stream Handbook. In your case, you want to map the stream and change it as it goes. The easiest way would be (in JS):

gulp.src(src)
  .pipe(makeChange())
  .pipe(gulp.dest(dest));

function makeChange() {
  // you're going to receive Vinyl files as chunks
  function transform(file, cb) {
    // read and modify file contents
    file.contents = new Buffer(String(file.contents) + ' some modified content');

    // if there was some error, just pass as the first parameter here
    cb(null, file);
  }

  // returning the map will cause your transform function to be called
  // for each one of the chunks (files) you receive. And when this stream
  // receives a 'end' signal, it will end as well.
  // 
  // Additionally, you want to require the `event-stream` somewhere else.
  return require('event-stream').map(transform);
}
like image 76
Caio Cunha Avatar answered Oct 15 '22 08:10

Caio Cunha