I\'m using gruntjs for my project and was wondering if it\'s possible to have multiple grunt.js files in my project? The reason I\'m asking is that my project is organized l
I know this question might be old, but I want to clarify there is a way to organise your codes in a proper way.
To prevent having multi call like this:
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-concat');
Replace them with:
require('load-grunt-tasks')(grunt);
And use:
grunt-config-dir
To run the tasks from a folder.
To know more about it, check this out.
There's a grunt task called grunt-hub which seems to do what you want to do.
grunt-hub:
A Grunt task to watch and run tasks on multiple Grunt projects.
hub task
The hub task is for running tasks on multiple projects. It would like to know which Gruntfiles to use and which tasks to run on each Grunt project. For example if I would like to lint and test on every Grunt project one folder up:
I haven't used it, but it might meet your requirements. Also you might want to look into more lower level such as grunt-exec or creating your own tasks to spawn child processes.
I recently solved this issue with a very simple solution.
I implemented grunt.config.merge(config)
to replace grunt.initConfig(config)
. You can call it multiple times, and it will simply merge the config into a single config.
Update: As of Grunt v0.4.5, the grunt.config.merge
function is built-in, so you no longer need to include it as a separate library.
This allows me to organize my config by feature.
I create a config file for each feature, such as desktop-javascripts.js
, desktop-css.js
, mobile-javascripts.js
, mobile-css.js
.
Also, they share common configuration in default-options.js
, so I can specify compression settings and what not.
Gruntfile.js:
module.exports = function(grunt) {
require('./default-options.js')(grunt);
require('./desktop-javascripts.js')(grunt);
require('./desktop-css.js')(grunt);
require('./mobile-javascripts.js')(grunt);
require('./mobile-css.js')(grunt);
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
};
desktop-javascripts.js:
module.exports = function(grunt) {
// Configure all JavaScript tasks:
grunt.registerTask('desktop-javascripts', [ 'concat:JS', 'jshint' ]);
grunt.config.merge({
concat: { 'JS': { files: allJS } },
jshint: { 'JS': { files: allJS } },
watch: { 'JS': { files: allJS, tasks: [ 'desktop-javascripts' ] } }
});
};