Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a stream between two gulp tasks?

Tags:

gulp

I haven't been able to find any documentation of how one does this in gulp, not even sure one should do something like this:

var fileStream;

gulp.task('lint', ['glob'], function () {
    var jshint = require('gulp-jshint');

    fileStream.pipe(jshint())
       .pipe(jshint.reporter('default'));    

});

gulp.task('glob', function () {
    fileStream = gulp.src([
        './js/**/*.js',
        '!**/node_modules/**'
    ]);
});

Is this the best way to do something like this? Is there a nicer way to 'pass' the stream returned/created by 1 task into some other task(s) so that for something like this we don't duplicate the globbing while still making 'lint' & 'glob' distinct tasks. Is this even desirable in Gulp?

Thanks!

like image 724
user2217522 Avatar asked Mar 21 '14 17:03

user2217522


1 Answers

Very common pattern in gulp builds

  • patterns are gathered into a single paths variable
  • each task begins with a "src" plugin (very often gulp.src) and ends with a "sink" plugin (gulp.dest, or like in your example, a reporter).

So for your example:

var paths = {
    scripts:[
        './js/**/*.js',
        '!**/node_modules/**'
    ],
    css: './css/**'
};

gulp.task('lint', function () {
    var jshint = require('gulp-jshint');

    var stream = gulp.src(paths.scripts)
        .pipe(jshint())
        .pipe(jshint.reporter('default'));

    return stream;
});

gulp.task('cssmin', function () {
    var stream = gulp.src(paths.css)
        .pipe(cssmin())
        .pipe(gulp.dest("..."));

    return stream;
});

Why not share streams between tasks?

Gulp's task orchestrator (and soon a new one coming for Gulp 4) is dealing with task dependencies and their execution. Remember, those tasks can be async, so you don't want to share a stream that can be used by multiple consumers!

like image 193
Brian Clozel Avatar answered Oct 20 '22 01:10

Brian Clozel