gulpjs / undertaker

Task registry that allows composition through series/parallel methods.
MIT License
200 stars 31 forks source link

Skipping null tasks #57

Closed TrySound closed 8 years ago

TrySound commented 8 years ago

It would be cool to implement accepting null values in gulp.series and gulp.parallel. My case is

gulp.task('build', gulp.series(
    env.clean ? 'clean' : null,
    gulp.parallel(
        conf.markup ? 'markup' : null,
        'script',
        'style',
        'image'
    )
));

For now I should pass something like

function (done) {
  done();
}

But I don't like to pass anonymous or named task, I just would like to skip it.

TrySound commented 8 years ago

By the way I was able to use null dependencies in gulp3

gulp.task('build', env.clean ? ['clean'] : null, function (done) {done();});
phated commented 8 years ago

I'm not sure how I feel about this. It goes against functional composition. Why not just use a named function called noop or something?

TrySound commented 8 years ago

I wouldn't like to see noop in log, caz it's nothing do. Maybe some gulp.noop() which will be ignored in log?

phated commented 8 years ago

@TrySound I've thought about this some more and I don't think this case should be supported. By "nulling" out some tasks, you will be screwing up the tasks listed by gulp --tasks because the dependency trees are built when the gulpfile is loaded and the task listing loads the gulpfile. This is something you can still do but I don't think the average user should be doing it, as it can be confusing and I don't want to adjust the API to make it easier for them to get confused.

The recommended way for this would be to put an early return in your task and maybe use gulplog to log a skipped message. You could make this into an interesting helper; maybe something like:

function skippable(condition, fn){
  if (!condition) {
    return fn;
  }

  function skipped(cb){
    cb();
  }
  skipped.displayName = (fn.name || fn.displayName) + ' (skipped)';
  return skipped;
}

function clean(cb){ ... }

gulp.task(skippable(env.clean, clean));