Skip to content

Instantly share code, notes, and snippets.

@usefulthink
Last active August 29, 2015 13:57
Show Gist options
  • Save usefulthink/9662676 to your computer and use it in GitHub Desktop.
Save usefulthink/9662676 to your computer and use it in GitHub Desktop.
grunt-task lazyloading
module.exports = function (grunt) {
grunt.initConfig({
// as usual, just make sure that – for multitasks – there always needs to be
// an `options`-entry to correctly detect them
copy: {
options: {},
dist: {
files: [{
// ....
}]
}
}
});
// registers task-placeholders that will load the npm-tasks when they are actually invoked.
grunt.registerNpmTasks = function(tasksMap) {
Object.keys(tasksMap).forEach(function(taskName) {
var cfg = grunt.config(taskName),
isMultiTask = false;
grunt.verbose.write('[lazyload] '+taskName.underline+' ('+tasksMap[taskName].yellow+')...');
if(!cfg) {
grunt.verbose.fail('no config');
return;
}
if(cfg.options) { isMultiTask = true; }
grunt.verbose.ok();
grunt[isMultiTask? 'registerMultiTask' : 'registerTask'](taskName, function() {
grunt.verbose.writeln('[lazyload] invocation for task '+taskName.underline+', target »'+this.target+'«. Will load module: '+tasksMap[taskName].yellow+'.');
grunt.loadNpmTasks(tasksMap[taskName]);
if(isMultiTask || this.target) {
grunt.task.run(taskName + ':' + this.target);
} else {
grunt.task.run(taskName);
}
});
});
};
// maps task-names to npm-module-names and registers the placeholders
grunt.registerNpmTasks({
'uglify': 'grunt-contrib-uglify',
'copy': 'grunt-contrib-copy',
'concat': 'grunt-contrib-concat',
'cssmin': 'grunt-contrib-cssmin'
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment