用grunt运行2个asynchronous任务

我正在一个小型的节点项目上工作,我使用coffeescript和更less的客户端代码。 我正在尝试使用grunt设置我的开发环境。 我已经实现了像这样运行服务器的自定义grunt任务:

start = require './start' #just a function to start express.js application grunt.registerTask 'server', 'Starting server', -> grunt.log.write 'Preparing server to start' done = do @async start (err) -> grunt.log.write "server running at localhost:4000" 

我也想用grunt-contrib-watch插件运行“watch”任务:

 grunt.initConfig watch: coffee: files: ['public/coffee/**/*.coffee'] tasks: ['coffee'] jade: files: ['public/jade/**/*.jade'] tasks: ['jade'] less: files: ['public/less/**/*.less'] tasks: ['less'] 

问题是:如何使这两个任务(手表和服务器)同时运行? 我想有一个服务器启动并运行,不想每次更改一些客户端代码时重新加载它。 提前致谢

将它前缀到你的监视任务,摆脱服务器任务内的done = do @async

tasks: ['server', 'coffee']

你想在你的Gruntconfiguration中为服务器任务指定一个选项,使其“长时间运行”。 然后,只有在需要长时间运行时才能调用@async (不包括监视任务)。

您可以使用这两个软件包中的任何一个同时运行两个或更多任务:

  1. 咕噜平行
  2. 咕噜并发

我有同样的问题,无法启动监视和连接服务器从一个咕task任务。

为了解决这个问题,我在Gruntfile中使用grunt-exec启动服务器作为后台进程

grunt connect:preview &结尾处的&符号(&) grunt connect:preview &是什么启动服务器作为后台进程。

 ,exec: { start_server: { command: 'grunt connect:preview &' } } 

然后像这样注册咕噜的任务

 grunt.registerTask('preview', ['clean:preview', 'template', 'exec', 'watch' ]); 

有一个更好的方法来做到这一点,但到目前为止,这是我能想到的最好的方法。