WDSSocket.js 871 B

1234567891011121314151617181920212223242526
  1. /* global __webpack_dev_server_client__ */
  2. const url = require('native-url');
  3. const getSocketUrlParts = require('./utils/getSocketUrlParts');
  4. /**
  5. * Initializes a socket server for HMR for webpack-dev-server.
  6. * @param {function(*): void} messageHandler A handler to consume Webpack compilation messages.
  7. * @param {string} [resourceQuery] Webpack's `__resourceQuery` string.
  8. * @returns {void}
  9. */
  10. function initWDSSocket(messageHandler, resourceQuery) {
  11. if (typeof __webpack_dev_server_client__ !== 'undefined') {
  12. const SocketClient = __webpack_dev_server_client__;
  13. const urlParts = getSocketUrlParts(resourceQuery);
  14. const connection = new SocketClient(url.format(urlParts));
  15. connection.onMessage(function onSocketMessage(data) {
  16. const message = JSON.parse(data);
  17. messageHandler(message);
  18. });
  19. }
  20. }
  21. module.exports = initWDSSocket;