gulp.on('data')如何将数据传递给下一个管道

时间:2014-04-03 11:04:55

标签: gulp

如何将数据从gulp.on('data')传递回下一步/管道,例如。 .pipe gulp.dest 以下是咖啡中的示例代码

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)

1 个答案:

答案 0 :(得分:22)

查看Writing Plugins docs

您想要的是创建转换流。看一下这个Stream Handbook。在您的情况下,您希望map流并随着时间的推移进行更改。最简单的方法是(在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);
}