【问题标题】:Running tasks sequentially in Grunt在 Grunt 中按顺序运行任务
【发布时间】:2015-09-16 18:28:52
【问题描述】:

我的印象是,在 grunt.registerTask(taskName, taskList) 中,taskList 将按顺序运行(即,一个完成,然后再进入下一个)。估计不是这样吧?

鉴于此任务:

grunt.registerTask('local-debug', [
    'clean',
    'concat:local',
    'ngconstant:common',
    'targethtml:local',
    'copy:debug'
]);

如何在运行复制之前确保 concat/ngconstant/targethtml 完整?我遇到了问题,因为 ngconstant 在 concat 完成之前正在运行。

编辑:任务未按顺序运行的详细信息。

'concat:local' 创建一个被 'ngconstant:common' 使用的 aggregate.json 文件。如果我删除现有的 aggregate.json 文件,则 ngconstant:common 任务会出错,因为 aggregate.json 文件丢失(但文件确实在 ngconstant 运行后创建)。另外,如果我不删除该文件,而只是进行更改,例如更改 concat 使用的源文件中的版本号,则 ngconstant 创建的文件不会接受更改,因为它不会等到新的聚合。 json由concat创建。

编辑2:任务代码

concat: {
        options: {
            banner: '{"appConfig": {',
            footer: "}}",
            separator: ','
        },
        local: {
            src: ["app/config/common-config.json", "app/config/local.json"],
            dest: "app/config/aggregate-config.json"
        }
    },
ngconstant: {
        options: {
            space: '  ',
            wrap: '(function(){\n\n"use strict";\n\n {%= __ngModule %}\n\n}());',
            name: 'CoreConfig',
            dest: 'app/scripts/config.js'
        },
        common: {
            constants: grunt.file.readJSON('app/config/aggregate-config.json')
        }
}

【问题讨论】:

  • Grunt 按顺序运行任务列表,因此您的问题可能出在其他地方。是什么症状让您认为没有?
  • 好吧,我就是这么想的,所以我会三重检查所有任务。我确实在问题中添加了详细信息。
  • 从您的详细信息看来,您正在观看您的文件,并且在观看期间出现问题,而不是直接致电 local-debug。然后请发布您的watch 任务的完整配置。
  • 我在手动调用 local-debug 任务时发生了这种情况,但在任何正在运行的手表之外。
  • 嗯。你能发布你的 concat:local 配置吗?

标签: gruntjs grunt-contrib-concat grunt-contrib-copy


【解决方案1】:

好的,明白了。

Grunt 按照以下步骤工作:

  1. 它的整个 Gruntfile 被读取并且配置被 评估
  2. 然后它会构建要运行的任务列表
  3. 然后遍历列表,依次运行任务

所以发生的情况是,在 1. 期间,您的文件 aggregate-config.json 被读取并将 config.ngconstant.common.constants 设置为其 当前 值(您之前运行的结果)。 然后 3. 发生,并生成了一个新的aggregate-config.json,但没有使用(任务之间不会重新读取配置)。

但如果您将 字符串 传递给 ngconstant.constants,它会被解释为文件名并在 任务运行时读取(第 3 步),让您你想要的结果:

ngconstant: {
  common: {
    constants: 'app/config/aggregate-config.json'
  }
}

【讨论】:

    猜你喜欢
    • 2017-04-19
    • 1970-01-01
    • 2015-02-15
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 2012-05-06
    • 1970-01-01
    相关资源
    最近更新 更多