1.3.11 • Published 6 years ago

gulp-simple-task-loader v1.3.11

Weekly downloads
115
License
MIT
Repository
github
Last release
6 years ago

Gulp Simple Task Loader

NPM

npm version Coverage Status Build Status Dependency Status Codacy Badge

Pull Requests Status Issues Status

Easily modularize gulp tasks and minify your gulpfile. Works well with gulp-load-plugins.

$ npm install gulp-simple-task-loader --save-dev

To test this package clone it and run the following commands:

$ npm install
$ npm test
var taskLoader = require('gulp-simple-task-loader');
taskLoader(options);

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 = require('gulp');
var taskLoader = require('gulp-simple-task-loader');
taskLoader(options, gulp);

You can pass in an options object as shown below. The values shown below are the defaults.

taskLoader({
  taskDirectory: 'gulp-tasks', // the directory your tasks are stored in (relative and absolute paths accepted)
  plugins: {},                 // the plugins to expose to your tasks
  filenameDelimiter: '',       // a character or string of characters to replace in task filenames
  tasknameDelimiter: '',       // a character or string of characters to insert in place of removed filenameDelimiter
  config: {},                  // an object to store configuration for use in tasks
  configFile: ''               // the relative path to your task configuration file from your 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.

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:

taskLoader({
  filenameDelimiter: '-',
  tasknameDelimiter: ':'
});

These options would convert a filename such as move-all.js to a task with the name move:all.

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 = require('gulp-simple-task-loader');
var plugins = require('gulp-load-plugins')();

taskLoader({ plugins: plugins });

If not using gulp-load-plugins you must specify which plugins you want made available to your tasks.

(gulpfile.js)
'use strict';

var taskLoader = require('gulp-simple-task-loader');
var plugins = {
  bump: require('gulp-bump'),
  mocha: require('gulp-mocha')
};

taskLoader({ plugins: plugins });

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.

(gulpfile.js)
'use strict';

var taskLoader = require('gulp-simple-task-loader');
var config = { env: 'production' };

taskLoader({ config: config, configFile: 'config.js' });
(config.js)
'use strict';

module.exports = {
  // insert configuration options here
};

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.

This is a typical function as you are used to with gulp.

'use strict';

module.exports = function(gulp, config, plugins) {
  return function([callback]) {}; // the task functionality -- callback optional
};

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.exports = function(gulp, config, plugins) {
  return {
    deps: [],                   // an array of task names to execute before this task
    params: [],                 // an array of parameters to send to `fn`
    fn: function([callback]) {} // 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' ],
fn: function(param, cb) {} // where param is an item from the params array,
                           // and cb is a callback to be called at the end of your function
(gulpfile.js)

'use strict';

var taskLoader = require('gulp-simple-task-loader');
var plugins = require('gulp-load-plugins');

taskLoader({
  filenameDelimiter: '-',
  tasknameDelimiter: ':',
  plugins: plugins
});
(tasks/lint-all.js)

'use strict';

module.exports = function(gulp, config, plugins) {
  return {
    deps: [ 'lint:client', 'lint:server' ]
  };
};
(tasks/lint-client.js)

'use strict';

module.exports = function(gulp, config, plugins) {
  return function() {
    return gulp.src('./client/**/*.js')
      .pipe(plugins.jshint);
  };
};
(tasks/lint-server.js)

'use strict';

module.exports = function(gulp, config, plugins) {
  return function() {
    return gulp.src('./server/**/*.js')
      .pipe(plugins.jshint);
  };
};
(gulpfile.js)

'use strict';

var taskLoader = require('gulp-simple-task-loader');
var plugins = require('gulp-load-plugins');

taskLoader({
  filenameDelimiter: '-',
  tasknameDelimiter: ':',
  plugins: plugins
});
(tasks/parameterized.js)

'use strict';

module.exports = function(gulp, config, plugins) {
  return {
    params: [ '1', '2' ],
    fn: function(param, cb) {
      console.log(param);
      cb();  // 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:

1
2

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.2.4 - added table of contents and changelog to README.md
      • 1.2.1 - added testing for plugins and config options
      • 1.2.0 - restructured package to be more modular
    • 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.11

6 years ago

1.3.10

6 years ago

1.3.8

9 years ago

1.3.7

9 years ago

1.3.6

9 years ago

1.3.5

9 years ago

1.3.4

9 years ago

1.3.3

9 years ago

1.3.2

9 years ago

1.3.1

9 years ago

1.3.0

9 years ago

1.2.4

9 years ago

1.2.3

9 years ago

1.2.2

9 years ago

1.2.1

9 years ago

1.2.0

9 years ago

1.1.9

9 years ago

1.1.8

9 years ago

1.1.7

9 years ago

1.1.6

9 years ago

1.1.5

9 years ago

1.1.4

9 years ago

1.1.3

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.38

9 years ago

1.0.37

9 years ago

1.0.36

9 years ago

1.0.32

9 years ago

1.0.31

9 years ago

1.0.30

9 years ago

1.0.29

9 years ago

1.0.28

9 years ago

1.0.27

9 years ago

1.0.26

9 years ago

1.0.25

9 years ago

1.0.24

9 years ago

1.0.23

9 years ago

1.0.22

9 years ago

1.0.21

9 years ago

1.0.20

9 years ago

1.0.19

9 years ago

1.0.18

9 years ago

1.0.17

9 years ago

1.0.16

9 years ago

1.0.15

9 years ago

1.0.14

9 years ago

1.0.12

9 years ago

1.0.11

9 years ago

1.0.10

9 years ago

1.0.9

9 years ago

1.0.8

9 years ago

1.0.7

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago