Skip to content

Commit 272c78a

Browse files
NewFutureSpaceK33z
authored andcommitted
Fixed ipv6 [::] (#676)
* fixed ipv6 parsing and listening [::] with latest * add example
1 parent 9b7a14e commit 272c78a

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

bin/webpack-dev-server.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,15 @@ function processOptions(wpOpt) {
281281

282282
var protocol = options.https ? "https" : "http";
283283

284+
// the formated domain (url without path) of the webpack server
285+
var domain = url.format({
286+
protocol: protocol,
287+
hostname: options.host,
288+
port: options.socket ? 0 : options.port.toString()
289+
});
290+
284291
if(options.inline !== false) {
285-
var devClient = [require.resolve("../client/") + "?" + protocol + "://" + (options.public || (options.host + ":" + options.port))];
292+
var devClient = [require.resolve("../client/") + "?" + (options.public ? protocol + "://" + options.public : domain)];
286293

287294
if(options.hotOnly)
288295
devClient.push("webpack/hot/only-dev-server");
@@ -308,12 +315,7 @@ function processOptions(wpOpt) {
308315
}));
309316
}
310317

311-
var uri = url.format({
312-
protocol: protocol,
313-
hostname: options.host,
314-
pathname: options.inline !== false ? "/" : "webpack-dev-server/",
315-
port: options.socket ? 0 : options.port.toString()
316-
});
318+
var uri = domain + (options.inline !== false ? "/" : "webpack-dev-server/");
317319

318320
var server = new Server(compiler, options);
319321

client/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ var onSocketMsg = {
8989
var hostname = urlParts.hostname;
9090
var protocol = urlParts.protocol;
9191

92-
if(urlParts.hostname === "0.0.0.0") {
92+
93+
//check ipv4 and ipv6 `all hostname`
94+
if(hostname === "0.0.0.0" || hostname === "::") {
9395
// why do we need this check?
9496
// hostname n/a for file protocol (example, when using electron, ionic)
9597
// see: https://github.com/webpack/webpack-dev-server/pull/384

examples/host-port/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
# host and port
22

3+
Only For ipv4
34
```shell
45
node ../../bin/webpack-dev-server.js --open --port 5000 --host 0.0.0.0
56
```
67

8+
For ipv6 support. (it also works with ipv4.)
9+
```shell
10+
node ../../bin/webpack-dev-server.js --open --port 5000 --host ::
11+
```
12+
713
We want to change the port to `5000`, and make the server publicly accessible.
814

915
## What should happen
1016

11-
The script should open `http://0.0.0.0:5000/`. You should see "It's working."
17+
The script should open `http://0.0.0.0:5000/`(ipv4) or `http://[::]:5000/`(ipv6). You should see "It's working."
1218

1319
Get your local ip (e.g. `192.168.1.40`), and try it from `192.168.1.40:5000`. Make sure your firewall doesn't block this port.

0 commit comments

Comments
 (0)