Skip to content

Commit 06b3d91

Browse files
authored
fix: show correct url in output status (#3013)
1 parent 3945c44 commit 06b3d91

File tree

3 files changed

+9
-86
lines changed

3 files changed

+9
-86
lines changed

lib/Server.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -588,14 +588,14 @@ class Server {
588588
const useColor = getColorsOption(getCompilerConfigArray(this.compiler));
589589

590590
const protocol = this.options.https ? 'https' : 'http';
591-
const { hostname, port } = this;
591+
const { address: hostname, port } = this.server.address();
592592
const prettyPrintUrl = (newHostname) =>
593593
url.format({ protocol, hostname: newHostname, port, pathname: '/' });
594594

595595
let prettyHostname;
596596
let lanUrlForTerminal;
597597

598-
if (hostname === '0.0.0.0' || hostname === '::') {
598+
if (hostname === '0.0.0.0' || ip.isLoopback(hostname)) {
599599
prettyHostname = 'localhost';
600600

601601
const localIP =

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

-39
This file was deleted.

test/cli/cli.test.js

+7-45
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,7 @@ const internalIp = require('internal-ip');
66
const testBin = require('../helpers/test-bin');
77
const isWebpack5 = require('../helpers/isWebpack5');
88

9-
// skip if webpack-dev-server is not linked
10-
let runCLITest = describe;
11-
let basePath;
12-
13-
try {
14-
basePath = path.join(require.resolve('webpack-dev-server'), '..', '..');
15-
} catch {
16-
runCLITest = describe.skip;
17-
}
18-
19-
runCLITest('CLI', () => {
20-
/* Based on webpack/test/StatsTestCases.test.js */
21-
/**
22-
* Escapes regular expression metacharacters
23-
* @param {string} str String to quote
24-
* @returns {string} Escaped string
25-
*/
26-
const quotemeta = (str) => str.replace(/[-[\]\\/{}()*+?.^$|]/g, '\\$&');
27-
28-
const normalizeOutput = (output) =>
29-
output
30-
// eslint-disable-next-line no-control-regex
31-
.replace(/\u001b\[[0-9;]*m/g, '')
32-
.replace(/[.0-9]+(\s?)(ms|KiB|bytes)/g, 'X$1$2')
33-
.replace(
34-
/(Built at:) (.*)$/gm,
35-
'$1 Thu Jan 01 1970 <CLR=BOLD>00:00:00</CLR> GMT'
36-
)
37-
.replace(/webpack [^ )]+/g, 'webpack x.x.x')
38-
.replace(new RegExp(quotemeta(basePath.replace(/\\/g, '/')), 'g'), 'Xdir')
39-
.replace(new RegExp(quotemeta(basePath), 'g'), 'Xdir')
40-
.replace(/[\\/]public/, '/public')
41-
.replace(/(Hash:) [a-z0-9]+/g, '$1 X')
42-
.replace(/ dependencies:Xms/g, '')
43-
.replace(/, additional resolving: X ms/g, '');
44-
9+
describe('CLI', () => {
4510
const webpack4Test = isWebpack5 ? it.skip : it;
4611
const webpack5Test = isWebpack5 ? it : it.skip;
4712

@@ -66,22 +31,21 @@ runCLITest('CLI', () => {
6631
});
6732

6833
webpack5Test('--hot webpack 5', (done) => {
69-
// host doesn't default to `localhost`
70-
testBin('--hot --host localhost')
34+
// need detailed stats to check for 'dev-server.js'
35+
testBin('--hot --stats=detailed')
7136
.then((output) => {
7237
expect(output.exitCode).toEqual(0);
73-
expect(normalizeOutput(output.stderr)).toMatchSnapshot();
38+
expect(output.stderr).toContain('webpack/hot/dev-server.js');
7439
done();
7540
})
7641
.catch(done);
7742
});
7843

7944
webpack5Test('--no-hot webpack 5', (done) => {
80-
// host doesn't default to `localhost`
81-
testBin('--no-hot --host localhost')
45+
testBin('--no-hot --stats=detailed')
8246
.then((output) => {
8347
expect(output.exitCode).toEqual(0);
84-
expect(normalizeOutput(output.stderr)).toMatchSnapshot();
48+
expect(output.stderr).not.toContain('webpack/hot/dev-server.js');
8549
done();
8650
})
8751
.catch(done);
@@ -119,9 +83,7 @@ runCLITest('CLI', () => {
11983
.catch(done);
12084
});
12185

122-
// TODO: enable after fixing url bug with undefined host
123-
// https://github.com/webpack/webpack-dev-server/pull/2992#discussion_r571360196
124-
it.skip('unspecified host and port', (done) => {
86+
it('unspecified host and port', (done) => {
12587
testBin('')
12688
.then((output) => {
12789
expect(/http:\/\/localhost:[0-9]+/.test(output.stderr)).toEqual(true);

0 commit comments

Comments
 (0)