gruntjs - 在 Grunt 中按顺序运行任务

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

我的印象是,在 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 按照以下步骤工作:

  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'
  }
}

关于gruntjs - 在 Grunt 中按顺序运行任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32616051/

相关文章:

node.js - 如何使用 GruntJS 运行一个特定的 CucumberJS 功能?

javascript - 使用 Grunt 的 ENOTSUP

javascript - Grunt 任务陷入无限循环

javascript - 为什么 Grunt Copy 会给我 'undefined is not a function' 警告?

css - grunt-cssc 不工作 "cannot read property ' 类型'"~ Foundation 5

css - Stylus ParseError : expected “indent” , 得到 “;”

node.js - 从 Grunt 任务设置 NODE_ENV

gruntjs - 使用 Grunt.js 连接 php 文件和 Javascript

gruntjs - Grunt 复制任务无法保留目录结构