【问题标题】:Cannot get grunt-php to launch browser and connect无法让 grunt-php 启动浏览器并连接
【发布时间】:2013-11-11 14:18:59
【问题描述】:

当我在 Gruntfile.js 中为我的 HTML 文件使用 grunt-contrib-connect 并调用任务时,浏览器会启动并毫无问题地与 LiveReload 连接。

我现在正尝试对 PHP 文件做同样的事情,所以我使用 grunt-php。作者指出,grunt-php “几乎是 grunt-contrib-connect 的直接替代品”

我已将 grunt-contrib-connect 的选项复制到 grunt-php 任务中,并添加了 keepaliveopen,但浏览器没有启动,也没有建立连接。然而终端显示:

Running "php:livereload" (php) task PHP 5.4.17 Development Server started at Mon Nov 11 15:56:04 2013 Listening on http://localhost:9000 Document root is /Users/fisu/Sites/generator-site-playground/dev

我的任务如下:

php: {
    options: {
        keepalive: true,
        open: true,
        port: 9000,
        livereload: 35729,
        hostname: 'localhost',
        base: 'dev'
    },
    livereload: {
        options: {
            open: 'http://localhost:9000',
            base: 'dev'
        }
    }
}

我尝试了不同的主机名,但浏览器仍然无法启动和连接。我错过了一个选项吗?

【问题讨论】:

    标签: gruntjs


    【解决方案1】:

    不只是你。问题似乎出在这种方法上:https://github.com/sindresorhus/grunt-php/blob/master/tasks/php.js#L51-L59 - open() 似乎永远不会被调用,因为它正在等待 HTTP 200,除非浏览器访问服务器并请求某些东西,否则它不会得到它。至少在我的机器上似乎是这样,我不知道该方法的测试效果如何。一个临时的解决办法是将该调用移出该方法;进入node_modules/grunt-php/tasks/ 目录并像这样编辑文件:

    (l49) // check when the server is ready. tried doing it by listening
          // to the child process `data` event, but it's not triggered...
          checkServer('http://' + host, function () {
              if (!this.flags.keepalive && !options.keepalive) {
                  cb();
              }
          }.bind(this));
          // open the browser straight away
          if (options.open) {
              open('http://' + host);
          }
    

    我也提交了一个问题。 https://github.com/sindresorhus/grunt-php/issues/14

    【讨论】:

    • 太棒了,感谢您对此进行调查并引起 Sindre 的注意。
    猜你喜欢
    • 1970-01-01
    • 2020-10-02
    • 2023-03-10
    • 2016-09-25
    • 2021-02-08
    • 2020-12-10
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    相关资源
    最近更新 更多