2014-09-11 28 views
0

我想從終端使用grunt獲取const名稱,並在uglify中使用它。這是我希望發生的:從終端執行const in grunt

uglify: { 
     options: { 
      sourceMap: true, 
      compress: { 
       global_defs: { 
       <myConst>: false 
      } 
      } 
     }, 
     ugly: { 
      src: 'beautiful.js', 
      dest: 'ugly.js' 
     } 
    } 

我用:

咕嚕--target =布拉布拉

傳遞參數,所以MYCONST應該從終端(輸入在這種情況下,布拉布拉)。我似乎無法找到一種方法來代替myConst(在代碼中)。這是可能的嗎?我該怎麼做?

回答

1

由於運行grunt讓你在process.argv以下命令行參數:

  1. 節點
  2. path_to_grunt_script

不能你只是這樣做:

module.exports = function(grunt) { 

    var compress_defs={}, 
     args=process.argv.slice(2); // take all command line arguments skipping first two 

    // scan command line arguments for "--target=SOMETHING" 
    args.forEach(function(arg){ 
     if(arg.match(/--target=(\S+)/)) { // found our --target argument 
      compress_defs[RegExp.$1]=false; 
     } 
    }); 

    grunt.initConfig({ 
     uglify: { 
      options: { 
       sourceMap: true, 
       compress: { 
        global_defs: compress_defs 
       } 
      }, 
      ugly: { 
       src: 'beautiful.js', 
       dest: 'ugly.js' 
      } 
    }); 
}; 

或更好的是,而不是滾動你自己的,使用像minimist這樣的命令行處理庫。