Skip to content

Commit 043e5c9

Browse files
author
Joel Denning
committed
fix(serve): set client port when using default port
1 parent ae78411 commit 043e5c9

File tree

4 files changed

+46
-21
lines changed

4 files changed

+46
-21
lines changed

packages/serve/__tests__/mergeOptions.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
'use strict';
22

33
import mergeOptions from '../src/mergeOptions';
4+
import { devServerClientLogging } from '../src/types';
45

56
describe('mergeOptions', () => {
67
it('merges CLI and devServer options correctly', () => {
78
const cliOptions = {
89
client: {
9-
logging: 'verbose',
10+
logging: devServerClientLogging.verbose,
1011
},
1112
hot: true,
1213
bonjour: true,
1314
};
1415
const devServerOptions = {
1516
client: {
1617
host: 'localhost',
17-
logging: 'none',
18+
logging: devServerClientLogging.none,
1819
},
1920
hot: false,
2021
liveReload: false,

packages/serve/__tests__/startDevServer.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('startDevServer', () => {
1414
DevServer.mockClear();
1515
});
1616

17-
it('should start dev server correctly for single compiler', () => {
17+
it('should start dev server correctly for single compiler', async () => {
1818
const config = {
1919
devServer: {
2020
port: 9000,
@@ -24,7 +24,7 @@ describe('startDevServer', () => {
2424
};
2525
const compiler = webpack(config);
2626

27-
const servers = startDevServer(compiler, {
27+
const servers = await startDevServer(compiler, {
2828
host: 'my.host',
2929
hot: true,
3030
progress: true,
@@ -43,13 +43,13 @@ describe('startDevServer', () => {
4343
expect(DevServer.mock.instances[0].listen.mock.calls[0]).toMatchSnapshot();
4444
});
4545

46-
it('should set default port and host if not provided', () => {
46+
it('should set default port and host if not provided', async () => {
4747
const config = {
4848
devServer: {},
4949
};
5050
const compiler = webpack(config);
5151

52-
const servers = startDevServer(compiler, {});
52+
const servers = await startDevServer(compiler, {});
5353

5454
expect(servers.length).toEqual(1);
5555
expect(servers).toEqual(DevServer.mock.instances);
@@ -64,7 +64,7 @@ describe('startDevServer', () => {
6464
expect(DevServer.mock.instances[0].listen.mock.calls[0]).toMatchSnapshot();
6565
});
6666

67-
it('should start dev server correctly for multi compiler with 1 devServer config', () => {
67+
it('should start dev server correctly for multi compiler with 1 devServer config', async () => {
6868
const config = [
6969
{
7070
devServer: {
@@ -77,7 +77,7 @@ describe('startDevServer', () => {
7777
];
7878
const compiler = webpack(config);
7979

80-
const servers = startDevServer(compiler, {
80+
const servers = await startDevServer(compiler, {
8181
host: 'my.host',
8282
hot: true,
8383
progress: true,
@@ -96,7 +96,7 @@ describe('startDevServer', () => {
9696
expect(DevServer.mock.instances[0].listen.mock.calls[0]).toMatchSnapshot();
9797
});
9898

99-
it('should start dev servers correctly for multi compiler with 2 devServer configs', () => {
99+
it('should start dev servers correctly for multi compiler with 2 devServer configs', async () => {
100100
const config = [
101101
{
102102
devServer: {
@@ -113,7 +113,7 @@ describe('startDevServer', () => {
113113
];
114114
const compiler = webpack(config);
115115

116-
const servers = startDevServer(compiler, {
116+
const servers = await startDevServer(compiler, {
117117
// this progress CLI flag should override progress: false above
118118
progress: true,
119119
});
@@ -137,8 +137,8 @@ describe('startDevServer', () => {
137137
expect(DevServer.mock.instances[1].listen.mock.calls[0]).toMatchSnapshot();
138138
});
139139

140-
it('should handle 2 multi compiler devServer configs with conflicting ports', () => {
141-
expect(() => {
140+
it('should handle 2 multi compiler devServer configs with conflicting ports', async () => {
141+
await expect(async () => {
142142
const config = [
143143
{
144144
devServer: {
@@ -153,8 +153,8 @@ describe('startDevServer', () => {
153153
];
154154
const compiler = webpack(config);
155155

156-
startDevServer(compiler, {});
157-
}).toThrow(
156+
await startDevServer(compiler, {});
157+
}).rejects.toThrow(
158158
'Unique ports must be specified for each devServer option in your webpack configuration. Alternatively, run only 1 devServer config using the --config-name flag to specify your desired config.',
159159
);
160160
});

packages/serve/src/startDevServer.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ const { logger } = utils;
1414
*
1515
* @returns {Object[]} array of resulting servers
1616
*/
17-
export default function startDevServer(compiler, cliOptions): object[] {
17+
export default async function startDevServer(compiler, cliOptions): Promise<object[]> {
1818
let isDevServer4 = false,
1919
devServerVersion,
20-
Server;
20+
Server,
21+
findPort;
2122
try {
2223
// eslint-disable-next-line node/no-extraneous-require
2324
devServerVersion = require('webpack-dev-server/package.json').version;
2425
// eslint-disable-next-line node/no-extraneous-require
2526
Server = require('webpack-dev-server/lib/Server');
27+
// eslint-disable-next-line node/no-extraneous-require
28+
findPort = require('webpack-dev-server/lib/utils/findPort');
2629
} catch (err) {
2730
logger.error(`You need to install 'webpack-dev-server' for running 'webpack serve'.\n${err}`);
2831
process.exit(2);
@@ -34,10 +37,14 @@ export default function startDevServer(compiler, cliOptions): object[] {
3437
const servers = [];
3538

3639
const usedPorts: number[] = [];
37-
devServerOptions.forEach((devServerOpts): void => {
40+
41+
for (const devServerOpts of devServerOptions) {
3842
const options = mergeOptions(cliOptions, devServerOpts);
39-
// devSever v4 handles the default host and port itself
40-
if (!isDevServer4) {
43+
if (isDevServer4) {
44+
options.port = await findPort(options.port);
45+
options.client = options.client || {};
46+
options.client.port = options.client.port || options.port;
47+
} else {
4148
options.host = options.host || 'localhost';
4249
options.port = options.port || 8080;
4350
}
@@ -61,7 +68,7 @@ export default function startDevServer(compiler, cliOptions): object[] {
6168
});
6269

6370
servers.push(server);
64-
});
71+
}
6572

6673
return servers;
6774
}

packages/serve/src/types.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export type devServerOptionsType = {
22
bonjour?: boolean;
3-
client?: object;
3+
client?: devServerClientOptions;
44
compress?: boolean;
55
dev?: object;
66
firewall?: boolean | string[];
@@ -28,3 +28,20 @@ export type devServerOptionsType = {
2828
transportMode?: object | string;
2929
useLocalIp?: boolean;
3030
};
31+
32+
type devServerClientOptions = {
33+
host?: string;
34+
path?: string;
35+
port?: string | number | null;
36+
logging?: devServerClientLogging;
37+
progress?: boolean;
38+
};
39+
40+
export enum devServerClientLogging {
41+
none = 'none',
42+
error = 'error',
43+
warn = 'warn',
44+
info = 'info',
45+
log = 'log',
46+
verbose = 'verbose',
47+
}

0 commit comments

Comments
 (0)