You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1.1 KiB
33 lines
1.1 KiB
2 years ago
|
/* global __webpack_dev_server_client__ */
|
||
|
|
||
|
const getSocketUrlParts = require('./utils/getSocketUrlParts.js');
|
||
|
const getUrlFromParts = require('./utils/getUrlFromParts');
|
||
|
const getWDSMetadata = require('./utils/getWDSMetadata');
|
||
|
|
||
|
/**
|
||
|
* 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') {
|
||
|
let SocketClient = __webpack_dev_server_client__;
|
||
|
if (typeof __webpack_dev_server_client__.default !== 'undefined') {
|
||
|
SocketClient = __webpack_dev_server_client__.default;
|
||
|
}
|
||
|
|
||
|
const wdsMeta = getWDSMetadata(SocketClient);
|
||
|
const urlParts = getSocketUrlParts(resourceQuery, wdsMeta);
|
||
|
|
||
|
const connection = new SocketClient(getUrlFromParts(urlParts, wdsMeta));
|
||
|
|
||
|
connection.onMessage(function onSocketMessage(data) {
|
||
|
const message = JSON.parse(data);
|
||
|
messageHandler(message);
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = { init: initWDSSocket };
|