我正在学习 gulp 源代码并尝试编写一个 gulp 插件。
现在我对某事感到困惑。
这是我的插件代码如下:
module.exports = function(){
return through2.obj(function(file,encode,callback){
console.log(vinyl.isVinyl(file));//false
console.log(file._isVinyl) // undefined
// the reason ? file is not Object of vinyl ? file's property of '_isVinyl' is undefine ?
if(file.isNull()){
callback(null,file);
}
if(file.isStream()){
file.contents = file.contents.pipe(through2(function(chuck,encode,callback){
if(util.isNull(chuck)){
callback(null, chuck);
}
if(util.isBuffer(chuck)){
chuck = new Buffer(String(chuck)
.replace(commentReg, '')
.replace(blankSpaceReg,''))
}
callback(null,chuck);
}));
}
if(file.isBuffer()){
file.contents = new Buffer(String(file.contents)
.replace(commentReg, '')
.replace(blankSpaceReg,''));
}
callback(null,file);
})
}
vinyl
这是创建文件的 gulp 源代码的一部分:
https://github.com/gulpjs/vinyl-fs/blob/master/lib/src/wrap-with-vinyl-file.js
我的困惑:
transformFunction
注册者though2.obj()
接收一个file
应该是vinyl
文件的对象。
为什么vinyl.isVinyl()
返回false
?
为什么file
对象没有_isVinyl
属性?