Skip to content

Commit 0989810

Browse files
committed
Open default browser if --open option is given
1 parent 0cc94c0 commit 0989810

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

bin/webpack-dev-server.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var path = require("path");
44
var url = require("url");
5+
var open = require("open");
56
var fs = require("fs");
67

78
// Local version replaces global one
@@ -44,8 +45,11 @@ var optimist = require("optimist")
4445

4546
.boolean("compress").describe("compress", "enable gzip compression")
4647

48+
.boolean("open").describe("open", "Open default browser")
49+
4750
.describe("port", "The port").default("port", 8080)
4851

52+
4953
.describe("host", "The hostname/ip address the server will bind to").default("host", "localhost");
5054

5155
require("webpack/bin/config-optimist")(optimist);
@@ -137,6 +141,9 @@ if(argv["history-api-fallback"])
137141
if(argv["compress"])
138142
options.compress = true;
139143

144+
if(argv["open"])
145+
options.open = true;
146+
140147
var protocol = options.https ? "https" : "http";
141148

142149
if(options.inline) {
@@ -156,16 +163,20 @@ if(options.inline) {
156163
}
157164

158165
new Server(webpack(wpOpt), options).listen(options.port, options.host, function(err) {
166+
var uri = protocol + "://" + options.host + ":" + options.port + "/";
167+
159168
if(err) throw err;
160169
if(options.inline)
161-
console.log(protocol + "://" + options.host + ":" + options.port + "/");
170+
console.log(uri);
162171
else
163-
console.log(protocol + "://" + options.host + ":" + options.port + "/webpack-dev-server/");
172+
console.log(uri + "webpack-dev-server/");
164173
console.log("webpack result is served from " + options.publicPath);
165174
if(typeof options.contentBase === "object")
166175
console.log("requests are proxied to " + options.contentBase.target);
167176
else
168177
console.log("content is served from " + options.contentBase);
169178
if(options.historyApiFallback)
170179
console.log("404s will fallback to %s", options.historyApiFallback.index || "/index.html");
180+
if (options.open)
181+
open(uri);
171182
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"express": "^4.13.3",
1313
"http-proxy": "^1.11.2",
1414
"optimist": "~0.6.0",
15+
"open": "0.0.5",
1516
"serve-index": "^1.7.2",
1617
"sockjs": "^0.3.15",
1718
"sockjs-client": "^1.0.3",

0 commit comments

Comments
 (0)