firstandthird / load-grunt-config

Grunt plugin that lets you break up your Gruntfile config by task
firstandthird.github.io/load-grunt-config/
MIT License
374 stars 64 forks source link

How to pass a variable to a config file? #15

Closed szimek closed 11 years ago

szimek commented 11 years ago

I've got the following configuration:

connect: {
  livereload: {
    options: {
      middleware: function (connect) {
        return [
          modRewrite([
            '!\\.ttf|\\.woff|\\.ttf|\\.eot|\\.html|\\.js|\\.css|\\.png|\\.jpg|\\.gif|\\.svg$ /index.html [L]'
          ]),
          lrSnippet,
          mountFolder(connect, '.tmp'),
          mountFolder(connect, yeomanConfig.app)
        ];
      }
    }
  }
}

It looks like the following syntax is supported:

module.exports = function (grunt) {
    return { /* ... */ };
};

and modRewrite and lrSnippet variables are local to this config, so it's not a problem, but yeomanConfig is shared with other tasks as well. Is there any way to pass it there?

jgallen23 commented 11 years ago

Where does yeomanConfig come from? If it's a global variable, it should work.

szimek commented 11 years ago

Right... the simplest solutions are the best :) It's currently defined like this:

module.exports = function (grunt) {
  var yeomanConfig = {
    app: 'app'
  };

  grunt.initConfig({
    yeoman: yeomanConfig,
    ...
  };
}

but I can make that a global variable, especially that there won't be grunt.initConifig anymore.

Is there a way to run specific config before others are executed? The yeoman option inside grunt.initConfig is later used in many tasks like:

watch: {
  coffee: {
    files: ['<%= yeoman.app %>/scripts/**/*.coffee'],
    tasks: ['coffee:dist']
  }
}

If it's possible, maybe I could just define yeoman option and instead of using global yeomanConfig variable, create a local one if needed:

module.exports = function (grunt) {
    var yeoman = grunt.option('yeoman');
    return { /* ... */ };
};
jgallen23 commented 11 years ago

If I understand it correctly, you could do something like:

module.exports = function(grunt) {

  var yeomanConfig = {
    app: 'app'
  };

    require('load-grunt-config')(grunt, {
        config: { //additional config vars
            yeoman: yeomanConfig
        } 
    });
};

and then you'll have access to yeoman in your tasks

watch.yaml (or .js or .coffee)

  coffee:
    files: '<%= yeoman.app %>/scripts/**/*.coffee'
    tasks: 'coffee'
szimek commented 11 years ago

Thanks!