2

我为两种环境定义了我的Gruntfile.js的任务部分 - developmentproduction。但我不明白,Grunt是如何决定使用哪个环境部分的。如何设置Grunt的环境?

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

}; 

如何让咕噜知道,这环境是当前活动?

回答

1

交替,我建议你创建一个development任务,并与grunt development

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

// Development task 
grunt.registerTask('development', ['less:development', 'watch']); 
+0

谢谢您的回答运行它!有用。是否可以从单独的文件获取环境信息?我的想法是将这些信息保存在一个非版本化的文件中,以便不必每次都考虑在当前执行命令的环境中。 – automatix 2015-04-01 15:12:29

+0

如果LESS(但可能对每个模块都有可能,我没有测试它)调用'grunt less:development'也可以。如果您知道,它适用于每项任务,请随时将这些信息延伸至您的答案。 – automatix 2015-04-01 15:13:39

+0

hi @automatix,这有点超前于问题范围。我建议你用这些信息创建另一个问题,我想其他人也许会觉得它有用。 :) – Rigotti 2015-04-01 16:47:21