Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Modular Grunt.js config
{
"production": [
"task:production",
"uninstalled:production"
],
"development": [
"task"
]
}
var path = require('path');
module.exports = {
task: {
development: {
options: {
path: [ '.tmp', '<%= pkg.app %>' ]
}
},
production: {
options: {
path: path.resolve('./path/to.js')
}
}
}
};
'use strict';
var path = require('path'),
fs = require('fs'),
matchdep = require('matchdep');
module.exports = function (grunt) {
var gruntConfig = {
pgk: grunt.file.readJSON('package.json');
};
// load all installed grunt tasks
matchdep.filterDev('grunt-*').forEach(function(name){
// create file path to task config with the same name
var gruntTaskFile = path.resolve('grunt/' + name + '.js');
// load the task
grunt.loadNpmTasks(name);
// check if config file exists
if(fs.existsSync(gruntTaskFile)) {
// load config file
var taskConfig = require(gruntTaskFile);
for(var task in taskConfig){
if(taskConfig.hasOwnProperty(task)){
// add the tasks to the final config object
gruntConfig[task] = taskConfig[task];
}
}
}
});
// load the combined config object
grunt.initConfig(gruntConfig);
var gruntTasks = grunt.file.readJSON('grunt/tasks.json');
// Filter tasks of uninstalled packages off the tasks.json object
// and register the alias tasks
function filterTaskList() {
// loop through config object with format { "aliasTaskName": [ "taskName" ] }
for(var aliasTaskName in gruntTasks) {
if(gruntTasks.hasOwnProperty(aliasTask)) {
var aliasTaskList = gruntTasks[aliasTaskName],
out = [];
for(var index in aliasTaskList){
if(aliasTaskList.hasOwnProperty(index)) {
var taskString = aliasTaskList[index],
// extract task name from taskString with format 'task:targets:args'
taskName = taskString.split(':')[0];
if(grunt.config.data[taskName] !== undefined || aliasTaskList[taskName] !== undefined){
out.push(taskString);
}
}
}
grunt.registerTask(aliasTaskName, out);
}
}
}
filterTaskList();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.