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

I found out about the gulp source code and tried to write a gulp plugin.

Now I'm embarrassed by 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 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 the files are created vinyl:

https://github.com/gulpjs/vinyl-fs/blob/master/lib/src/wrap-with-vinyl-file.js

MY ABBREVIATION:

transformFunctionregistered in though2.obj()gets the object filethat should be vinyl.

Why vinyl.isVinyl()returns false?

Why filedoes an object have no property _isVinyl?

+4
source share
1 answer

vinyl-fs vinyl, Github, vinyl-fs vinyl gulp.

, gulp npmjs.com, :

$ npm install --save-dev gulp

3.9.1 gulp. , vinyl-fs vinyl 3.9.1 gulp npm ls. () :

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

So gulp@3.9.1 vinyl-fs@0.3.14 vinyl-fs@0.3.14 vinyl@0.4.6.

GitHub:

https://github.com/gulpjs/vinyl-fs/tree/v0.3.14
https://github.com/gulpjs/vinyl/tree/v0.4.6

GitHub vinyl@0.4.6, ._isVinyl. , vinyl@1.2.0 .

gulp@3.9.1 vinyl@0.4.6, , gulp, ._isVinyl. vinyl.isVinyl() false .

gulp 4.0 vinyl@1.2.0. gulp, vinyl.isVinyl() true.

+4

Source: https://habr.com/ru/post/1652494/


All Articles