How to perform multiple gulp commands in one task

2019-03-11 02:58发布

问题:

I'm having a hard time to understand on how to process multiple gulp sources in a single task. In a task like this:

gulp.task('task1', function (cb) {
    gulp.src('src/js/**/*').pipe(gulp.dest('dist'));
    gulp.src('src/css/**/*').pipe(gulp.dest('dist'));
    ...
});

I would like to process all the different source files and then mark the task as finished, so the others tasks can depend on it's completion.

I'm aware of the possibility, to using individual tasks for each individual source but this would make everything more complicated and bloat the orchestrator with a huge number of tasks that are actually not needed individually.

回答1:

You can pass an array of globs to gulp.src if you are doing the same things to all files. For example,

gulp.task('task1', function () {
  return gulp.src(['src/js/**/*', 'src/css/**/*']).pipe(gulp.dest('dist'));
});

Be sure to return the stream so the orchestrator knows when that task is complete.

If you are doing different things to the different sets of files, you can merge the streams like this in one task:

var es = require('event-stream');
gulp.task('fancy-task', function () {
  return es.merge(
      gulp.src('*.js').pipe(some-js-plugin()),
      gulp.src('*.css').pipe(some-style-plugin))
      .pipe(gulp.dest('dist'));
});


标签: gulp