Skip to content

Commit 26cdab3

Browse files
refactor: code
1 parent 1000f2b commit 26cdab3

File tree

6 files changed

+116
-62
lines changed

6 files changed

+116
-62
lines changed

lib/Server.js

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const routes = require('./utils/routes');
1414
const getSocketServerImplementation = require('./utils/getSocketServerImplementation');
1515
const getCompilerConfigArray = require('./utils/getCompilerConfigArray');
1616
const setupExitSignals = require('./utils/setupExitSignals');
17+
const getStatsOption = require('./utils/getStatsOption');
18+
const getColorsOption = require('./utils/getColorsOption');
1719
const schema = require('./options.json');
1820

1921
if (!process.env.WEBPACK_SERVE) {
@@ -40,6 +42,7 @@ class Server {
4042
this.wsHeartbeatInterval = 30000;
4143

4244
normalizeOptions(this.compiler, this.options);
45+
4346
this.applyDevServerPlugin();
4447

4548
this.SocketServerImplementation = getSocketServerImplementation(
@@ -629,8 +632,6 @@ class Server {
629632
}
630633

631634
showStatus() {
632-
const getColorsOption = require('./utils/getColorsOption');
633-
634635
const useColor = getColorsOption(getCompilerConfigArray(this.compiler));
635636
const protocol = this.options.https ? 'https' : 'http';
636637
const { address, port } = this.server.address();
@@ -761,38 +762,55 @@ class Server {
761762
}
762763

763764
listen(port, hostname, fn) {
764-
if (typeof hostname !== 'undefined' && hostname !== this.options.host) {
765-
throw new Error(
766-
'The host specified in options is different from the host passed as an argument.'
765+
if (
766+
typeof port !== 'undefined' &&
767+
typeof this.options.port !== 'undefined' &&
768+
port !== this.options.port
769+
) {
770+
this.options.port = port;
771+
772+
this.logger.warn(
773+
'The "port" specified in options is different from the port passed as an argument. Will be used from arguments.'
767774
);
768775
}
769776

770-
if (typeof port !== 'undefined' && port !== this.options.port) {
777+
if (!this.options.port) {
778+
this.options.port = port;
779+
}
780+
781+
if (
782+
typeof hostname !== 'undefined' &&
783+
typeof this.options.host !== 'undefined' &&
784+
hostname !== this.options.host
785+
) {
786+
this.options.host = hostname;
787+
771788
this.logger.warn(
772-
'The port specified in options is different from the port passed as an argument.'
789+
'The "host" specified in options is different from the host passed as an argument. Will be used from arguments.'
773790
);
774791
}
775792

776-
hostname = this.options.host;
793+
if (!this.options.host) {
794+
this.options.host = hostname;
795+
}
777796

778-
if (hostname === 'local-ip') {
797+
if (this.options.host === 'local-ip') {
779798
this.options.host =
780799
internalIp.v4.sync() || internalIp.v6.sync() || '0.0.0.0';
781-
} else if (hostname === 'local-ipv4') {
800+
} else if (this.options.host === 'local-ipv4') {
782801
this.options.host = internalIp.v4.sync() || '0.0.0.0';
783-
} else if (hostname === 'local-ipv6') {
802+
} else if (this.options.host === 'local-ipv6') {
784803
this.options.host = internalIp.v6.sync() || '::';
785-
} else {
786-
this.options.host = hostname;
787804
}
788805

789-
return (
790-
Server.getFreePort(port || this.options.port)
791-
// eslint-disable-next-line no-shadow
792-
.then((port) => {
793-
this.options.port = port;
806+
return Server.getFreePort(this.options.port)
807+
.then((foundPort) => {
808+
this.options.port = foundPort;
794809

795-
return this.server.listen(port, this.options.host, (error) => {
810+
return this.server.listen(
811+
this.options.port,
812+
this.options.host,
813+
(error) => {
796814
if (this.options.hot || this.options.liveReload) {
797815
this.createSocketServer();
798816
}
@@ -812,14 +830,14 @@ class Server {
812830
if (typeof this.options.onListening === 'function') {
813831
this.options.onListening(this);
814832
}
815-
});
816-
})
817-
.catch((error) => {
818-
if (fn) {
819-
fn.call(this.server, error);
820833
}
821-
})
822-
);
834+
);
835+
})
836+
.catch((error) => {
837+
if (fn) {
838+
fn.call(this.server, error);
839+
}
840+
});
823841
}
824842

825843
close(cb) {
@@ -883,8 +901,6 @@ class Server {
883901
}
884902

885903
getStats(statsObj) {
886-
const getStatsOption = require('./utils/getStatsOption');
887-
888904
const stats = Server.DEFAULT_STATS;
889905

890906
const configArr = getCompilerConfigArray(this.compiler);

test/helpers/test-server.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,25 @@ function startFullSetup(config, options, done) {
2121
server = new Server(options, compiler);
2222

2323
let port;
24+
2425
if (Object.prototype.hasOwnProperty.call(options, 'port')) {
2526
port = options.port;
2627
} else {
2728
console.warn('Using the default port for testing is not recommended');
2829
port = 8080;
2930
}
3031

31-
server.listen(port, options.host, (err) => {
32-
if (err && done) {
33-
return done(err);
32+
let host;
33+
34+
if (Object.prototype.hasOwnProperty.call(options, 'host')) {
35+
host = options.host;
36+
} else {
37+
host = 'localhost';
38+
}
39+
40+
server.listen(port, host, (error) => {
41+
if (error && done) {
42+
return done(error);
3443
}
3544

3645
if (done) {

test/server/Server.test.js

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,41 +170,83 @@ describe('Server', () => {
170170
compiler = webpack(config);
171171
});
172172

173-
it('should listen and not throw error', (done) => {
173+
it('should work and using "port" and "host" from options', (done) => {
174174
const options = {
175175
host: 'localhost',
176176
port,
177177
};
178178

179-
server = new Server(compiler, options);
179+
server = new Server(options, compiler);
180+
181+
// eslint-disable-next-line no-undefined
182+
server.listen(undefined, undefined, () => {
183+
const info = server.server.address();
184+
185+
expect(info.address).toBe('127.0.0.1');
186+
expect(info.port).toBe(port);
187+
188+
server.close(done);
189+
});
190+
});
180191

181-
expect(() => server.listen(port, 'localhost')).not.toThrowError();
182-
server.close(done);
192+
it('should work and using "port" and "host" from arguments', (done) => {
193+
server = new Server({}, compiler);
194+
195+
server.listen(port, '127.0.0.1', () => {
196+
const info = server.server.address();
197+
198+
expect(info.address).toBe('127.0.0.1');
199+
expect(info.port).toBe(port);
200+
201+
server.close(done);
202+
});
183203
});
184204

185-
it('should work and listen', (done) => {
205+
it('should work and using the same "port" and "host" from options and arguments', (done) => {
186206
const options = {
187207
host: 'localhost',
208+
port,
188209
};
189210

190-
server = new Server(compiler, options);
211+
server = new Server(options, compiler);
191212

192-
expect(() => server.listen(9000)).not.toThrowError();
193-
server.close(done);
213+
server.listen(options.port, options.host, () => {
214+
const info = server.server.address();
215+
216+
expect(info.address).toBe('127.0.0.1');
217+
expect(info.port).toBe(port);
218+
219+
server.close(done);
220+
});
194221
});
195222

196-
it('should throw an error', (done) => {
223+
it('should log warning when the "port" and "host" options from options different from arguments', (done) => {
197224
const options = {
198-
host: '192.160.21.34',
199-
port,
225+
host: '127.0.0.2',
226+
port: '9999',
200227
};
201228

202229
server = new Server(compiler, options);
203230

204-
expect(() => server.listen(port, 'localhost')).toThrowError(
205-
'The host specified in options is different from the host passed as an argument.'
206-
);
207-
server.close(done);
231+
const loggerWarnSpy = jest.spyOn(server.logger, 'warn');
232+
233+
server.listen(port, '127.0.0.1', () => {
234+
const info = server.server.address();
235+
236+
expect(loggerWarnSpy).toHaveBeenNthCalledWith(
237+
1,
238+
'The "port" specified in options is different from the port passed as an argument. Will be used from arguments.'
239+
);
240+
expect(loggerWarnSpy).toHaveBeenNthCalledWith(
241+
2,
242+
'The "host" specified in options is different from the host passed as an argument. Will be used from arguments.'
243+
);
244+
expect(info.address).toBe('127.0.0.1');
245+
expect(info.port).toBe(port);
246+
247+
loggerWarnSpy.mockRestore();
248+
server.close(done);
249+
});
208250
});
209251
});
210252

test/server/host-option.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('host option', () => {
3535
it('server address', () => {
3636
const address = server.server.address();
3737

38-
expect(address.address).toBe('::');
38+
expect(address.address).toBe('127.0.0.1');
3939
expect(address.port).toBe(port);
4040
});
4141

test/server/port-option.test.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ describe('port', () => {
2525
watch: false,
2626
},
2727
port,
28-
host: 'localhost',
2928
},
3029
done
3130
);
@@ -58,7 +57,6 @@ describe('port', () => {
5857
},
5958
// eslint-disable-next-line no-undefined
6059
port: undefined,
61-
host: 'localhost',
6260
},
6361
done
6462
);
@@ -90,7 +88,6 @@ describe('port', () => {
9088
watch: false,
9189
},
9290
port: 'auto',
93-
host: 'localhost',
9491
},
9592
done
9693
);
@@ -122,7 +119,6 @@ describe('port', () => {
122119
watch: false,
123120
},
124121
port: '33333',
125-
host: 'localhost',
126122
},
127123
done
128124
);
@@ -153,7 +149,6 @@ describe('port', () => {
153149
watch: false,
154150
},
155151
port: '33333',
156-
host: 'localhost',
157152
},
158153
done
159154
);

test/server/stats-option.test.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ describe('stats option', () => {
2929
() =>
3030
new Promise((resolve) => {
3131
const compiler = webpack(Object.assign({}, config, { stats }));
32-
const server = createServer(compiler, {
33-
static: false,
34-
port,
35-
host: 'localhost',
36-
});
32+
const server = createServer(compiler, { static: false, port });
3733

3834
compiler.hooks.done.tap('webpack-dev-server', (s) => {
3935
expect(
@@ -57,11 +53,7 @@ describe('stats option', () => {
5753
stats: { warningsFilter: 'test' },
5854
})
5955
);
60-
const server = createServer(compiler, {
61-
static: false,
62-
port,
63-
host: 'localhost',
64-
});
56+
const server = createServer(compiler, { static: false, port });
6557

6658
compiler.hooks.done.tap('webpack-dev-server', (s) => {
6759
s.compilation.warnings = ['test', 'another warning'];

0 commit comments

Comments
 (0)