2016-08-10 40 views
1

为了保持gruntfile DRY,我想改变基于环境的选项,我正在运行一个grunt任务。grunt环境特定选项的任务

因此,举例来说,如果我想用两个咕噜任务:

grunt.task.run('uglify:production'); 
grunt.task.run('uglify:development'); 

我想他们两个编译相同的文件,但有不同的选择。

uglify: { 

    production: { 
    options: { 
     compress: true 
    } 
    } 

    development: { 
    options: { 
     compress: false, 
     beautify: true 
    } 
    } 

    // rather not redeclare these files twice 
    files: { 

    vendor: { 
     // this name should change based on the environment 
     'dest/vendor-output.js': ['src/vendor-input1.js', 'src/vendor-input2.js'] 
    }, 
    custom: { 
     'dest/custom-output.js': ['src/custominput1.js', 'src/custominput2.js'] 
    } 

} 

如果生产甚至可能将目标名称设置为custom-output.min.js,那将更加理想。

尝试了一个if-else语句,但在不能飞行的grunt任务定义中。

回答

1

由于grunt配置只是json,你可以使用grunt模板并将你的文件作为配置属性。

uglifyFiles: { 

    vendor: { 
    // this name should change based on the environment 
    'dest/vendor-output.js': ['src/vendor-input1.js', 'src/vendor-input2.js'] 
    }, 
    custom: { 
    'dest/custom-output.js': ['src/custominput1.js', 'src/custominput2.js'] 
    } 
}, 
uglify: { 

    production: { 
    options: { 
     compress: true 
    }, 
    files: '<%= uglifyFiles %>' 
    }, 

    development: { 
    options: { 
     compress: false, 
     beautify: true 
    }, 
    files: '<%= uglifyFiles %>' 
    } 
} 

http://gruntjs.com/configuring-tasks#templates

对不起,我不太明白这个问题

If production could even have the destination name to custom-output.min.js that would be even more ideal.

可不可以给多一点信息,或者是上面的你在那里想达到什么目的?

编辑

好像你正在试图做什么需要重复的部分进行空的,你其实是想在每个略有不同的代码。它可以完成,但不是在json中,你需要使用js并使用括号表示法将目标创建为其键。我认为一个更简单的方法,以及这样设置的咕噜声将会做到以下几点。

vendorUglifyFiles: ['src/vendor-input1.js', 'src/vendor-input2.js'], 
customUglifyFiles: ['src/custominput1.js', 'src/custominput2.js'], 
uglify: { 

    production: { 
    options: { 
     compress: true 
    }, 
    files: { 
     vendor: { 
     'dest/vendor.min.js': '<%= vendorUglifyFiles %>' 
     }, 
     custom: { 
     'dest/custom.min.js': '<%= customUglifyFiles %>' 
     } 
    } 
    }, 

    development: { 
    options: { 
     compress: false, 
     beautify: true 
    }, 
    files: { 
     vendor: { 
     'dest/vendor.js': '<%= vendorUglifyFiles %>' 
     }, 
     custom: { 
     'dest/custom.js': '<%= customUglifyFiles %>' 
     } 
    } 
    } 
} 

编辑:2016年11月8日,15:12

移除抛出的indexOf错误级别:

vendorUglifyFiles: ['src/vendor-input1.js', 'src/vendor-input2.js'], 
customUglifyFiles: ['src/custominput1.js', 'src/custominput2.js'], 
uglify: { 

    production: { 
    options: { 
     compress: true 
    }, 
    files: { 
     'dest/vendor.min.js': '<%= vendorUglifyFiles %>', 
     'dest/custom.min.js': '<%= customUglifyFiles %>' 
    } 
    }, 

    development: { 
    options: { 
     compress: false, 
     beautify: true 
    }, 
    files: { 
     'dest/vendor.js': '<%= vendorUglifyFiles %>', 
     'dest/custom.js': '<%= customUglifyFiles %>' 
    } 
    } 
} 

这做的伎俩。

+0

我试图让一个任务编译成一个缩小的生产版本或文件的正常美化开发版本。你写的例子适用于第一部分。现在只有文件名应该是用于开发的vendor.js或生产版本的vender.min.js。 – Remi

+1

似乎在'production'任务下嵌套'vendor'和'custom'会抛出Warning:pattern.indexOf不是函数错误。去除那个额外的水平就是诀窍。 – Remi

+0

对不起,由记忆打字,感谢您的修复。 –

0

Grunt是Javascript,所以你可以添加IF/ELSE语句。

例子:

files: { 
    (() => { 
     if (grunt.option('vendor')) { 
      return { 
       'dest/vendor-output.js': ['src/vendor-input1.js', 'src/vendor-input2.js'] 
      } 
     } else (grunt.option('release')) { 
      return { 
       'dest/custom-output.js': ['src/custominput1.js', 'src/custominput2.js'] 
      } 
     } 
    }()) 
} 

/*** 
* OR SOMETHING LIKE 
**/ 

files: { 
    (() => { 
     switch(grunt.option) { 
      case 'vendor': 
       return { 
        'dest/vendor-output.js': ['src/vendor-input1.js', 'src/vendor-input2.js'] 
       }; 
       break; 
      case 'release: 
       return { 
        'dest/custom-output.js': ['src/custominput1.js', 'src/custominput2.js'] 
       }; 
       break; 
      default: 
       return {}; 
     } 
    } 
} 

显然,你需要更改为您所希望的情况,因为它现在是不明如何解决供应商和/或释放。

+0

基于grunt任务'uglify:production'或'uglify:development',不应该在这个条件下捕捉生产和开发选项吗? – Remi

+0

这也是可能的。你也可以像下面这样设置一个变量:'grunt uglify --type = production'并且用下面的代码收集它:'grunt.option('type')// = production' –