I have a project where I\'m using grunt to process my Js and SASS files.
At the moment, when I need to process something, I nee
Here is how, you can organise your grunt tasks :
Some rules that you have to respect :
home configuration task and a home register task because when you call it with grunt home, grunt isn't able to know the task that you are refering to. ├── Gruntfile.js
└── grunt
├── config
│ ├── sass.js
│ ├── concat.js
│ └── uglify.js
└── register
├── GroupOfTasks.js
└── AnotherGroupOfTasks.js
Your Gruntfile.js will load and configure all tasks in grunt/config and grunt/register folder with this code :
module.exports = function(grunt) {
// Load the include-all library in order to require all of our grunt
// configurations and task registrations dynamically.
var includeAll;
try {
includeAll = require('include-all');
}
catch (e0) {
console.error('Could not find `include-all` module.');
console.error('Skipping grunt tasks...');
console.error('To fix this, please run:');
console.error('npm install include-all --save-dev');
console.error();
}
/**
* Loads Grunt configuration modules from the specified
* relative path. These modules should export a function
* that, when run, should either load/configure or register
* a Grunt task.
*/
function loadTasks(relPath) {
return includeAll({
dirname: require('path').resolve(__dirname, relPath),
filter: /(.+)\.js$/
}) || {};
}
/**
* Invokes the function from a Grunt configuration module with
* a single argument - the `grunt` object.
*/
function invokeConfigFn(tasks) {
for (var taskName in tasks) {
if (tasks.hasOwnProperty(taskName)) {
tasks[taskName](grunt);
}
}
}
// Load task functions
var taskConfigurations = loadTasks('./grunt/config'),
registerDefinitions = loadTasks('./grunt/register');
// Run task functions to configure Grunt.
invokeConfigFn(taskConfigurations);
invokeConfigFn(registerDefinitions);
};
Your config task should look like that (E.g : sass.js) :
module.exports = function(grunt) {
grunt.config.set('sass', {
dev: {
options: {
sourceMap: false
},
files: {
'main.css': 'main.scss'
}
}
});
grunt.loadNpmTasks('grunt-sass');
};
You will be able to run config task with grunt sass to run all sass task or grunt sass:dev to run only one task.
And a register task should look like that (E.g: SassAndConcat task to run sass and concat task) :
module.exports = function(grunt) {
grunt.registerTask('SassAndConcat', [
'sass:dev',
'concat:dev',
]);
};
And now you will be able to run grunt SassAndConcat.
If you understand that, you will be able to run your grunt tasks more efficiently by running the right task at the right time.
Don't forget to install include-all NPM module to require all of our grunt configurations and task registrations dynamically.