Organize your gulp-tasks in separate files
Because I rarely use gulp these days. Wanna take over? Tell me.
npm install gulp-task-loader --save-dev
- Create one file / task
- Place the task-files in a folder named 'gulp-tasks' (or whatever you like)
- Require this module and invoke it [with or without options]
- Gulp-tasks now magically exist (named after task file name)
You may create subfolders of tasks as well. Tasks in these folders will have their task name prefixed by the folder name. For example, if you have a task named coffee
that compiles CoffeeScript files, you could place this task in the gulp-tasks/browser
folder and it would be invoked using gulp browser:coffee
. You may nest folders as deep as required, and each folder will be added to the task name.
// gulp-tasks/copy.js
module.exports = function() {
return gulp.src("src/**/*")
.pipe(gulp.dest("dist/**/*"));
};
// gulpfile.js
// Load all tasks from folder `gulp-tasks`
require('gulp-task-loader')();
// use it!
gulp.watch(someFiles, ['copy']);
// gulp-tasks/task-with-deps.js
module.exports = function() {
return gulp.src("src/**/*")
.pipe(gulp.dest("dist/**/*"));
};
module.exports.dependencies = ['copy'];
require('gulp-task-loader')('le-tasks-de-gulp');
require('coffee-script/register');
require('gulp-task-loader')({ exts: ['.coffee'] });
require('gulp-task-loader')({ exts: ['.jscript'] });
Each task is called with a context object containing a reference to gulp
and opts
(the options object).
// gulpfile.js
var pkg = require('./package.json');
require('gulp-task-loader')({ pkg: pkg, dest: 'dist' });
// gulp-tasks/xxx.js
module.exports = function() {
return this.gulp.src(this.opts.pkg.main)
.pipe(this.gulp.dest(this.opts.dest));
};
// gulp-tasks/copy/all.js
// gulp-tasks/copy/fonts.js
// gulpfile.js
gulp.watch(allFiles, ['copy:all']);
gulp.watch(someFiles, ['copy:fonts']);
Given the files in folder copy - two tasks have been created. copy:all
& copy:fonts
Type String
Default gulp-tasks
Path to folder with gulp tasks
Type Array
Default to keys of require.extensions
List of extensions to filter tasks by. Example: ['.js', '.coffee']
npm test
- Bugfix for options.dir
- Fixed bug that caused subtasks to break
- Allow loading of infinitely nested children directories
- Replaced lodash.defaults with object-assign
- Call tasks with context. Thanks to @mamboer
- Load tasks relative to project. Thanks to @archr
- tasks in subfolder will be named
folderName:taskName
. Thanks to @evanshortiss.
- added support for other sources than .js. Thanks to @blvz.
- the dark ages of not documenting version bumps..