使用各种浏览器运行grunt服务器

时间:2013-07-02 16:00:08

标签: gruntjs yeoman

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

布赖恩

4 个答案:

答案 0 :(得分:11)

快速回答

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: "chromium-browser --incognito" - @ bk11425

答案 1 :(得分: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:&#39; Google Chrome&#39;

答案 2 :(得分:2)

虽然这里的答案有助于解决我出现的问题,但是作为一个对grunt不熟悉的人,我很难确定我应该把放在哪里 &#34;开放:&#34;我的Gruntfile.js中的节。我花了大约三次尝试才找到合适的位置(例如我直接在&#39; grunt.initConfig&#39;以及&#39; connect:options:&#39;下无效)

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

我发布我把它放在这个文件中的地方只是为了提供更多&#34; context&#34;对于任何处于类似困境的人。

以下是Gruntfile.js的相关片段:

// ...
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: {
//...

答案 3 :(得分:1)

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

的任何浏览器

根据海报的评论澄清:

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

因此,要回答,您指定打开start个文件(或任何正在打开的文件)的任何应用程序都将打开此任务。