Skip to content

Commit bfc7f86

Browse files
fix: use localhost for output and open
1 parent 2de2e01 commit bfc7f86

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

lib/Server.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,9 @@ class Server {
597597
let networkUrlForTerminalIPv6;
598598

599599
const parsedIP = ipaddr.parse(hostname);
600+
const isUnspecified = parsedIP.range() === 'unspecified';
600601

601-
if (parsedIP.range() === 'unspecified') {
602+
if (isUnspecified) {
602603
localhostForTerminal = prettyPrintUrl('localhost');
603604

604605
const networkIPv4 = internalIp.v4.sync();
@@ -615,6 +616,10 @@ class Server {
615616
}
616617
}
617618
} else {
619+
if (parsedIP.range() === 'loopback') {
620+
localhostForTerminal = prettyPrintUrl('localhost');
621+
}
622+
618623
if (parsedIP.kind() === 'ipv6') {
619624
if (parsedIP.isIPv4MappedAddress()) {
620625
networkUrlForTerminalIPv4 = prettyPrintUrl(
@@ -630,7 +635,7 @@ class Server {
630635
}
631636
}
632637

633-
if (localhostForTerminal) {
638+
if (isUnspecified) {
634639
this.logger.info('Project is running at:');
635640
this.logger.info(`Local: ${colors.info(useColor, localhostForTerminal)}`);
636641

@@ -649,6 +654,11 @@ class Server {
649654
this.logger.info(`On Your Network: ${networkUrlsForTerminal.join(', ')}`);
650655
} else {
651656
const networkUrlsForTerminal = []
657+
.concat(
658+
localhostForTerminal
659+
? [`${colors.info(useColor, localhostForTerminal)}`]
660+
: []
661+
)
652662
.concat(
653663
networkUrlForTerminalIPv4
654664
? [`${colors.info(useColor, networkUrlForTerminalIPv4)} (IPv4)`]

test/cli/__snapshots__/cli.test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exports[`CLI --host :: (IPv6): stderr 1`] = `
88
`;
99
1010
exports[`CLI --host ::1 (IPv6): stderr 1`] = `
11-
"<i> [webpack-dev-server] Project is running at http://[::1]:<port>/ (IPv6)
11+
"<i> [webpack-dev-server] Project is running at http://localhost:<port>/, http://[::1]:<port>/ (IPv6)
1212
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
1313
`;
1414
@@ -30,7 +30,7 @@ exports[`CLI --host 0:0:0:0:0:FFFF:7F00:0001 (IPv6): stderr 1`] = `
3030
`;
3131
3232
exports[`CLI --host 127.0.0.1 (IPv4): stderr 1`] = `
33-
"<i> [webpack-dev-server] Project is running at http://127.0.0.1:<port>/ (IPv4)
33+
"<i> [webpack-dev-server] Project is running at http://localhost:<port>/, http://127.0.0.1:<port>/ (IPv4)
3434
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
3535
`;
3636
@@ -42,11 +42,11 @@ exports[`CLI --host and --port are unspecified: stderr 1`] = `
4242
`;
4343
4444
exports[`CLI --host localhost --port 9999: stderr 1`] = `
45-
"<i> [webpack-dev-server] Project is running at http://127.0.0.1:<port>/ (IPv4)
45+
"<i> [webpack-dev-server] Project is running at http://localhost:<port>/, http://127.0.0.1:<port>/ (IPv4)
4646
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
4747
`;
4848
4949
exports[`CLI --host localhost: stderr 1`] = `
50-
"<i> [webpack-dev-server] Project is running at http://127.0.0.1:<port>/ (IPv4)
50+
"<i> [webpack-dev-server] Project is running at http://localhost:<port>/, http://127.0.0.1:<port>/ (IPv4)
5151
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
5252
`;

test/server/open-option.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ open.mockImplementation(() => {
1515
});
1616

1717
describe('open option', () => {
18-
it.only('should work with unspecified host', (done) => {
18+
it('should work with unspecified host', (done) => {
1919
const compiler = webpack(config);
2020
const server = new Server(compiler, {
2121
open: true,
@@ -27,7 +27,7 @@ describe('open option', () => {
2727
server.close(() => {
2828
expect(open.mock.calls[0]).toMatchInlineSnapshot(`
2929
Array [
30-
"http://127.0.0.1:8117/",
30+
"http://localhost:8117/",
3131
Object {
3232
"wait": false,
3333
},
@@ -54,7 +54,7 @@ describe('open option', () => {
5454
server.close(() => {
5555
expect(open.mock.calls[1]).toMatchInlineSnapshot(`
5656
Array [
57-
"http://127.0.0.1:8117/index.html",
57+
"http://localhost:8117/index.html",
5858
Object {
5959
"wait": false,
6060
},
@@ -80,7 +80,7 @@ describe('open option', () => {
8080
server.close(() => {
8181
expect(open.mock.calls[0]).toMatchInlineSnapshot(`
8282
Array [
83-
"http://127.0.0.1:8117/",
83+
"http://localhost:8117/",
8484
Object {
8585
"wait": false,
8686
},
@@ -107,7 +107,7 @@ describe('open option', () => {
107107
server.close(() => {
108108
expect(open.mock.calls[0]).toMatchInlineSnapshot(`
109109
Array [
110-
"http://127.0.0.1:8117/",
110+
"http://localhost:8117/",
111111
Object {
112112
"wait": false,
113113
},
@@ -134,7 +134,7 @@ describe('open option', () => {
134134
server.close(() => {
135135
expect(open.mock.calls[0]).toMatchInlineSnapshot(`
136136
Array [
137-
"http://127.0.0.1:8117/",
137+
"http://localhost:8117/",
138138
Object {
139139
"wait": false,
140140
},

0 commit comments

Comments
 (0)