【问题标题】:Changing Grunt config variable through command line通过命令行更改 Grunt 配置变量
【发布时间】:2013-11-07 15:05:06
【问题描述】:

我有两种不同的路径来编译移动和桌面代码。我想通过在命令行中传递一个 grunt 参数来替代。

/**
 * @module Build
 * @class Build.Config
 * @static
 */

module.exports = function(grunt) {

var config = {};

    var NewPath;

    var env = grunt.option('target') || "Mobile";


    if (env == "Desktop") {  // MAKE THIS DYNAMIC WITH COMMAND LINE ARGUMENT
        newPath = "source/desktop/";
    }
    else {
       newPath = "source/mobile/";
    }

config.root = newPath;
config.stylesheets = config.root + '/stylesheets';
config.javascripts = config.root + '/javascripts';
config.images = config.root + '/images';
config.jsbin = config.javascripts + '/generated';
config.cssbin = config.stylesheets + '/generated';
config.docsbin = 'docs';



// Project configuration.
grunt.initConfig({

    'beautifier': {
        'options': {
            'indentSize': 1,
            'indentChar': '\t',
            'spaceAfterAnonFunction': true
        }
    },

    'beautify': {
        'files': [ config.javascripts + '/app/**/*.js' ]
    },

    'requirejs': require('./build/config/requirejs.js')(config),

    'watch': require('./build/config/watch.js')(config),
    'stylus':require('./build/config/stylus.js')(config)

});


// Default task.
grunt.registerTask('default', ['stylus:compile','requirejs']);      
grunt.registerTask('dev', ['stylus:dev']);

grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-requirejs');
grunt.loadNpmTasks('grunt-contrib-stylus');
};

【问题讨论】:

    标签: javascript command-line-interface gruntjs


    【解决方案1】:

    事实证明我做得对,我只需要正确传递 env 的变量:

    $ grunt --target="桌面"

    【讨论】:

      【解决方案2】:

      --option 的替代方法是通过冒号传递它。例如将其传递给 jshint

      grunt jshint:desktop
      

      然后配置 grunt 以使用 process.argv 获取该命令行参数,您可以使用它来配置您的路径或其他可能需要的内容:

      module.exports = function(grunt) {
          "use strict";
      
         //dynamic config after the ':'. 'desktop' here
          var env = process.argv[2].split(':')[1]; 
      
      
          var config = {
              pkg: grunt.file.readJSON('package.json'),
      
              jshint: {
                  options: {
                      jshintrc: '.jshintrc',
                      "force": true
                  }
              },
          };
      
          //...
      
          config.jshint[env] = { // ex:  $ grunt jshint:desktop
            src: ['public/'+env+'/js/main.js']
          };
      
          //...
      
          // Project configuration.
          grunt.initConfig(config);
      
          //...
      };
      

      使用process 的一个警告是,当您使用重生您的进程的grunt 任务(如有用的grunt-concurrent)时,它将不起作用。在这种情况下,最好使用grunt.option,如@im_benton 所示。传递grunt mytask --myvar=myval 并在你的 Gruntfile.js 中以grunt.option('myvar') 的形式获取它`

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-18
        • 2016-06-22
        • 2019-10-17
        • 1970-01-01
        • 2021-11-26
        • 1970-01-01
        相关资源
        最近更新 更多