javascriptgulpthrough2gulp-plugin

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

  • 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:

    └─┬ gulp@3.9.1
      └─┬ vinyl-fs@0.3.14
        └─┬ vinyl@0.4.6
    

    So gulp@3.9.1 depends on vinyl-fs@0.3.14 and vinyl-fs@0.3.14 depends on vinyl@0.4.6.

    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 vinyl@0.4.6 does not have a ._isVinyl property. Only newer versions like vinyl@1.2.0 have this property.

    Since gulp@3.9.1 emits vinyl files using vinyl@0.4.6 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 vinyl@1.2.0 . If you were to install that version of gulp the vinyl.isVinyl() call in your example would return true.