Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a stream in gulp

Tags:

gulp

Basically I have a set of files that I process using markdown and what not. After doing this initial processing, I'd like to split the stream into two:

  1. First, 1..1 mapping with additional processing like layout
  2. Secondly, mapping all the files into one, like index, without the layouts applied above

Is it ok to save the stream into a variable and just keep piping? Here's my current task:

gulp.task('default', function() {
    var entries = gulp.src('./log/*.md')
        .pipe(frontMatter())
        .pipe(markdown());

    var templated = entries
        .pipe(applyTemplate())
        .pipe(gulp.dest('./build/log'));

    var index = entries
        .pipe(index())
        .pipe(applyIndexTemplate())
        .pipe(gulp.dest('./build'));

    return merge(templated, index);
}

I could use lazypipe and/or just construct the pipe multiple times, but is there another way?

like image 331
Jani Avatar asked Jan 28 '15 18:01

Jani


1 Answers

According to the Node.js docs, "multiple destinations can be piped to safely" and the original example is correct:

var entries = gulp.src('./log/*.md')
    .pipe(frontMatter())
    .pipe(markdown());

var templated = entries
    .pipe(applyTemplate())
    .pipe(gulp.dest('./build/log'));

var index = entries
    .pipe(index())
    .pipe(applyIndexTemplate())
    .pipe(gulp.dest('./build'));

return merge(templated, index);
like image 60
Jani Avatar answered Sep 29 '22 16:09

Jani