Skip to content

Commit dc642a8

Browse files
authored
feat: added getClientEntry and getClientHotEntry methods to get clients entries
1 parent 2b7d318 commit dc642a8

File tree

6 files changed

+93
-7
lines changed

6 files changed

+93
-7
lines changed

lib/Server.js

+23-7
Original file line numberDiff line numberDiff line change
@@ -793,15 +793,12 @@ class Server {
793793
webSocketURLStr = searchParams.toString();
794794
}
795795

796-
additionalEntries.push(
797-
`${require.resolve("../client/index.js")}?${webSocketURLStr}`,
798-
);
796+
additionalEntries.push(`${this.getClientEntry()}?${webSocketURLStr}`);
799797
}
800798

801-
if (this.options.hot === "only") {
802-
additionalEntries.push(require.resolve("webpack/hot/only-dev-server"));
803-
} else if (this.options.hot) {
804-
additionalEntries.push(require.resolve("webpack/hot/dev-server"));
799+
const clientHotEntry = this.getClientHotEntry();
800+
if (clientHotEntry) {
801+
additionalEntries.push(clientHotEntry);
805802
}
806803

807804
const webpack = compiler.webpack || require("webpack");
@@ -1676,6 +1673,25 @@ class Server {
16761673
return implementation;
16771674
}
16781675

1676+
/**
1677+
* @returns {string}
1678+
*/
1679+
// eslint-disable-next-line class-methods-use-this
1680+
getClientEntry() {
1681+
return require.resolve("../client/index.js");
1682+
}
1683+
1684+
/**
1685+
* @returns {string | void}
1686+
*/
1687+
getClientHotEntry() {
1688+
if (this.options.hot === "only") {
1689+
return require.resolve("webpack/hot/only-dev-server");
1690+
} else if (this.options.hot) {
1691+
return require.resolve("webpack/hot/dev-server");
1692+
}
1693+
}
1694+
16791695
/**
16801696
* @private
16811697
* @returns {void}

test/e2e/__snapshots__/client.test.js.snap.webpack5

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ exports[`client option default behaviour responds with a 200 status code for /ws
1212

1313
exports[`client option default behaviour responds with a 200 status code for /ws path: response status 1`] = `200`;
1414

15+
exports[`client option override client entry should disable client entry: response status 1`] = `200`;
16+
1517
exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: console messages 1`] = `[]`;
1618

1719
exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: page errors 1`] = `[]`;

test/e2e/client.test.js

+54
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,60 @@ describe("client option", () => {
193193
});
194194
});
195195

196+
describe("override client entry", () => {
197+
let compiler;
198+
let server;
199+
let page;
200+
let browser;
201+
202+
class OverrideServer extends Server {
203+
// eslint-disable-next-line class-methods-use-this
204+
getClientEntry() {
205+
return require.resolve(
206+
"../fixtures/custom-client/CustomClientEntry.js",
207+
);
208+
}
209+
// eslint-disable-next-line class-methods-use-this
210+
getClientHotEntry() {
211+
return require.resolve(
212+
"../fixtures/custom-client/CustomClientHotEntry.js",
213+
);
214+
}
215+
}
216+
217+
beforeEach(async () => {
218+
compiler = webpack(config);
219+
220+
server = new OverrideServer(
221+
{
222+
port,
223+
},
224+
compiler,
225+
);
226+
227+
await server.start();
228+
229+
({ page, browser } = await runBrowser());
230+
});
231+
232+
afterEach(async () => {
233+
await browser.close();
234+
await server.stop();
235+
});
236+
237+
it("should disable client entry", async () => {
238+
const response = await page.goto(`http://127.0.0.1:${port}/main.js`, {
239+
waitUntil: "networkidle0",
240+
});
241+
242+
expect(response.status()).toMatchSnapshot("response status");
243+
244+
const content = await response.text();
245+
expect(content).toContain("CustomClientEntry.js");
246+
expect(content).toContain("CustomClientHotEntry.js");
247+
});
248+
});
249+
196250
describe("webSocketTransport", () => {
197251
const clientModes = [
198252
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
console.log("custom client entry");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
console.log("custom client hot entry");

types/lib/Server.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,14 @@ declare class Server<
12301230
* @returns {T}
12311231
*/
12321232
private getServerTransport;
1233+
/**
1234+
* @returns {string}
1235+
*/
1236+
getClientEntry(): string;
1237+
/**
1238+
* @returns {string | void}
1239+
*/
1240+
getClientHotEntry(): string | void;
12331241
/**
12341242
* @private
12351243
* @returns {void}

0 commit comments

Comments
 (0)