【问题标题】:Run grunt server with various browsers使用各种浏览器运行 grunt 服务器
【发布时间】:2013-07-02 16:00:08
【问题描述】:

是否可以在 gruntfile 或命令提示符中指定特定的浏览器(除了操作系统的默认浏览器)?例如。 “grunt server firefox”之类的。我的默认浏览器是 Chrome,但我想在多个浏览器中测试/开发我的网站。我在 Yeoman 中使用 GruntJS。

布莱恩

【问题讨论】:

    标签: gruntjs yeoman


    【解决方案1】:

    快速解答

    Gruntfile.js 中可以添加app 参数:

    open: {
      server: {
        url: 'http://localhost:<%= connect.options.port %>',
        app: 'firefox'
      }
    },
    

    拉取请求:https://github.com/jsoverson/grunt-open/pull/7

    提交:https://github.com/GabLeRoux/grunt-open/commit/6f2c7069767e58160ec96aaaa8fa20ed210ba183


    命令行参数

    可以在app字符串中传递命令行参数,如app: "chromium-browser --incognito"-@bk11425

    【讨论】:

    • 抱歉回复晚了,感谢您的更新!效果很好。
    • 如果有人感兴趣,我还注意到可以在应用字符串中传递命令行参数,例如app: "chromium-browser --incognito"
    • 我可以通过您的修复在任何浏览器中启动该应用程序,但它也可以在默认浏览器中启动(以及目标浏览器)。知道如何解决吗?
    • @Nilesh 您的open 配置对象是什么样的?任务如何调用它?
    • @Nilesh:你能把 grunt 控制台输出添加到小提琴吗?
    【解决方案2】:

    来自 grunt connect 的文档:https://github.com/gruntjs/grunt-contrib-connect

    你可以使用:

        open: {
            target: 'http://localhost:8000', // target url to open
            appName: 'open', // name of the app that opens, ie: open, start, xdg-open
            callback: function() {} // called when the app has opened
        }
    

    即appName: '谷歌浏览器'

    【讨论】:

      【解决方案3】:

      虽然这里的答案有助于解决我遇到的问题,但作为一个不太熟悉 grunt 的人,我很难弄清楚我应该将“打开: " 我的 Gruntfile.js 中的节。我花了大约三三遍才找到正确的位置(例如,我直接在 'grunt.initConfig' 和 'connect: options:' 下尝试过,但没有效果)

      我正在使用由standard angular yeoman generator 生成的 Gruntfile.js。

      我将其发布在此文件中的位置,只是为了为处于类似困境中的任何人提供更多“背景”。

      这里是Gruntfile.js的相关sn-p:

      // ...
      The actual grunt server settings
      connect: {
        options: {
          port: 9000,
          // Change this to '0.0.0.0' to access the server from outside.
          hostname: 'localhost',
          livereload: 35729
        },
        livereload: {
          options: {
            //open: true, <- original line, comment out
            // add this
            open: {
              //target: 'http://localhost:9000', <- this works too
              target: 'http://localhost:<%= connect.options.port %>',
              appName: 'firefox'
            },
            // end add
            middleware: function (connect) {
              return [
                connect.static('.tmp'),
                connect().use(
                  '/bower_components',
                  connect.static('./bower_components')
                ),
                connect().use(
                  '/app/styles',
                  connect.static('./app/styles')
                ),
                connect.static(appConfig.app)
              ];
            }
          }
        },
        test: {
      //...
      

      【讨论】:

        【解决方案4】:

        grunt server 任务几乎与浏览器无关,它只是启动一个静态服务器供您连接和预览您的应用程序。理论上你可以使用任何你想连接的浏览器http://localhost:8080/

        根据发帖者的评论澄清一下:

        grunt-open 是与 grunt-server 不同的任务:https://npmjs.org/package/grunt-open.grunt-open 使用 node-open,它默认为 darwin 的默认 open 任务或 win32 的 starthttps://github.com/jjrdn/node-open#how-it-works

        因此,回答一下,您指定打开.html 文件的任何应用程序(或您正在打开的任何应用程序)都将与此任务一起打开。

        【讨论】:

        • 确实如此,但是在gruntfile中,有一个调用默认浏览器的设置:open: { server: { path: 'http://localhost:&lt;%= connect.options.port %&gt;' } },我想知道的是它是否有办法调用特定的浏览器或者如果它受制于操作系统默认浏览器设置
        • 看来我不是第一个提出这个问题的人:[github.com/onehealth/grunt-open/issues/3]
        猜你喜欢
        • 1970-01-01
        • 2018-12-21
        • 1970-01-01
        • 2012-03-16
        • 1970-01-01
        • 2019-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多