如何在任务完成时告诉Gulp?

时间:2014-12-06 12:59:05

标签: javascript node.js gulp node-modules

我正在尝试编写一个gulp插件,它将计算流中的文件数。 I used this as a starting point

function count() {

  var count = 0;

  function countFiles(data) {
    count++;
    // added this as per official docs:
    this.queue(data);
  }

  function endStream() {
    console.log(count + " files processed");
    // not doing this as per original post, as it "ends" the gulp chain:
    //this.emit("end");
    // so doing this instead as per official docs:
    this.queue(null);
  }

  return through(countFiles, endStream);
}


module.exports = count;

这是一个示例任务:

gulp.task("mytask", function () {
  gulp
    .src("...files...")
    .pipe(count());                // <--- here it is
    .pipe(changed("./some/path"))
    .pipe(uglify())
    .pipe(rename({ extname: ".min.js" }))
    .pipe(gulp.dest(./some/path))
    .pipe(count());                // <--- here it is again
});

它完美无缺,但它没有按预期开始/结束:

[14:39:12] Using gulpfile c:\foo\bar\baz\gulpfile.js
[14:39:12] Starting 'mytask'...
[14:39:12] Finished 'mytask' after 9.74 ms
9 files processed
5 files processed

这意味着事物以异步方式运行并在任务完成后结束。文档说您必须使用回调或返回流。它似乎正在这样做。

如何使此功能运行?是因为我使用的是through而不是through2插件?

1 个答案:

答案 0 :(得分:2)

return放在gulp之前的任务中。您还没有任何方法可以知道您的信息流何时结束。

相关问题