Skip to content

Expose Socket Client for external integrations #4261

@pmmmwh

Description

@pmmmwh

Feature Proposal

Allow external integrations to tap into the socket client of WDS in order to consume compilation events, such as build logs, errors, progress, etc.

Right now, the client is done as a module level singleton:

let client = null;

Perhaps we can assign it to some special global variable on window, or export this variable, so external parties can just consume the client.

Feature Use Case

This is mainly for custom error overlays that include wider coverage outside of what WDS provides (react-error-overlay, overlay from @pmmmwh/react-refresh-webpack-plugin), but it would also enable client-only tooling to sniff on HMR lifecycle, build status and more.

It is not impossible to achieve this functionality right now since the __webpack_dev_server_client__ global is defined, and we can use that as a starting point, but we would have to do two things that is not preferrable:

  1. Duplicate socket URL logic - which tightly couples the code to 1 version of WDS (and we have to keep up with all the special cases handled here)
  2. Duplicate socket connection - since we have to create the client from scratch, the connection would happen multiple times, which could be confusing to users ([webpack-dev-server]: Setting Websocket Connection invoked twice #4224, SockJS connection is duplicated with error overlay enabled pmmmwh/react-refresh-webpack-plugin#279, Migrate plugin for Webpack dev server v4 pmmmwh/react-refresh-webpack-plugin#481)

Inspirations

webpack-hot-middleware

https://github.com/webpack-contrib/webpack-hot-middleware/blob/e140693fa3f648c078a47a678d66b53579f28ee2/client.js#L148-L159

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions