Skip to content

Commit c7c1b3a

Browse files
refactor: allow to set client: false
1 parent 2b7723b commit c7c1b3a

File tree

3 files changed

+57
-42
lines changed

3 files changed

+57
-42
lines changed

lib/Server.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Server {
5353
this.options
5454
);
5555

56-
if (this.options.client.progress) {
56+
if (this.options.client && this.options.client.progress) {
5757
this.setupProgressPlugin();
5858
}
5959

@@ -623,15 +623,15 @@ class Server {
623623
this.sendMessage([connection], 'liveReload');
624624
}
625625

626-
if (this.options.client.progress) {
626+
if (this.options.client && this.options.client.progress) {
627627
this.sendMessage(
628628
[connection],
629629
'progress',
630630
this.options.client.progress
631631
);
632632
}
633633

634-
if (this.options.client.overlay) {
634+
if (this.options.client && this.options.client.overlay) {
635635
this.sendMessage([connection], 'overlay', this.options.client.overlay);
636636
}
637637

@@ -1144,7 +1144,10 @@ class Server {
11441144
}
11451145

11461146
// Also allow if `client.webSocketURL.hostname` provided
1147-
if (typeof this.options.client.webSocketURL !== 'undefined') {
1147+
if (
1148+
this.options.client &&
1149+
typeof this.options.client.webSocketURL !== 'undefined'
1150+
) {
11481151
return this.options.client.webSocketURL.hostname === hostname;
11491152
}
11501153

lib/utils/DevServerPlugin.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ class DevServerPlugin {
2424

2525
let clientTransport;
2626

27-
if (typeof this.options.client.transport !== 'undefined') {
28-
clientTransport = this.options.client.transport;
29-
} else if (isKnownWebSocketServerImplementation) {
30-
clientTransport = this.options.webSocketServer.type;
27+
if (this.options.client) {
28+
if (typeof this.options.client.transport !== 'undefined') {
29+
clientTransport = this.options.client.transport;
30+
} else if (isKnownWebSocketServerImplementation) {
31+
clientTransport = this.options.webSocketServer.type;
32+
}
33+
} else {
34+
clientTransport = 'ws';
3135
}
3236

3337
switch (typeof clientTransport) {
@@ -191,7 +195,7 @@ class DevServerPlugin {
191195
getHotEntry() {
192196
const { options } = this;
193197

194-
/** @type {(string[] | string | undefined)} */
198+
/** @type {(string[] | string)} */
195199
let hotEntry;
196200

197201
if (options.hot === 'only') {
@@ -238,18 +242,19 @@ class DevServerPlugin {
238242

239243
const additionalEntries = [];
240244

241-
const clientEntry = this.getClientEntry();
242-
243245
if (
246+
this.options.client &&
244247
this.isWebTarget(compiler.options) &&
245248
(Boolean(this.options.hot) || this.options.liveReload)
246249
) {
250+
const clientEntry = this.getClientEntry();
251+
247252
additionalEntries.push(clientEntry);
248253
}
249254

250-
const hotEntry = this.getHotEntry();
255+
if (this.options.hot) {
256+
const hotEntry = this.getHotEntry();
251257

252-
if (hotEntry && Boolean(this.options.hot)) {
253258
additionalEntries.push(hotEntry);
254259
}
255260

@@ -328,7 +333,7 @@ class DevServerPlugin {
328333
compiler.options.plugins = compiler.options.plugins || [];
329334

330335
if (
331-
hotEntry &&
336+
this.options.hot &&
332337
!compiler.options.plugins.find(
333338
(p) => p.constructor === webpack.HotModuleReplacementPlugin
334339
)

lib/utils/normalizeOptions.js

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,43 @@ function normalizeOptions(compiler, options, logger, cacheDir) {
3939
options.allowedHosts = [options.allowedHosts];
4040
}
4141

42-
if (!options.client) {
43-
options.client = {};
44-
}
42+
if (
43+
typeof options.client === 'undefined' ||
44+
(typeof options.client === 'object' && options.client !== null)
45+
) {
46+
if (!options.client) {
47+
options.client = {};
48+
}
4549

46-
if (typeof options.client.webSocketURL === 'undefined') {
47-
options.client.webSocketURL = {};
48-
} else if (typeof options.client.webSocketURL === 'string') {
49-
const parsedURL = new URL(options.client.webSocketURL);
50-
51-
options.client.webSocketURL = {
52-
protocol: parsedURL.protocol,
53-
hostname: parsedURL.hostname,
54-
port: parsedURL.port.length > 0 ? Number(parsedURL.port) : '',
55-
pathname: parsedURL.pathname,
56-
username: parsedURL.username,
57-
password: parsedURL.password,
58-
};
59-
} else if (typeof options.client.webSocketURL.port === 'string') {
60-
options.client.webSocketURL.port = Number(options.client.webSocketURL.port);
61-
}
50+
if (typeof options.client.webSocketURL === 'undefined') {
51+
options.client.webSocketURL = {};
52+
} else if (typeof options.client.webSocketURL === 'string') {
53+
const parsedURL = new URL(options.client.webSocketURL);
54+
55+
options.client.webSocketURL = {
56+
protocol: parsedURL.protocol,
57+
hostname: parsedURL.hostname,
58+
port: parsedURL.port.length > 0 ? Number(parsedURL.port) : '',
59+
pathname: parsedURL.pathname,
60+
username: parsedURL.username,
61+
password: parsedURL.password,
62+
};
63+
} else if (typeof options.client.webSocketURL.port === 'string') {
64+
options.client.webSocketURL.port = Number(
65+
options.client.webSocketURL.port
66+
);
67+
}
6268

63-
// Enable client overlay by default
64-
if (typeof options.client.overlay === 'undefined') {
65-
options.client.overlay = true;
66-
} else if (typeof options.client.overlay !== 'boolean') {
67-
options.client.overlay = {
68-
errors: true,
69-
warnings: true,
70-
...options.client.overlay,
71-
};
69+
// Enable client overlay by default
70+
if (typeof options.client.overlay === 'undefined') {
71+
options.client.overlay = true;
72+
} else if (typeof options.client.overlay !== 'boolean') {
73+
options.client.overlay = {
74+
errors: true,
75+
warnings: true,
76+
...options.client.overlay,
77+
};
78+
}
7279
}
7380

7481
if (typeof options.compress === 'undefined') {

0 commit comments

Comments
 (0)