Skip to content

Commit e602458

Browse files
committed
fix: support webpack-dev-server 6.x (drop SockJS instanceof checks)
WDS 6.0.0 removed the SockJSClient module, which broke WDSSocket.js's require() of it with a compile-time "Module not found" error. Detect the active client via duck-typing on `.sock`/`.client` instead of instanceof checks against imported client classes, which works unchanged across WDS 4/5/6 and needs no optional require. Also widen the webpack-dev-server peer range to include 6.x and extend the test harness/CI matrix to cover it.
1 parent 257c43a commit e602458

8 files changed

Lines changed: 334 additions & 33 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ jobs:
4242
wds-version:
4343
- '4'
4444
- '5'
45+
- '6'
4546
steps:
4647
- name: Checkout Repository
4748
uses: actions/checkout@v4
@@ -71,6 +72,7 @@ jobs:
7172
wds-version:
7273
- '4'
7374
- '5'
75+
- '6'
7476
steps:
7577
- name: Checkout Repository
7678
uses: actions/checkout@v4

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,11 @@ but you can set the [`overlay.sockIntegration`](docs/API.md#sockintegration) opt
320320

321321
The supported versions are as follows:
322322

323-
| Dependency | Version |
324-
| ------------------------ | ----------------- |
325-
| `webpack-dev-server` | `4.8.0`+ or `5.x` |
326-
| `webpack-hot-middleware` | `2.x` |
327-
| `webpack-plugin-serve` | `1.x` |
323+
| Dependency | Version |
324+
| ------------------------ | -------------------------- |
325+
| `webpack-dev-server` | `4.8.0`+ or `5.x` or `6.x` |
326+
| `webpack-hot-middleware` | `2.x` |
327+
| `webpack-plugin-serve` | `1.x` |
328328

329329
## API
330330

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
"webpack-cli": "^7.0.3",
107107
"webpack-dev-server": "^5.2.6",
108108
"webpack-dev-server-v4": "npm:webpack-dev-server@^4.8.0",
109+
"webpack-dev-server-v6": "npm:webpack-dev-server@^6.0.0",
109110
"webpack-hot-middleware": "^2.26.1",
110111
"webpack-plugin-serve": "^1.6.0",
111112
"yn": "^4.0.0"
@@ -116,7 +117,7 @@
116117
"sockjs-client": "^1.4.0",
117118
"type-fest": ">=0.17.0 <6.0.0",
118119
"webpack": "^5.0.0",
119-
"webpack-dev-server": "^4.8.0 || 5.x",
120+
"webpack-dev-server": "^4.8.0 || 5.x || 6.x",
120121
"webpack-hot-middleware": "2.x",
121122
"webpack-plugin-serve": "1.x"
122123
},

sockets/WDSSocket.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
* @returns {void}
55
*/
66
function initWDSSocket(messageHandler) {
7-
const { default: SockJSClient } = require('webpack-dev-server/client/clients/SockJSClient');
8-
const { default: WebSocketClient } = require('webpack-dev-server/client/clients/WebSocketClient');
97
const { client } = require('webpack-dev-server/client/socket');
108

119
/** @type {WebSocket} */
1210
let connection;
13-
if (client instanceof SockJSClient) {
11+
if (client.sock) {
12+
// SockJSClient exposes the underlying socket via `.sock`.
13+
// WDS 6.0.0 dropped SockJS support, so this branch is dead in WDS 6+.
1414
connection = client.sock;
15-
} else if (client instanceof WebSocketClient) {
15+
} else if (client.client) {
16+
// WebSocketClient exposes the underlying socket via `.client`.
1617
connection = client.client;
1718
} else {
1819
throw new Error('Failed to determine WDS client type');

test/helpers/sandbox/aliasWDSv6.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const moduleAlias = require('module-alias');
2+
3+
moduleAlias.addAliases({ 'webpack-dev-server': 'webpack-dev-server-v6' });

test/helpers/sandbox/configs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ module.exports = {
8686
alias: ${JSON.stringify(
8787
{
8888
...(WDS_VERSION === 4 && { 'webpack-dev-server': 'webpack-dev-server-v4' }),
89+
...(WDS_VERSION === 6 && { 'webpack-dev-server': 'webpack-dev-server-v6' }),
8990
},
9091
null,
9192
2

test/helpers/sandbox/spawn.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ function spawnWebpackServe(port, dirs, options = {}) {
133133

134134
const NODE_OPTIONS = [
135135
// This requires a script to alias `webpack-dev-server` -
136-
// both v4 and v5 are installed,
136+
// v4, v5 and v6 are all installed,
137137
// so we have to ensure that they resolve to the correct variant.
138138
WDS_VERSION === 4 && `--require "${require.resolve('./aliasWDSv4')}"`,
139+
WDS_VERSION === 6 && `--require "${require.resolve('./aliasWDSv6')}"`,
139140
]
140141
.filter(Boolean)
141142
.join(' ');

0 commit comments

Comments
 (0)