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' ] } }
});
};
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…