Gulp Simple Task Loader
Easily modularize gulp tasks and minify your gulpfile. Works well with gulp-load-plugins.
Installation
$ npm install gulp-simple-task-loader --save-dev
Test
To test this package clone it and run the following commands:
$ npm install$ npm test
Usage
var taskLoader = ;;
This will load and register tasks for all files defined in your taskDirectory
.
If your tasks aren't registering (they are, but to the wrong gulp package) you can optionally specify a gulp to register the tasks to.
var gulp = ;var taskLoader = ;;
Options
You can pass in an options object as shown below. The values shown below are the defaults.
;
Task Directory
Only put gulp tasks in your taskDirectory
. All .js
files in this directory, unless the configFile
option is being used, will be attempted to be read as gulp tasks. Nested directories are supported as of v1.0.29
.
Delimiters
The purpose of the delimiters is to allow flexibility in task naming. A common convention for task names is to delimit using the :
character, however :
's are not generally used or allowed in file names. A common usage of the delimiters is as follows:
;
These options would convert a filename such as move-all.js
to a task with the name move:all
.
Plugins
Using gulp-load-plugins
You can use gulp-load-plugins to easily lazy-load your gulp plugins. Use gulp-load-plugins in conjunction with gulp-simple-task-loader to minimize your gulpfile.
'use strict'; var taskLoader = ;var plugins = ; ;
Passing in plugins manually
If not using gulp-load-plugins you must specify which plugins you want made available to your tasks.
gulpfilejs'use strict'; var taskLoader = ;var plugins = bump: mocha: ; ;
Config File
You have the option of passing in the location of a configuration file from within your task directory. Please note that the configuration file takes precedence over the config
option that you may also pass in, meaning that any key in config
is overwritten if the same key exists in the config file.
gulpfilejs'use strict'; var taskLoader = ;var config = env: 'production' ; ;
configjs'use strict'; moduleexports = // insert configuration options here;
Structuring a task
All tasks should be functions that receive the parameters gulp
, config
, and plugins
.
There are 2 ways to structure a task -- returning a function that executes the task, or returning an object that contains dependencies, parameters, and the function that executes the task.
Basic tasks
This is a typical function as you are used to with gulp.
'use strict'; module { return {}; // the task functionality -- callback optional};
Tasks with dependencies and/or parameters
All 3 object keys (deps
, params
, and fn
) are optional. This allows you to create a task that strictly calls other tasks, a task that is parameterized, or a task that just acts like a normal task.
If there are no dependencies or parameters for the task you can use the above "Basic task" format for creating a basic task.
The values shown below are the defaults.
'use strict'; module { return deps: // an array of task names to execute before this task params: // an array of parameters to send to `fn` {} // the task functionality -- callback optional ;};
Please note that if you use the params
key your fn
must be of the following form:
params: 'a' 'b' {} // where param is an item from the params array, // and cb is a callback to be called at the end of your function
Complete examples
Using gulp-load-plugins
gulpfilejs 'use strict'; var taskLoader = ;var plugins = ; ;
tasks/lint-alljs 'use strict'; module { return deps: 'lint:client' 'lint:server' ;};
tasks/lint-clientjs 'use strict'; module { return { return gulp ; };};
tasks/lint-serverjs 'use strict'; module { return { return gulp ; };};
Parameterize tasks
gulpfilejs 'use strict'; var taskLoader = ;var plugins = ; ;
tasks/parameterizedjs 'use strict'; module { return params: '1' '2' { console; ; // note that the callback must be called in order for the task // to finish iterating through your params array } ;};
The task in parameterized.js
would produce the following output:
12
Changelog
Documented below are any significant changes to the package.
- 1.x.x
- 1.3.x
- 1.3.0 - added feature for passing in configuration file github issue #6; restructured test suite to section out functionality
- 1.2.x
- 1.1.x
- 1.1.6 - added documentation for testing the package
- 1.1.4 - improved documentation for parameterized tasks
- 1.1.0 - added functionality to parameterize tasks - github issue #9
- 1.0.x
- 1.0.35 - added support for coffeescript - @chafnan
- 1.0.33 - fixed github issue #8
- 1.0.29 - added functionality to allow for recursive directory task searching
- 1.0.17 - added documentation for calling the plugin; added documentation for complete examples
- 1.0.16 - created README.md
- 1.0.15 - added dependency support for tasks
- 1.0.14 - implemented first set of tests
- 1.3.x