Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have multiple gruntjs files in my project for code organization?

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 like so:

grunt.js
|
|- project1
|   |
|   |-grunt.js
|
|- project2
|
|- project3 (etc..)

I want the top level grunt.js to build all the projects. However as the list of projects grow I don't want my top level grunt.js file to become huge. So I would like to organize it so that the top level grunt can call the project level grunt files to build them. Or if someone wants to just build project1, they can go to the project1 folder and run it's own grunt.js. Can this be done? If so, how do I call the other grunt files? If not, then what's an alternative solution other than having one huge grunt.js file? Thanks.

like image 994
Charlton Avatar asked Dec 04 '12 00:12

Charlton


2 Answers

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.

Example

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' ] } }
  });

};
like image 134
Scott Rippey Avatar answered Oct 17 '22 03:10

Scott Rippey


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.

like image 14
jaime Avatar answered Oct 17 '22 01:10

jaime