任务完成后如何告诉Gulp?

我正在尝试编写一个gulp插件来计算stream中文件的数量。 我以此为出发点 :

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 

这意味着这个东西是asynchronous运行的,并在任务完成后结束。 文档说,你必须使用callback或返回一个stream。 这似乎就是这样做的。

我如何使这个function的行为? 是因为我使用through而不是through2插件吗?

把你的任务放在gulp 。 您还没有提供任何方式让您了解您的信息stream何时完成。