László
László

Reputation: 872

Creating a single file from multiple streams while keeping the streams and files order with Gulp

I have a Gulp task where I would like to compile HTML templates to javascript (lets say from /views directory), add all other javascript sources to this (from /js directory) and concatenate all of them into one single file.

Its also important to have the files concatenated in the correct order, meaning: all files from /js then all compiled files from /views.

My goal is to avoid creating temporary files in general, so the solution I seek only works with streams.

This task currently works without correctly ordering the output:

var eventstream = require('event-stream');
gulp.task('dist', function()
{
    return eventstream.merge(
        gulp.src(['**/[^_]*.js', '!**/_*/**'], { cwd: 'js' })
    ,
        gulp.src(['**/[^_]*.html', '!**/_*/**'], { cwd: 'views' })
        .pipe(compile_views())
    )
    .pipe(concat('output.js'))
    .pipe(gulp.dest('dist'));
});

Tried a few stream merging modules from NPM with less success, all of them scrambled my output so far:

Also tried using the gulp-order plugin after merging the streams, which still does not ordered it correctly.

Did I miss something, or am I doing something the bad way? I'm not so expert in pipes yet.

Any help would be appreciated!

Thanks!

Upvotes: 0

Views: 582

Answers (1)

Sven Schoenung
Sven Schoenung

Reputation: 30564

I think streamqueue is what you're looking for:

var streamqueue = require('streamqueue');

gulp.task('dist', function() {
  return streamqueue({ objectMode: true },
    gulp.src(['**/[^_]*.js', '!**/_*/**'], { cwd: 'js' }),
    gulp.src(['**/[^_]*.html', '!**/_*/**'], { cwd: 'views' })
      .pipe(compile_views())
  )
  .pipe(concat('output.js'))
  .pipe(gulp.dest('dist'));
});

Upvotes: 1

Related Questions