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

coo*_*ool 14 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)
Run Code Online (Sandbox Code Playgroud)

Cai*_*nha 22

看看Writing Plugins文档.

你想要的是创建一个转换流.看看这本Stream手册.在您的情况下,您想要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);
}
Run Code Online (Sandbox Code Playgroud)

  • 通过使用`gulp-tap`插件,它可能会更短一些. (3认同)