【问题标题】:Do Grunt options need to be set when the task is initiated, or can I modify options at runtime?启动任务时是否需要设置 Grunt 选项,或者我可以在运行时修改选项?
【发布时间】:2014-04-06 15:12:57
【问题描述】:

我一直在尝试从一个 grunt 任务中找到为我的所有 grunt 任务设置全局选项的最佳方法,但没有太多运气让事情正常工作。

我想知道任务是否以某种方式编译,以便它们被初始选项值卡住,或者是否可以在运行时更改选项(当 watch 任务正在运行时),我只是做错了事。

基本情况

这是一个特定的情况(注意 Gruntfile 是用 coffeescript 编写的)。我从以下任务开始:

sass:
    options:
        sourcemap: true
    compile:
        files:
            "css/style.css" : "sass/style.sass"

我想要做什么

我希望能够从另一个任务中动态设置 sourcemap 选项,如下所示:

sass:
    options:
        sourcemap: '<% grunt.options('local') %>'
    compile:
        files:
            "css/style.css" : "sass/style.sass"

watch 任务将获取更改,并运行一个任务来适当地设置全局选项。

watch:
    local:
        files: ['local.json']
        tasks: ['local']
    dist:
        files: ['dist.json']
        tasks: ['dist']

grunt.option('local', true) # Base declaration

grunt.registerTask( 'local', 'Local is true', () -> grunt.option('local', true) )
grunt.registerTask( 'dist', 'Local is false', () -> grunt.option('no-local') )

我希望对其进行配置,以便在“本地”或“dist”任务运行之后触发的任何任务(例如当watch 再次运行sass 任务时)它将使用我的“本地”的最新值选项。我尝试了几种方法,这似乎是最有希望的,但我还没有让它按预期工作。

【问题讨论】:

    标签: coffeescript gruntjs grunt-contrib-watch


    【解决方案1】:

    默认情况下,grunt-contrib-watch 将生成任务作为子进程运行。这些子进程不共享父进程的上下文,因此不存在对配置的更改。

    最简单的方法是禁用生成任务:

    watch:
      options: spawn: false
    

    监视任务将在同一进程上下文中运行任务。

    【讨论】:

    • 感谢您的解释。我已经阅读了 grunt-contrib-watch 文档,但没有注意到重要的信息或将其与我的问题联系起来。
    猜你喜欢
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多