0

我一直在尝试从一个 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任务时)它将使用我的“本地”选项的最新值。我尝试了一些方法,这似乎是最有希望的,但我还没有能够让它按预期工作。

4

1 回答 1

1

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

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

watch:
  options: spawn: false

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

于 2014-03-05T06:02:53.620 回答