Skip to content

Commit 1c0cce5

Browse files
refactor: options
1 parent 7489e83 commit 1c0cce5

7 files changed

+126
-107
lines changed

lib/options.json

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -140,49 +140,45 @@
140140
},
141141
"additionalProperties": false
142142
},
143-
"TransportModeEnum": {
143+
"WebSocketServerEnum": {
144144
"enum": ["sockjs", "ws"]
145145
},
146-
"TransportModeByTypeObject": {
146+
"WebSocketServerString": {
147+
"type": "string",
148+
"minLength": 1
149+
},
150+
"WebSocketServerFunction": {
151+
"instanceof": "Function"
152+
},
153+
"WebSocketServerObject": {
147154
"type": "object",
148155
"properties": {
149156
"type": {
150-
"type": "string"
151-
},
152-
"options": {
153-
"type": "object",
154-
"additionalProperties": true
155-
}
156-
},
157-
"additionalProperties": false
158-
},
159-
"TransportModeObject": {
160-
"properties": {
161-
"client": {
162-
"anyOf": [
163-
{
164-
"type": "string"
165-
},
166-
{
167-
"$ref": "#/definitions/TransportModeByTypeObject"
168-
}
169-
]
170-
},
171-
"server": {
172157
"anyOf": [
173158
{
174-
"type": "string"
159+
"$ref": "#/definitions/WebSocketServerEnum"
175160
},
176161
{
177-
"instanceof": "Function"
162+
"$ref": "#/definitions/WebSocketServerString"
178163
},
179164
{
180-
"$ref": "#/definitions/TransportModeByTypeObject"
165+
"$ref": "#/definitions/WebSocketServerFunction"
181166
}
182167
]
168+
},
169+
"options": {
170+
"type": "object",
171+
"additionalProperties": true
183172
}
184173
},
185174
"additionalProperties": false
175+
},
176+
"ClientTransportEnum": {
177+
"enum": ["sockjs", "ws"]
178+
},
179+
"ClientTransportString": {
180+
"type": "string",
181+
"minLength": 1
186182
}
187183
},
188184
"properties": {
@@ -200,6 +196,17 @@
200196
"client": {
201197
"type": "object",
202198
"properties": {
199+
"transport": {
200+
"anyOf": [
201+
{
202+
"$ref": "#/definitions/ClientTransportEnum"
203+
},
204+
{
205+
"$ref": "#/definitions/ClientTransportString"
206+
}
207+
],
208+
"description": "Allows to set custom transport to communicate with server."
209+
},
203210
"host": {
204211
"type": "string",
205212
"description": "Tells clients connected to devServer to use the provided host."
@@ -218,6 +225,10 @@
218225
],
219226
"description": "Tells clients connected to devServer to use the provided path to connect."
220227
},
228+
"path": {
229+
"type": "string",
230+
"description": "Tells clients connected to devServer to use the provided port."
231+
},
221232
"logging": {
222233
"enum": ["none", "error", "warn", "info", "log", "verbose"],
223234
"decription": "Log level in the browser."
@@ -273,6 +284,23 @@
273284
"description": "Specifies client properties. https://webpack.js.org/configuration/dev-server/#devserverclient",
274285
"additionalProperties": false
275286
},
287+
"webSocketServer": {
288+
"anyOf": [
289+
{
290+
"$ref": "#/definitions/WebSocketServerEnum"
291+
},
292+
{
293+
"$ref": "#/definitions/WebSocketServerString"
294+
},
295+
{
296+
"$ref": "#/definitions/WebSocketServerFunction"
297+
},
298+
{
299+
"$ref": "#/definitions/WebSocketServerObject"
300+
}
301+
],
302+
"description": "Allows to set web socket server and options."
303+
},
276304
"compress": {
277305
"type": "boolean",
278306
"description": "Enable gzip compression for everything served. https://webpack.js.org/configuration/dev-server/#devservercompress"
@@ -519,17 +547,6 @@
519547
],
520548
"description": "It is possible to configure advanced options for serving static files from directory. See the Express documentation for the possible options. https://webpack.js.org/configuration/dev-server/#devserverstatic"
521549
},
522-
"transportMode": {
523-
"anyOf": [
524-
{
525-
"$ref": "#/definitions/TransportModeEnum"
526-
},
527-
{
528-
"$ref": "#/definitions/TransportModeObject"
529-
}
530-
],
531-
"description": "This option allows us either to choose the current devServer transport mode for client/server individually or to provide custom client/server implementation. https://webpack.js.org/configuration/dev-server/#devservertransportmode"
532-
},
533550
"watchFiles": {
534551
"anyOf": [
535552
{

lib/servers/SockJSServer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ module.exports = class SockJSServer extends BaseServer {
5858
};
5959

6060
this.socket.installHandlers(this.server.server, {
61-
...this.server.options.transportMode.server.options,
62-
prefix: getPrefix(this.server.options.transportMode.server.options),
61+
...this.server.options.webSocketServer.options,
62+
prefix: getPrefix(this.server.options.webSocketServer.options),
6363
});
6464
}
6565

lib/servers/WebsocketServer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = class WebsocketServer extends BaseServer {
1111
super(server);
1212

1313
this.wsServer = new ws.Server({
14-
...this.server.options.transportMode.server.options,
14+
...this.server.options.webSocketServer.options,
1515
noServer: true,
1616
});
1717

lib/utils/DevServerPlugin.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,38 @@ class DevServerPlugin {
3030

3131
/** @type {string} */
3232
const domain = createDomain(options);
33+
34+
// SockJS is not supported server mode, so `host` and `port` can't specified, let's ignore them
35+
// TODO show warning about this
36+
const isSockJSType = options.webSocketServer.type === 'sockjs';
37+
3338
/** @type {string} */
34-
const host =
35-
options.client && options.client.host
36-
? `&host=${options.client.host}`
37-
: '';
39+
let host = '';
40+
41+
if (options.webSocketServer.options.host && !isSockJSType) {
42+
host = `&host=${options.webSocketServer.options.host}`;
43+
} else if (options.client && options.client.host) {
44+
host = `&host=${options.client.host}`;
45+
}
46+
3847
/** @type {string} */
39-
let path = '';
48+
let port = '';
4049

41-
// only add the path if it is not default
42-
if (
43-
options.transportMode &&
44-
options.transportMode.client &&
45-
options.transportMode.client.options &&
46-
options.transportMode.client.options.path
47-
) {
48-
path = `&path=${options.transportMode.client.options.path}`;
50+
if (options.webSocketServer.options.port && !isSockJSType) {
51+
port = `&port=${options.webSocketServer.options.port}`;
52+
} else if (options.client && options.client.port) {
53+
port = `&port=${options.client.port}`;
4954
}
5055

5156
/** @type {string} */
52-
const port =
53-
(options.client && options.client.port) || options.port
54-
? `&port=${options.client.port || options.port}`
55-
: '';
57+
let path = '';
58+
59+
// Only add the path if it is not default
60+
if (options.client && options.client.path && options.client.path) {
61+
path = `&path=${options.client.path}`;
62+
} else if (options.webSocketServer.options.path) {
63+
path = `&path=${options.webSocketServer.options.path}`;
64+
}
5665

5766
/** @type {string} */
5867
const logging =
@@ -62,7 +71,7 @@ class DevServerPlugin {
6271

6372
/** @type {string} */
6473
const clientEntry = `${require.resolve(
65-
'../../client/'
74+
'../../client/index.js'
6675
)}?${domain}${host}${path}${port}${logging}`;
6776

6877
/** @type {(string[] | string)} */

lib/utils/getSocketClientPath.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ function getSocketClientPath(options) {
44
let ClientImplementation;
55
let clientImplementationFound = true;
66

7-
switch (typeof options.transportMode.client.type) {
7+
const clientTransport =
8+
options.client.transport || options.webSocketServer.type;
9+
10+
switch (typeof clientTransport) {
811
case 'string':
912
// could be 'sockjs', 'ws', or a path that should be required
10-
if (options.transportMode.client.type === 'sockjs') {
13+
if (clientTransport === 'sockjs') {
1114
ClientImplementation = require('../../client/clients/SockJSClient');
12-
} else if (options.transportMode.client.type === 'ws') {
15+
} else if (clientTransport === 'ws') {
1316
ClientImplementation = require('../../client/clients/WebsocketClient');
1417
} else {
1518
try {
1619
// eslint-disable-next-line import/no-dynamic-require
17-
ClientImplementation = require(options.transportMode.client.type);
20+
ClientImplementation = require(clientTransport);
1821
} catch (e) {
1922
clientImplementationFound = false;
2023
}
@@ -26,8 +29,8 @@ function getSocketClientPath(options) {
2629

2730
if (!clientImplementationFound) {
2831
throw new Error(
29-
"transportMode.client must be a string denoting a default implementation (e.g. 'sockjs', 'ws') or a full path to " +
30-
'a JS file which exports a class extending BaseClient (webpack-dev-server/client-src/clients/BaseClient) ' +
32+
"client.transport must be a string denoting a default implementation (e.g. 'sockjs', 'ws') or a full path to " +
33+
'a JS file which exports a class extending BaseClient (webpack-dev-server/client-src/clients/BaseClient.js) ' +
3134
'via require.resolve(...)'
3235
);
3336
}

lib/utils/getSocketServerImplementation.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,34 @@ function getSocketServerImplementation(options) {
44
let ServerImplementation;
55
let ServerImplementationFound = true;
66

7-
switch (typeof options.transportMode.server.type) {
7+
switch (typeof options.webSocketServer.type) {
88
case 'string':
99
// could be 'sockjs', in the future 'ws', or a path that should be required
10-
if (options.transportMode.server.type === 'sockjs') {
10+
if (options.webSocketServer.type === 'sockjs') {
1111
ServerImplementation = require('../servers/SockJSServer');
12-
} else if (options.transportMode.server.type === 'ws') {
12+
} else if (options.webSocketServer.type === 'ws') {
1313
ServerImplementation = require('../servers/WebsocketServer');
1414
} else {
1515
try {
1616
// eslint-disable-next-line import/no-dynamic-require
17-
ServerImplementation = require(options.transportMode.server.type);
17+
ServerImplementation = require(options.webSocketServer.type);
1818
} catch (e) {
1919
ServerImplementationFound = false;
2020
}
2121
}
2222
break;
2323
case 'function':
24-
// potentially do more checks here to confirm that the user implemented this properlly
25-
// since errors could be difficult to understand
26-
ServerImplementation = options.transportMode.server.type;
24+
// Potentially do more checks here to confirm that the user implemented this properly since errors could be difficult to understand
25+
ServerImplementation = options.webSocketServer.type;
2726
break;
2827
default:
2928
ServerImplementationFound = false;
3029
}
3130

3231
if (!ServerImplementationFound) {
3332
throw new Error(
34-
"transportMode.server must be a string denoting a default implementation (e.g. 'ws', 'sockjs'), a full path to " +
35-
'a JS file which exports a class extending BaseServer (webpack-dev-server/lib/servers/BaseServer) ' +
33+
"webSocketServer (webSocketServer.type) must be a string denoting a default implementation (e.g. 'ws', 'sockjs'), a full path to " +
34+
'a JS file which exports a class extending BaseServer (webpack-dev-server/lib/servers/BaseServer.js) ' +
3635
'via require.resolve(...), or the class itself which extends BaseServer'
3736
);
3837
}

lib/utils/normalizeOptions.js

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -73,40 +73,31 @@ function normalizeOptions(compiler, options) {
7373
options.liveReload =
7474
typeof options.liveReload !== 'undefined' ? options.liveReload : true;
7575

76-
const normalizeTransportModeByType = (type, key) => {
77-
const defaultType = 'ws';
78-
const defaultOptions = { path: '/ws' };
79-
80-
// The `transportMode` option is `undefined`
81-
if (typeof type === 'undefined') {
82-
return { type: defaultType, options: defaultOptions };
83-
}
84-
// `transportMode: 'ws'`
85-
else if (typeof type === 'string') {
86-
return { type: type || defaultType, options: defaultOptions };
87-
}
88-
// `transportMode: {}`
89-
else if (typeof type[key] === 'undefined') {
90-
return { type: defaultType, options: defaultOptions };
91-
}
92-
// `transportMode: { server: 'ws' }`
93-
else if (typeof type[key] === 'string') {
94-
return { type: type[key], options: defaultOptions };
95-
}
96-
97-
// `transportMode: { server: { type: 'ws', options: { path: '/ws' } } }`
98-
return {
99-
type: type[key].type || defaultType,
100-
options: { ...defaultOptions, ...type[key].options },
101-
};
102-
};
76+
const defaultWebSocketServerType = 'ws';
77+
const defaultWebSocketServerOptions = { path: '/ws' };
10378

104-
options.transportMode = {
105-
server: normalizeTransportModeByType(options.transportMode, 'server'),
106-
client: normalizeTransportModeByType(options.transportMode, 'client'),
107-
};
108-
109-
// console.log(options.transportMode);
79+
if (typeof options.webSocketServer === 'undefined') {
80+
options.webSocketServer = {
81+
type: defaultWebSocketServerType,
82+
options: defaultWebSocketServerOptions,
83+
};
84+
} else if (
85+
typeof options.webSocketServer === 'string' ||
86+
typeof options.webSocketServer === 'function'
87+
) {
88+
options.webSocketServer = {
89+
type: options.webSocketServer,
90+
options: defaultWebSocketServerOptions,
91+
};
92+
} else {
93+
options.webSocketServer = {
94+
type: options.webSocketServer.type || defaultWebSocketServerType,
95+
options: {
96+
...defaultWebSocketServerOptions,
97+
...options.webSocketServer.options,
98+
},
99+
};
100+
}
110101

111102
if (!options.client) {
112103
options.client = {};

0 commit comments

Comments
 (0)