Myles
Myles

Reputation: 21500

Piping concat through gulp.src changes gulp.src glob

I'm still fairly new to gulp, so this might be a misunderstanding on my part.

I have gulp pipeline I'm trying to create that does something like this:

gulp.src('src/**/*.html')
    .pipe(html2js({some:options}))
    .pipe(concat('templates.js'))
    .pipe(gulp.src('src/**/*.js'))
    .pipe(uglify())
    .pipe(gulp.dest('build/'))

When I run this however, the second glob does not catch all the .js files. If I run two separate piplines as:

gulp.src('src/**/*.html')
    .pipe(html2js({some:options}))
    .pipe(concat('template.js'))
    .pipe(gulp.dest('build/'));

gulp.src(['src/**/*.js', 'build/template.js'])
    .pipe(uglify())
    .pipe(gulp.dest('build/'));

It works as expected. No errors appear to be thrown and in the first case, the template.js is added to the end of the file list as I would expect.

Any suggestions would be greatly appreciated.

Upvotes: 0

Views: 1956

Answers (2)

Justin Howard
Justin Howard

Reputation: 5643

You should check out gulp-merge. It's designed to combine gulp pipelines:

var gulpMerge = require('gulp-merge');

gulp.task('build', function() {
    return gulpMerge(
        gulp.src('src/**/*.html')
            .pipe(html2js({some:options}))
            .pipe(concat('template.js')),

        gulp.src('src/**/*.js')
    )
    .pipe(uglify())
    .pipe(gulp.dest('build/'));
});

Upvotes: 2

Ghidello
Ghidello

Reputation: 1863

Gulp.src accepts a glob and options so it can't handle the stream that's piped into it in your first sample script.

You may use an approach similar to your first one using gulp-filter:

var filter = gulpFilter('**/*.html');
gulp.src(['src/**/*.html', 'src/**/*.js'])
    .pipe(filter)
    .pipe(html2js({some:options}))
    .pipe(concat('templates.js'))
    .pipe(filter.restore())
    .pipe(uglify())
    .pipe(gulp.dest('build/'))

Otherwise, following the lines of your second sample, you can use merge-stream for merging the streams before uglifying:

var merge = require('merge-stream');

var htmlStream = gulp.src('src/**/*.html')
    .pipe(html2js({some:options}))
    .pipe(concat('template.js'));

return merge(htmlStream, gulp.src('src/**/*.js'))
    .pipe(uglify())
    .pipe(gulp.dest('build/'));

Upvotes: 1

Related Questions