1

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

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

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

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

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

4

1 回答 1

2

不只是你。问题似乎出在这种方法上: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

于 2013-11-11T23:41:08.887 回答