Created
July 6, 2026 22:10
-
-
Save rtm-ctrlz/8409eb012cf5709537263ee7401293c9 to your computer and use it in GitHub Desktop.
react-refresh-webpack-plugin WDS-6 fix candidates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Initializes a socket server for HMR for webpack-dev-server. | |
| * @param {function(*): void} messageHandler A handler to consume Webpack compilation messages. | |
| * @returns {void} | |
| */ | |
| function initWDSSocket(messageHandler) { | |
| let SockJSClient; | |
| try { | |
| // that still produces compilation warning | |
| ({ default: SockJSClient } = require('webpack-dev-server/client/clients/SockJSClient')); | |
| } catch (e) { | |
| SockJSClient = undefined; | |
| } | |
| const { default: WebSocketClient } = require('webpack-dev-server/client/clients/WebSocketClient'); | |
| const { client } = require('webpack-dev-server/client/socket'); | |
| /** @type {WebSocket} */ | |
| let connection; | |
| if (SockJSClient && client instanceof SockJSClient) { | |
| connection = client.sock; | |
| } else if (client instanceof WebSocketClient) { | |
| connection = client.client; | |
| } else { | |
| throw new Error('Failed to determine WDS client type'); | |
| } | |
| connection.addEventListener('message', function onSocketMessage(message) { | |
| messageHandler(JSON.parse(message.data)); | |
| }); | |
| } | |
| module.exports = { init: initWDSSocket }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Initializes a socket server for HMR for webpack-dev-server. | |
| * @param {function(*): void} messageHandler A handler to consume Webpack compilation messages. | |
| * @returns {void} | |
| */ | |
| function initWDSSocket(messageHandler) { | |
| const { client } = require('webpack-dev-server/client/socket'); | |
| /** @type {WebSocket} */ | |
| let connection; | |
| if (client && client.sock) { | |
| // SockJSClient exposes `.sock` (WDS < 6) | |
| connection = client.sock; | |
| } else if (client && client.client) { | |
| // WebSocketClient exposes `.client` | |
| connection = client.client; | |
| } else { | |
| throw new Error('Failed to determine WDS client type'); | |
| } | |
| connection.addEventListener('message', function onSocketMessage(message) { | |
| messageHandler(JSON.parse(message.data)); | |
| }); | |
| } | |
| module.exports = { init: initWDSSocket }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment