使用 grunt 运行 2 个异步任务

2022-01-24 00:00:00 node.js javascript gruntjs 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 插件运行监视"任务:

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

问题是:如何让这两个任务(watch和server)同时运行?我想让服务器启动并运行,并且不想在每次更改某些客户端代码时重新加载它.提前致谢

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

推荐答案

给你的watch任务加前缀,去掉服务器任务里面的done = do @async.

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

任务:['server', 'coffee']

您想在 Grunt 配置中指定一个选项,以使服务器任务长时间运行".然后你可以调用 @async 只有当你需要它长时间运行(没有监视任务).

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).

相关文章