代码之家  ›  专栏  ›  技术社区  ›  SimplGy

在运行时将选项传递给一个咕哝的任务

  •  33
  • SimplGy  · 技术社区  · 11 年前

    我认为有一种方法可以做到这一点,而且我以前也偶然发现过。 我读过这些答案,但它们不是我所说的:

    Programmatically pass arguments to grunt task?

    Grunt conditional option

    Accessing the process / environment from a grunt template

    我也看了看那些咕哝的医生,但没有:

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

    有这样的语法吗?

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

    1 回复  |  直到 7 年前
        1
  •  63
  •   Sergio    9 年前

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

    例如,给定以下任务:

    grunt.registerTask('so', function(arg1, arg2) {
       console.log(arg1 + ", " + arg2); 
    }); 
    

    正在运行:

    grunt so:barley:test=true
    

    给出以下输出:

    barley, test=true
    

    还有其他方法可以传递常见问题中描述的参数/共享信息: How can I share parameters across multiple tasks?

    --Options 可能适用于您

    在多个任务之间共享参数的另一种方法是使用 grunt.option 。在本例中,运行 grunt deploy --target=staging 在命令行上会导致 grunt.option('target') 返回“暂存”。