4

I defined a task section of my Gruntfile.js for two environments -- development and production. But I don't understand, how Grunt decides, wheter to use which environment section.

Gruntfile.js

module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    less: {
      development: {
        options: {
          paths: ["public/css"]
        },
        files: {
          "public/css/style.css": "public/css/style.less"
        }
      },
      production: {
        options: {
          paths: ["public/css"],
          plugins: [
          ],
          modifyVars: {
          }
        },
        files: {
          "public/css/style.css": "public/css/style.less"
        }
      }
    },
    watch: {
      css: {
        files: 'public/css/*.less',
        tasks: ['less'],
        options: {
          livereload: true,
        },
      },
    }
  });

  // Load the plugin that provides the "less" task.
  grunt.loadNpmTasks('grunt-contrib-less');
  // Load the plugin that provides the "watch" task.
  grunt.loadNpmTasks('grunt-contrib-watch');

  // Default task(s).
  grunt.registerTask('default', ['less', 'watch']);

};

How to let Grunt know, which environment is currently active?

automatix
  • 14,018
  • 26
  • 105
  • 230

2 Answers2

3

To alternate I suggest you to create a development task and run it with grunt development

// Default task(s).
grunt.registerTask('default', ['less:production', 'watch']);

// Development task
grunt.registerTask('development', ['less:development', 'watch']);
Rigotti
  • 2,766
  • 23
  • 28
  • Thank you for your answer! It works. Is it also possible to get the environment information from a separate file? My idea is to save this information in a non-versioned file, in order to not have to think every time, in which environment I'm currently executing the command. – automatix Apr 01 '15 at 15:12
  • In case of LESS (but maybe it's possible for every module, I didn't test it) the call `grunt less:development` works as well. If you know, wheter it works for every task, feel free to extend your answer with this information. – automatix Apr 01 '15 at 15:13
  • hi @automatix, that's a bit ahead of the question scope. I suggest you to create another question with these info, I guess other people might find it useful either. :) – Rigotti Apr 01 '15 at 16:47
0

You can have two different grunt files one for development environment and one for production (for instance gruntfile.dev.js and gruntfile.prod.js) and specify which file should be considered as corresponding grunfile by specifying file name in your npm grunt command like this

grunt --gruntfile gruntfile.prod.js

Remember you can define two aliases for your dev and prod grunt commands see below

"scripts": 
 {
    "build:prod": "grunt --gruntfile gruntfile.prod.js",
    "build:dev": "grunt --gruntfile gruntfile.dev.js",
 }

so, by typing npm run build:prod the prod gruntfile will be run and by typing npm run build:dev dev gruntfile will be run.

By doing so you have more flexibility and it becomes easier to maintain changes associated to your grunt configuration

Code_Worm
  • 4,069
  • 2
  • 30
  • 35