How to use a list of objects as a gulp stream source

I know gulp requires the vinyl source stream to work correctly, but is there an easy way to use an existing vinyl file or json object instead of the famous gulp.src that only accepts globes?

+6
source share
2 answers

After several studies, I did not find a suitable solution for this problem, so I decided to share my solution with you.

This problem is solved in 2 steps.

1: you need to convert what you want to connect in gulp to a vinyl file. This can be done like this:

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

: https://github.com/gulpjs/vinyl

2:

, , npm, .

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

+6

gulp (, .base), , gulp. :

'use strict';
const gulp = require('gulp');
const header = require('gulp-header');//https://www.npmjs.com/package/gulp-header

gulp.task('default', function () {
    gulp.src("a.json")//empty file
        .pipe(header(JSON.stringify({a:1,b:2})))//inject your own content
        .pipe(gulp.dest("dist/"))
});

a.json . gulp-header , .

(a.json), :

'use strict';
const gulp = require('gulp');
const transform = require('gulp-transform');
const rename = require("gulp-rename");

gulp.task('default', function () {
    return gulp.src("./gulpfile.js")
        .pipe(transform(() => JSON.stringify({a:1,b:2})))
        .pipe(rename("a.json"))
        .pipe(gulp.dest("dist/"))
});
+1

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


All Articles