【发布时间】:2015-02-04 13:41:47
【问题描述】:
我正在尝试编写一个 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 插件吗?
【问题讨论】:
标签: javascript node.js gulp node-modules