Last active
December 17, 2015 15:28
-
-
Save MarcelloDiSimone/5631466 to your computer and use it in GitHub Desktop.
Modular Grunt.js config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"production": [ | |
"task:production", | |
"uninstalled:production" | |
], | |
"development": [ | |
"task" | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = require('path'); | |
module.exports = { | |
task: { | |
development: { | |
options: { | |
path: [ '.tmp', '<%= pkg.app %>' ] | |
} | |
}, | |
production: { | |
options: { | |
path: path.resolve('./path/to.js') | |
} | |
} | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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