gruntjsconfigdevelopment-environmentproduction-environmentgruntfile

How to set the environment for Grunt?


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?


Solution

  • 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']);