【问题标题】:Pass Options to a grunt task while running it在运行时将选项传递给 grunt 任务
【发布时间】:2013-09-04 21:04:09
【问题描述】:

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

Programmatically pass arguments to grunt task?

Grunt conditional option

Accessing the process / environment from a grunt template

我还查看了 grunt 文档,但没有:

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

有这样的语法吗?

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

【问题讨论】:

    标签: gruntjs


    【解决方案1】:

    您可以使用该语法,但这意味着将这些参数传递给 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') 返回“staging”。

    【讨论】:

    • 您与Dynamic Alias Tasks 共享的链接是我正在寻找的语法。看起来每个任务都需要以这种方式接受参数,因此它不会以相同的方式用于每个任务,目前,不适用于大多数现有任务。我认为这是一种覆盖选项的通用方式。
    • “另一种方式”选项似乎是首选方法。我很想调整答案以同时显示两者。
    猜你喜欢
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多