Justin
Justin

Reputation: 45350

Combining multiple sources in a single gulp chain

How can I combine multiple sources in a single gulp pipe chain? For example I have:

gulp.task('compressJS', function () {
    gulp.src('client/js/source/**/*.js')
        .pipe(concat('app'))
        .pipe(ngmin())
        .pipe(uglify())
        .pipe(rev())
        .pipe(rename({
            extname: ".min.js"
         }))
        .pipe(gulp.dest('client/js'))
        .pipe(rev.manifest())
        .pipe(gulp.dest('client/js'))
        .pipe(gulp.src('client/views/index.html'))
        .pipe(replace(/app\-[a-fA-F0-9]\.min\.js/, 'app-prod.min.js'))
        .pipe(gulp.dest('client/build'))
        .pipe(gulp.src('client/js/rev-manifest.json', {
             read: false
         }))
        .pipe(rimaf());
});

Getting error:

stream.js:94
      throw er; // Unhandled stream error in pipe.

Upvotes: 1

Views: 2644

Answers (1)

NT3RP
NT3RP

Reputation: 15370

The most common solution to this problem that I have seen is by leveraging gulp-filter. The idea is that your gulp.src is a lot more broad, but your filters allow you to process the more specific subsets.

For example, using your code:

   gulpFilter = require('gulp-filter');

   gulp.task('compressJS', function () {

       jsFilter = gulpFilter('client/js/source/**/*.js');
       clientFilter = gulpFilter('client/views/index.html');

       gulp.src('client/**/*')
           .pipe(jsFilter)
           .pipe(concat('app'))
           .pipe(ngmin())
           .pipe(uglify())
           .pipe(rev())
           .pipe(rename({
               extname: ".min.js"
           }))
           .pipe(gulp.dest('client/js'))
           .pipe(rev.manifest())
           .pipe(gulp.dest('client/js'))
           .pipe(jsFilter.restore())
           .pipe(clientFilter)
           .pipe(gulp.src('client/views/index.html'))
           .pipe(replace(/app\-[a-fA-F0-9]\.min\.js/, 'app-prod.min.js'))
           .pipe(gulp.dest('client/build'))
           .pipe(gulp.src('client/js/rev-manifest.json', {
               read: false
           }))
           .pipe(clientFilter.restore())
           .pipe(rimaf());
    });

Upvotes: 2

Related Questions