如何同时运行两个grunt监视任务

Vit*_*kov 58 gruntjs grunt-contrib-watch

是否可以同时运行两个监视任务?

我知道我可以在手表设置中拥有任何我想要的任务,只需启动grunt手表,它就会观看所有这些,就像这样

...
watch: {
    A: {
        files: "js/dev/**/*.coffee",
        tasks: ["coffee", "requirejs"]
    },
    B: {
        files: "js/dev/**/*.coffee",
        tasks: ["coffee"]
    },
    C: {
        files: "js/dev/**/*.html",
        tasks: ["copy"]
    }
}
...
Run Code Online (Sandbox Code Playgroud)

......但我不需要这个.我只想拥有不同的开发和生产任务.你可以猜到,A(生产)和B(开发)之间的唯一区别是缩小和连接.我不需要同时启动AB任务.

首先,我提出了这个想法

grunt.registerTask("prod", ["watch:A", "watch:C"]);
grunt.registerTask("dev", ["watch:B", "watch:C"]);
Run Code Online (Sandbox Code Playgroud)

但这没效果.只是第一次观察任务正在运行(C永远不会起作用).这可能做我想要的吗?

Nic*_*ery 74

我发现使用grunt-concurrent工作:

concurrent: {
  options: {
    logConcurrentOutput: true
  },
  prod: {
    tasks: ["watch:A", "watch:C"]
  },
  dev: {
    tasks: ["watch:B", "watch:C"]
  }
}
Run Code Online (Sandbox Code Playgroud)

然后:

grunt.registerTask("prod", ["concurrent:prod"]);
grunt.registerTask("dev", ["concurrent:dev"]);
Run Code Online (Sandbox Code Playgroud)

  • 这对我不起作用.我有一个'致命错误:端口35729已经被另一个进程使用了​​'当同时运行两个监视时.有线索吗? (7认同)
  • @ jmcollin92 grunt-focus对我很有用,但我不太清楚为什么它会成为"最好的,唯一可行的解​​决方案".grunt-concurrent也可以工作,它可以同时执行任何任务.grunt-focus仅限于观看目标. (4认同)
  • @hashchange我同意你的看法.grunt-focus仅对观看目标有价值.grunt-concurrent适用于所有其他目的. (2认同)
  • @ jmcollin92当我第一次运行它时,我得到了相同的"致命错误:端口35729已被另一个进程使用".我的手表配置有多种配置.我只需要为每个端口设置一个不同的livereload端口.[这是一个小问题](http://jsfiddle.net/dk44x00t/) (2认同)

Rob*_*obW 18

编辑:并发现在有一个logConcurrentOutput选项!更多信息:https://github.com/sindresorhus/grunt-concurrent#logconcurrentoutput.

Watch是一个奇怪的并发但阻塞的任务,因此您必须具有创造性才能使多任务类功能正常工作.

并发丢失了监视任务的所有输出,这并不理想.

尝试在自定义任务中动态编写配置对象:

grunt.registerTask('watch:test', function() {
  // Configuration for watch:test tasks.
  var config = {
    options: {
      interrupt: true
    },
    unit: {
      files: [
        'test/unit/**/*.spec.coffee'
      ],
      tasks: ['karma:unit']
    },
    integration: {
      files: [
        'test/integration/**/*.rb',
        '.tmp/scripts/**/*.js'
      ],
      tasks: ['exec:rspec']
    }
  };

  grunt.config('watch', config);
  grunt.task.run('watch');
});
Run Code Online (Sandbox Code Playgroud)


jmc*_*n92 11

有最好和唯一可行的解​​决方案:https://npmjs.org/package/grunt-focus 添加此插件,然后:

focus: {
            sources: {
                include: ['js', 'html', 'css', 'grunt']
            },
            testu: {
                include: ['js', 'html', 'css', 'testu', 'grunt']
            },
            testi: {
                include: ['js', 'html', 'css', 'testu', 'testi', 'grunt']
            }
        },
        watch: {
            js: {
                files: paths.js,
                tasks: ['jshint'],
                options: {
                    livereload: true
                }
            },
            html: {
                files: paths.html,
                options: {
                    livereload: true
                }
            },
            css: {
                files: paths.css,
                tasks: ['csslint'],
                options: {
                    livereload: true
                }
            },
            testu: {
                files: ['test/**/*.js', 'test/**/*.css'],
                tasks: ['mochaTest'],
                options: {}
            },
            testi: {
                files: ['test/**/*.js', 'test/**/*.css'],
                tasks: ['exec:cleanTestDB', 'protractor_webdriver', 'protractor'],
                options: {}
            },
            grunt: {
                files: ['Gruntfile.js', 'server/config/env/*.js'],
                options: {
                    reload: true
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

然后你使用焦点:来源或焦点:testu作为您的方便.

JM.

  • grunt-concurrent不适用于具有同一端口的livereload的多个watch.这样做了,谢谢! (3认同)

Ale*_*der 7

grunt-concurrent或grunt-focus都是很好的解决方案,但它们都破坏了livereload功能.

我的解决方案是动态组合手表配置,假设您不会同时运行这两种配置.

你可以做这样的事情

grunt.config.merge({
  watch: {
    options: {
      livereload: true
    },
    C: {
      files: "js/dev/**/*.html",
      tasks: ["copy"]
    }
  }
});

grunt.registerTask('watch-forProd', function () {
  grunt.config.merge({
    watch: {
      A: {
        files: "js/dev/**/*.coffee",
        tasks: ["coffee", "requirejs"]
      }
    }
  });

  grunt.task.run('watch');
});

grunt.registerTask('watch-forDev', function () {
  grunt.config.merge({
    watch: {
      B: {
        files: "js/dev/**/*.coffee",
        tasks: ["coffee"]
      }
    }
  });

  grunt.task.run('watch');
});

grunt.registerTask("prod", ["watch-forProd"]);
grunt.registerTask("dev", ["watch-forDev"]);
Run Code Online (Sandbox Code Playgroud)