I am learning about the gulp source code and tried to write a gulp plugin.
Now I am confused about something.
This is my plugin code below:
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);
})
}
This is the part of the gulp source code where vinyl
files are created:
https://github.com/gulpjs/vinyl-fs/blob/master/lib/src/wrap-with-vinyl-file.js
MY CONFUSION:
The transformFunction
registered with though2.obj()
receives a file
object that should be a vinyl
file.
Why does vinyl.isVinyl()
return false
?
Why doesn't the file
object have a _isVinyl
property?
This is a matter of which versions of
vinyl-fs
andvinyl
you look at on Github and which versions ofvinyl-fs
andvinyl
your localgulp
installation is using.You probably installed gulp from
npmjs.com
by typing:This currently installs version
3.9.1
of gulp. You can see which versions ofvinyl-fs
andvinyl
the3.9.1
version of gulp depends on by usingnpm ls
. Here's the (abbreviated) output from that command:So
[email protected]
depends on[email protected]
and[email protected]
depends on[email protected]
.Here are links to those version on GitHub:
https://github.com/gulpjs/vinyl-fs/tree/v0.3.14
https://github.com/gulpjs/vinyl/tree/v0.4.6
As you can see on GitHub
[email protected]
does not have a._isVinyl
property. Only newer versions like[email protected]
have this property.Since
[email protected]
emits vinyl files using[email protected]
the vinyl files emitted by your gulp installation don't have the._isVinyl
property. And that's why thevinyl.isVinyl()
function returnsfalse
in your example.The current development version for the upcoming gulp 4.0 uses
[email protected]
. If you were to install that version of gulp thevinyl.isVinyl()
call in your example would returntrue
.