使用grunt运行两个异步任务 [英] Running 2 async tasks with grunt

查看:502
本文介绍了使用grunt运行两个异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on a small node project and I use coffeescript and less for client-side code. I am trying to set up my development environment using grunt. I've implemented custom grunt task for running server like this:

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任务:

I also want to run the "watch" task using grunt-contrib-watch plugin:

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

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

The question is: How to make this two tasks (watch and server) run simultaneously? I want to have a server up and running and don't want to reload it every time some client-side code is changed. Thanks in advance

推荐答案

将其添加到您的观察任务中,摆脱 done = do @

Prefix it to your watch tasks, and get rid of the done = do @async inside the server task.

tasks:['server','coffee']

您希望在Grunt配置中指定一个选项,使服务器任务成为长时间运行。然后你可以调用 @async 仅当你需要它长时间运行(没有watch任务)。

You want to specify an option in your Grunt configuration for the server task to be "long-running" or not. Then you can call @async only if you need it to be long running (without the watch task).

这篇关于使用grunt运行两个异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆