gulp-cjs-tasks
Create gulp tasks using commonjs conventions
Why?
- Large gulp files are a pain
- Ability to create self documenting tasks
- Commonjs is a fine pattern for creating tasks
- Built-in sequences, rather than deps
Installation
Create a new npm
based project first. If you already have that, you can skip this step.
$ npm init
Install gulp
and gulp-cjs-tasks
dependencies.
$ npm install --save-dev gulp$ npm install --save-dev gulp-cjs-tasks
Create a gulpfile.js
file and add the following
var gulp = ;var taskLoader = ; ;
Create a ./tasks
directory, this is where you tasks will go. If you'd like your tasks
in a different directory, make sure you path reference in the gulpfile accordingly.
$ mkdir tasks
Examples
For examples, take a look at the example repository
Commonjs conventions as tasks
Simple
Let's take a simple gulp task that does nothing but console.log something. Consider something like this:
gulp;
If we dissect the above then we can conclude the following:
- we have a task name, in our case
foo
- we have a function that will be executed once
gulp foo
is called - we also have a callback function
done
that is called when the task is done
In this example the name of the task is derived from the filename: foo.js
, where
the basename is foo
, so our task name will be foo
.
// ./tasks/foo.jsmodule { return { console; ; };};
Since you might have the need to group tasks in single file, you can also export an object, in which each key is a task name, and each value is a task function.
moduleexports = { console; ; };
If you'd like to pass the gulp
object for each task module, you can export a function
rather than an object:
module { return { return gulp ; } };
Or make it more nice and neat by hosting the task function:
module { return nicer: nicer ; { return gulp ; }};
Let's add a task with a dependent task.
gulp;gulp;
We can achieve this by specifying a task object containing a task function rather than just a task function.
moduleexports = first: first second: fn: second dep: 'first' ; { console; ;} { console; ;}
Usage
This library consists of the following modules:
task-loader
- A gulp task loader which loads all tasks from a given pathhelp
- Utility for creating help on the CLI and help documentation
task-loader
var gulp = ; var taskLoader = ; ;
help
var helpUtils = gulp; // Shows help on the consolehelpUtils;
Usage gulp task [ option ... ] Tasks help : Show help one : Task one two : Task two three : Task three four : Task four one-then-two : Tasks one then two one-and-two-then-three : Tasks one and two then three one-and-two : Tasks one and two one-and-two-then-three-and-four : Tasks one and two then three and four