Exports as tasks
Created by: jamiebuilds
One of the things I've started doing in all my gulpfiles is to write tasks as plain functions and then write gulp.task at the end of the file. This makes abstraction a bit easier and avoids using IDs for composition instead of just functions, making writing large complex gulpfiles much much easier to follow.
function test() { ... }
gulp.task('test', test);
This gets a lot easier with gulp 4 using series and parallel.
function test() { ... }
function buildFiles() { ... }
var build = gulp.series(test, buildFiles);
gulp.task('test', test);
gulp.task('build', build);
But at this point writing out gulp.task seems a bit unnecessary since the id isn't being used in the code and is only there to export a set of tasks.
I'd prefer just to write:
exports.test = test;
exports.build = build;
Now this gets even more interesting with ES2015 because you can write:
export function test() { ... }
export function build() { ... }
One of the cool benefits here is that you can use static analysis to determine which tasks the gulpfile defines and use it for stuff like CLI autocompletion:
$ gulp [tab]
- test
- build
- dev
But the bigger benefit here is that it encourages people to write gulp tasks as plain functions that return streams, making them far more generic and simplifying the gulp API greatly.
This is what gulpfiles could look like in the future: https://gist.github.com/thejameskyle/2778e4df363612635989
How would you all feel about allowing tasks to be defined as exports?