Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance for common configuration options in grunt.js configuration

How do people handle common configuration options in Grunt for multiple projects. The projects would share some common configuration options, e.g. for min, but also have private or custom configuration settings per project, e.g. only one out of three projects requires less or has different options for it.

Is there a way to share this common configuration between the projects, using inheritance or importing an existing file, or does each project have to define all settings?

The projects I'm referring to would reside in a directory hierarchy like

root
    module1
         grunt.js
    module2
         grunt.js
    module3
         grunt.js

Is there some way to provide common configuration settings at the root level?

like image 213
nwinkler Avatar asked Nov 19 '12 16:11

nwinkler


1 Answers

You can easily store configuration in as many external JSON files as you need. grunt.file.readJSON will help you here. For example:

module.exports = function(grunt) {

  var concatConf = grunt.file.readJSON('../concat-common.json'),
      minConf = grunt.file.readJSON('../min-common.json');

  // do whatever you want with concatConf and minConf here
  // ...

  // Project configuration.
  grunt.initConfig({
    pkg: '<json:grunt-sample.jquery.json>',
    meta: {
      banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
        '<%= grunt.template.today("yyyy-mm-dd") %>\n' +
        '<%= pkg.homepage ? "* " + pkg.homepage + "\n" : "" %>' +
        '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' +
        ' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */'
    },

    concat: concatConf,
    min: minConf

    // ...
  });

  // Default task.
  grunt.registerTask('default', 'concat min');

};

Don't forget that a gruntfile is a regular JavaScript file executed in Node environment and configuration options are regular JavaScript objects :)

like image 147
Dmitry Pashkevich Avatar answered Nov 15 '22 10:11

Dmitry Pashkevich