Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Define specific target configuration.
JavaScript
branch: master

README.md

grunt-config Build Status

Easy way to define specific target configuration.

Getting Started

This plugin requires Grunt ~0.4.0

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-config --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-config');

This plugin was designed to work with Grunt 0.4.x. If you're still using grunt v0.3.x it's strongly recommended that you upgrade.

Config task

Run this task with the grunt config command.

Task targets and options may be specified according to the grunt Configuring tasks guide.

Options

variables

Type: Object

This option is used to define the variables to be exported in grunt.config object.

options: {
  variables: {
    'foo': 'bar'
  }
}
logOutput

Type: boolean Default: true

When set to false this option suppresses the output in the console. This is useful if you are setting credentials in the config variables.

Usage Examples

config: {
  dev: {
    options: {
      variables: {
        'environment': 'development'
      }
    }
  },
  prod: {
    options: {
      variables: {
        'environment': 'production'
      }
    }
  }
}

Environment variable in source (with grunt-replace)

File build/environment.txt:

@@environment

Gruntfile:

config: {
  dev: {
    options: {
      variables: {
        'environment': 'development'
      }
    }
  },
  prod: {
    options: {
      variables: {
        'environment': 'production'
      }
    }
  }
},

replace: {
  dist: {
    options: {
      variables: {
        'environment': '<%= grunt.config.get("environment") %>'
      },
      force: true
    },
    files: [
      {expand: true, flatten: true, src: ['build/environment.txt'], dest: 'public/'}
    ]
  }
}

// development
grunt.registerTask('dev', ['config:dev', 'replace']);

// production
grunt.registerTask('prod', ['config:prod', 'replace']);

Handlebars environment partials (with assemble)

Gruntfile:

config: {
  dev: {
    options: {
      variables: {
        'partials': ['assets/partials/dev/*.hbs']
      }
    }
  },
  dist: {
    options: {
      variables: {
        'partials': ['assets/partials/dist/*.hbs']
      }
    }
  }
},

assemble: {
  dist: {
    options: {
      partials: '<%= grunt.config.get("partials") %>',
      ext: '.html'
    },
    files: [
      {expand: true, cwd: 'assets/templates', src: ['**/*.hbs'], dest: 'tmp/templates'}
    ]
  }
}

// development
grunt.registerTask('dev', ['config:dev', 'assemble']);

// production
grunt.registerTask('prod', ['config:prod', 'assemble']);

Dynamic options (with grunt-contrib-concat)

Gruntfile:

config: {
  dev: {
    options: {
      variables: {
        'concatOptions': {
          banner: '(function dev () {',
          footer: '}());'
        }
      }
    }
  },
  prod: {
    options: {
      variables: {
        'concatOptions': {
          banner: '(function prod () {',
          footer: '}());'
        }
      }
    }
  }
},

concat: {
  dist: {
    options: {
      process: function (src, filepath) {
        var concatOptions = grunt.config.get('concatOptions');
        return concatOptions.banner + '\n' + src +
          concatOptions.footer + '\n';
      }
    },
    files: [
      {expand: true, flatten: true, src: ['test/fixtures/concat.txt'], dest: 'tmp/'}
    ]
  }
}

// development
grunt.registerTask('dev', ['config:dev', 'concat']);

// production
grunt.registerTask('prod', ['config:prod', 'concat']);

Gruntfile (using grunt templates):

config: {
  dev: {
    options: {
      variables: {
        'concatOptions': {
          banner: '(function dev () {',
          footer: '}());'
        }
      }
    }
  },
  prod: {
    options: {
      variables: {
        'concatOptions': {
          banner: '(function prod () {',
          footer: '}());'
        }
      }
    }
  }
},

concat: {
  dist: {
    options: {
      banner: '<%= grunt.config.get("concatOptions").banner %>',
      footer: '<%= grunt.config.get("concatOptions").footer %>'
    },
    files: [
      {expand: true, flatten: true, src: ['test/fixtures/concat.txt'], dest: 'tmp/'}
    ]
  }
}

// development
grunt.registerTask('dev', ['config:dev', 'concat']);

// production
grunt.registerTask('prod', ['config:prod', 'concat']);

Prevent stdout in production environment

Gruntfile:

config: {
  dev: {
    options: {
      variables: {
        'environment': 'development'
      }
    }
  },
  prod: {
    logOutput: false,
    options: {
      variables: {
        'environment': 'production'
      }
    }
  }
}

Release History

  • 2014-08-26   v0.2.2   Fixes backwards incompatible changes introduced in NPM.
  • 2014-07-13   v0.2.1   Readme updated and new logOutput flag (thanks @kylerush).
  • 2014-06-09   v0.2.0   No way, finally remove support for 8.x of node.
  • 2014-06-09   v0.1.8   Update grunt-contrib-jshint version to support node 8.x versions.
  • 2014-06-09   v0.1.7   Support for node v8.x versions.
  • 2014-06-09   v0.1.6   Third party updated, and few examples.
  • 2014-03-06   v0.1.5   Update dependencies.
  • 2013-04-07   v0.1.4   Fix log for complex object with util.inspect.
  • 2013-04-02   v0.1.3   Add peerDependencies.
  • 2013-04-02   v0.1.2   Update description and README.md.
  • 2013-04-02   v0.1.1   Update variable definition object.
  • 2013-04-02   v0.1.0   Initial version.

Task submitted by Ariel Falduto

Something went wrong with that request. Please try again.