-
Notifications
You must be signed in to change notification settings - Fork 194
/
WDSSocket.js
26 lines (21 loc) · 871 Bytes
/
WDSSocket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* global __webpack_dev_server_client__ */
const url = require('native-url');
const getSocketUrlParts = require('./utils/getSocketUrlParts');
/**
* Initializes a socket server for HMR for webpack-dev-server.
* @param {function(*): void} messageHandler A handler to consume Webpack compilation messages.
* @param {string} [resourceQuery] Webpack's `__resourceQuery` string.
* @returns {void}
*/
function initWDSSocket(messageHandler, resourceQuery) {
if (typeof __webpack_dev_server_client__ !== 'undefined') {
const SocketClient = __webpack_dev_server_client__;
const urlParts = getSocketUrlParts(resourceQuery);
const connection = new SocketClient(url.format(urlParts));
connection.onMessage(function onSocketMessage(data) {
const message = JSON.parse(data);
messageHandler(message);
});
}
}
module.exports = initWDSSocket;