运行时将选项传递给grunt任务

Sim*_*lGy 31 gruntjs

我以为有办法做到这一点,我以前偶然发现了它.我已经阅读了这些答案,但它们并不是我所说的:

以编程方式将参数传递给grunt任务?

Grunt条件选项

从grunt模板访问进程/环境

我也查看了咕噜咕噜的文档,但它不存在:

https://github.com/gruntjs/grunt/wiki/Configuring-tasks

有这样的语法吗?

grunt.task.run 'htmlmin:allFiles:collapseWhitespace=true'

dc5*_*dc5 55

您可以使用该语法,但这意味着将这些参数传递给htmlmin任务:allFiles,'collapse=true'.

例如,给定以下任务:

grunt.registerTask('so', function(arg1, arg2) {
   console.log(arg1 + ", " + arg2); 
}); 
Run Code Online (Sandbox Code Playgroud)

运行:

grunt so:barley:test=true
Run Code Online (Sandbox Code Playgroud)

给出以下输出:

barley, test=true
Run Code Online (Sandbox Code Playgroud)

还有其他方法可以传递faq中描述的参数/共享信息:如何跨多个任务共享参数?

- 选项可能适用于您

另一种跨多个任务共享参数的方法是使用grunt.option.在此示例中,grunt deploy --target=staging在命令行上运行将导致grunt.option('target')返回"staging".