【发布时间】:2013-07-02 16:00:08
【问题描述】:
是否可以在 gruntfile 或命令提示符中指定特定的浏览器(除了操作系统的默认浏览器)?例如。 “grunt server firefox”之类的。我的默认浏览器是 Chrome,但我想在多个浏览器中测试/开发我的网站。我在 Yeoman 中使用 GruntJS。
布莱恩
【问题讨论】:
是否可以在 gruntfile 或命令提示符中指定特定的浏览器(除了操作系统的默认浏览器)?例如。 “grunt server firefox”之类的。我的默认浏览器是 Chrome,但我想在多个浏览器中测试/开发我的网站。我在 Yeoman 中使用 GruntJS。
布莱恩
【问题讨论】:
在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"。
open 配置对象是什么样的?任务如何调用它?
来自 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: '谷歌浏览器'
【讨论】:
虽然这里的答案有助于解决我遇到的问题,但作为一个不太熟悉 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: {
//...
【讨论】:
grunt server 任务几乎与浏览器无关,它只是启动一个静态服务器供您连接和预览您的应用程序。理论上你可以使用任何你想连接的浏览器http://localhost:8080/
根据发帖者的评论澄清一下:
grunt-open 是与 grunt-server 不同的任务:https://npmjs.org/package/grunt-open.grunt-open 使用 node-open,它默认为 darwin 的默认 open 任务或 win32 的 start:https://github.com/jjrdn/node-open#how-it-works
因此,回答一下,您指定打开.html 文件的任何应用程序(或您正在打开的任何应用程序)都将与此任务一起打开。
【讨论】:
open: { server: { path: 'http://localhost:<%= connect.options.port %>' } },我想知道的是它是否有办法调用特定的浏览器或者如果它受制于操作系统默认浏览器设置