我正在尝试学习Gulp。我在下面创建了我认为可能会失败的代码,但是它可以正常工作,我想知道为什么吗? “可写”以作为缓冲区的“ gulp.src(PATHS.src)”的形式接收输入,Node.js文档说它应该接收可读流(“ read.pipe(destination [,options])”),因此,“ gulp.src(PATHS.src).pipe(可写);”应该会失败,但是可以!!!为什么?
var gulp = require('gulp');
var through = require('through2');
var connect = require('gulp-connect');
var fs = require('fs');
var PATHS = {
src : 'src/**/*.ts',
html : 'src/**/*.html',
css : 'src/**/*.css'
};
gulp.task('test', function() {
// This is working !!!
/*
* fs.createReadStream('ex.txt').pipe(through(function(chunk, enc, callback)
{
* console.log("Text is: " + chunk.toString());
*
* this.push(chunk)
*
* callback() })).pipe(fs.createWriteStream('out.txt')).on('finish',
* function() { console.log("Kocham Rencie"); })
*/
// But this is not working, why !!!
var writable = through.obj(function(chunk, enc, callback) {
if (chunk.isStream()) {
console.log("Chunk is stream ");
console.log("Path is: " + chunk.path);
} else if (chunk.isBuffer()) {
console.log("Path is:"+chunk.path);
console.log("Chunk is buffer ");
console.log("***");
console.log(chunk.contents.toString());
console.log("***");
} else {
console.log("Chunk is something else then stream or buffer");
}
this.push(chunk);
callback(null);
});
gulp.src(PATHS.src).pipe(writable);
});