简体   繁体   中英

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.

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

};

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM