【问题标题】:Running 2 async tasks with grunt使用 grunt 运行 2 个异步任务
【发布时间】:2013-01-23 20:42:32
【问题描述】:

我正在开发一个小型节点项目,我使用咖啡脚本而不是客户端代码。我正在尝试使用 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 插件运行“监视”任务:

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)同时运行?我想让服务器启动并运行,并且不想每次更改某些客户端代码时都重新加载它。提前致谢

【问题讨论】:

    标签: javascript node.js coffeescript gruntjs


    【解决方案1】:

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

    1. grunt-parallel
    2. grunt-concurrent

    【讨论】:

      【解决方案2】:

      将其添加到您的监视任务中,并去掉服务器任务中的done = do @async

      tasks: ['server', 'coffee']

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

      【讨论】:

      • 这不起作用,因为服务器任务没有结束,因此永远不会将控制权交还给监视任务。
      • 是的,它确实结束了;正如我在回答中解释的那样,您删除了允许任务结束的 async() 调用,然后根据选项有条件地将其添加回来。
      • 我很困惑。他希望服务器始终长时间运行,他希望手表长期运行,并且他希望它们在一次 grunt 调用中同时运行。这就是你在这里实现的目标吗?看起来不像
      【解决方案3】:

      我遇到了同样的问题,即无法从 grunt 任务启动监视和连接服务器。

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

      grunt connect:preview & 末尾的和号 (&) 将服务器作为后台进程启动。

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

      然后像这样注册 grunt 任务

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

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

      【讨论】:

      • 我使用的是 grunt-shell 而不是 grunt-exec,& 似乎没有效果。
      猜你喜欢
      • 1970-01-01
      • 2014-02-17
      • 2023-01-10
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      • 2011-04-15
      • 1970-01-01
      • 2010-10-25
      相关资源
      最近更新 更多