1
我想寫一個吞嚥插件,它會計算流中文件的數量。 I used this as a starting point:如何在任務完成時告訴Gulp?
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
插件?
'return gulp ...'效果很好! – 2014-12-06 15:42:11
我對原始資料所做的更改是否正確?我改變了它,所以我可以多次使用該插件。我應該繼續使用'through'還是'through2'? – 2014-12-06 15:56:55
如果你在意切換到through2,那可能沒關係。如果他們工作,變化是好的! – OverZealous 2014-12-06 16:18:13