'Why does vinyl.isVinyl() return false for vinyl files emitted by gulp?

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?



Solution 1:[1]

This is a matter of which versions of vinyl-fs and vinyl you look at on Github and which versions of vinyl-fs and vinyl your local gulp installation is using.

You probably installed gulp from npmjs.com by typing:

$ npm install --save-dev gulp

This currently installs version 3.9.1 of gulp. You can see which versions of vinyl-fs and vinyl the 3.9.1 version of gulp depends on by using npm ls. Here's the (abbreviated) output from that command:

??? [email protected]
  ??? [email protected]
    ??? [email protected]

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 the vinyl.isVinyl() function returns false 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 the vinyl.isVinyl() call in your example would return true.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community