Skip to content

Commit 5ccfa4e

Browse files
refactor: allow to set client: false
1 parent fb6920c commit 5ccfa4e

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
@@ -45,7 +45,7 @@ class Server {
4545
initialize() {
4646
this.applyDevServerPlugin();
4747

48-
if (this.options.client.progress) {
48+
if (this.options.client && this.options.client.progress) {
4949
this.setupProgressPlugin();
5050
}
5151

@@ -715,15 +715,15 @@ class Server {
715715
this.sendMessage([connection], 'liveReload');
716716
}
717717

718-
if (this.options.client.progress) {
718+
if (this.options.client && this.options.client.progress) {
719719
this.sendMessage(
720720
[connection],
721721
'progress',
722722
this.options.client.progress
723723
);
724724
}
725725

726-
if (this.options.client.overlay) {
726+
if (this.options.client && this.options.client.overlay) {
727727
this.sendMessage([connection], 'overlay', this.options.client.overlay);
728728
}
729729

@@ -1224,7 +1224,10 @@ class Server {
12241224
}
12251225

12261226
// Also allow if `client.webSocketURL.hostname` provided
1227-
if (typeof this.options.client.webSocketURL !== 'undefined') {
1227+
if (
1228+
this.options.client &&
1229+
typeof this.options.client.webSocketURL !== 'undefined'
1230+
) {
12281231
return this.options.client.webSocketURL.hostname === hostname;
12291232
}
12301233

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') {
@@ -239,18 +243,19 @@ class DevServerPlugin {
239243

240244
const additionalEntries = [];
241245

242-
const clientEntry = this.getClientEntry();
243-
244246
if (
247+
this.options.client &&
245248
this.isWebTarget(compiler.options) &&
246249
(Boolean(this.options.hot) || this.options.liveReload)
247250
) {
251+
const clientEntry = this.getClientEntry();
252+
248253
additionalEntries.push(clientEntry);
249254
}
250255

251-
const hotEntry = this.getHotEntry();
256+
if (this.options.hot) {
257+
const hotEntry = this.getHotEntry();
252258

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

@@ -329,7 +334,7 @@ class DevServerPlugin {
329334
compiler.options.plugins = compiler.options.plugins || [];
330335

331336
if (
332-
hotEntry &&
337+
this.options.hot &&
333338
!compiler.options.plugins.find(
334339
(p) => p.constructor === webpack.HotModuleReplacementPlugin
335340
)

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)