javascriptnode.jsgulpnode-streamsvinyl

How to use objects list as gulp source stream


I know that gulp require a vinyl source stream to work properly, but is there an easy way to use an already existant vinyl file or a json object instead of the well known gulp.src that only takes globs?


Solution

  • After several researches I did not found any good solution for that problem so I decided to share my solution with you.

    That problem is solved in 2 steps

    1: You need to convert what ever you want to pipe in gulp to a vinyl file. This can be done like so

    const Vinyl = require('vinyl');
    var vinyl = new Vinyl();
    vinyl.cwd = '/';
    vinyl.base = '/';
    vinyl.path = '/yourfictivefilepath';
    vinyl.contents = new Buffer(JSON.stringify(yourobject));
    

    For more information about that step: https://github.com/gulpjs/vinyl

    2: Create a stream out of your vinyl files

    I found that those steps can be repetitive and can make code less readable so I decided to encapsulate those steps in an npm package that does this for you.

    https://www.npmjs.com/package/gulp-to-stream