Skip to content

Commit 83cff95

Browse files
committed
Merge pull request #329 from CodeYellowBV/open-browser
Open default browser if --open option is given
2 parents e85e8f6 + 98fd2f4 commit 83cff95

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

bin/webpack-dev-server.js

Lines changed: 14 additions & 4 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
@@ -46,8 +47,11 @@ var optimist = require("optimist")
4647

4748
.boolean("compress").describe("compress", "enable gzip compression")
4849

50+
.boolean("open").describe("open", "Open default browser")
51+
4952
.describe("port", "The port").default("port", 8080)
5053

54+
5155
.describe("host", "The hostname/ip address the server will bind to").default("host", "localhost");
5256

5357
require("webpack/bin/config-optimist")(optimist);
@@ -147,6 +151,9 @@ if(argv["history-api-fallback"])
147151
if(argv["compress"])
148152
options.compress = true;
149153

154+
if(argv["open"])
155+
options.open = true;
156+
150157
var protocol = options.https ? "https" : "http";
151158

152159
if(options.inline) {
@@ -166,16 +173,19 @@ if(options.inline) {
166173
}
167174

168175
new Server(webpack(wpOpt), options).listen(options.port, options.host, function(err) {
176+
var uri = protocol + "://" + options.host + ":" + options.port + "/";
177+
if(!options.inline)
178+
uri += "webpack-dev-server/";
179+
169180
if(err) throw err;
170-
if(options.inline)
171-
console.log(protocol + "://" + options.host + ":" + options.port + "/");
172-
else
173-
console.log(protocol + "://" + options.host + ":" + options.port + "/webpack-dev-server/");
181+
console.log(uri);
174182
console.log("webpack result is served from " + options.publicPath);
175183
if(typeof options.contentBase === "object")
176184
console.log("requests are proxied to " + options.contentBase.target);
177185
else
178186
console.log("content is served from " + options.contentBase);
179187
if(options.historyApiFallback)
180188
console.log("404s will fallback to %s", options.historyApiFallback.index || "/index.html");
189+
if (options.open)
190+
open(uri);
181191
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"connect-history-api-fallback": "1.1.0",
1212
"express": "^4.13.3",
1313
"optimist": "~0.6.0",
14+
"open": "0.0.5",
1415
"serve-index": "^1.7.2",
1516
"sockjs": "^0.3.15",
1617
"sockjs-client": "^1.0.3",

0 commit comments

Comments
 (0)