From dc642a832d45c23c5c7a08fbf29995e0db7e0d95 Mon Sep 17 00:00:00 2001 From: harpsealjs Date: Thu, 26 Sep 2024 21:41:11 +0800 Subject: [PATCH] feat: added `getClientEntry` and `getClientHotEntry` methods to get clients entries --- lib/Server.js | 30 ++++++++--- .../client.test.js.snap.webpack5 | 2 + test/e2e/client.test.js | 54 +++++++++++++++++++ .../custom-client/CustomClientEntry.js | 3 ++ .../custom-client/CustomClientHotEntry.js | 3 ++ types/lib/Server.d.ts | 8 +++ 6 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/custom-client/CustomClientEntry.js create mode 100644 test/fixtures/custom-client/CustomClientHotEntry.js diff --git a/lib/Server.js b/lib/Server.js index 7958299bbf..22b0a5ebd2 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -793,15 +793,12 @@ class Server { webSocketURLStr = searchParams.toString(); } - additionalEntries.push( - `${require.resolve("../client/index.js")}?${webSocketURLStr}`, - ); + additionalEntries.push(`${this.getClientEntry()}?${webSocketURLStr}`); } - if (this.options.hot === "only") { - additionalEntries.push(require.resolve("webpack/hot/only-dev-server")); - } else if (this.options.hot) { - additionalEntries.push(require.resolve("webpack/hot/dev-server")); + const clientHotEntry = this.getClientHotEntry(); + if (clientHotEntry) { + additionalEntries.push(clientHotEntry); } const webpack = compiler.webpack || require("webpack"); @@ -1676,6 +1673,25 @@ class Server { return implementation; } + /** + * @returns {string} + */ + // eslint-disable-next-line class-methods-use-this + getClientEntry() { + return require.resolve("../client/index.js"); + } + + /** + * @returns {string | void} + */ + getClientHotEntry() { + if (this.options.hot === "only") { + return require.resolve("webpack/hot/only-dev-server"); + } else if (this.options.hot) { + return require.resolve("webpack/hot/dev-server"); + } + } + /** * @private * @returns {void} diff --git a/test/e2e/__snapshots__/client.test.js.snap.webpack5 b/test/e2e/__snapshots__/client.test.js.snap.webpack5 index fb39002b72..9ce0c7bdb0 100644 --- a/test/e2e/__snapshots__/client.test.js.snap.webpack5 +++ b/test/e2e/__snapshots__/client.test.js.snap.webpack5 @@ -12,6 +12,8 @@ exports[`client option default behaviour responds with a 200 status code for /ws exports[`client option default behaviour responds with a 200 status code for /ws path: response status 1`] = `200`; +exports[`client option override client entry should disable client entry: response status 1`] = `200`; + exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: console messages 1`] = `[]`; exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: page errors 1`] = `[]`; diff --git a/test/e2e/client.test.js b/test/e2e/client.test.js index e74d9cb9c2..cd5062aa99 100644 --- a/test/e2e/client.test.js +++ b/test/e2e/client.test.js @@ -193,6 +193,60 @@ describe("client option", () => { }); }); + describe("override client entry", () => { + let compiler; + let server; + let page; + let browser; + + class OverrideServer extends Server { + // eslint-disable-next-line class-methods-use-this + getClientEntry() { + return require.resolve( + "../fixtures/custom-client/CustomClientEntry.js", + ); + } + // eslint-disable-next-line class-methods-use-this + getClientHotEntry() { + return require.resolve( + "../fixtures/custom-client/CustomClientHotEntry.js", + ); + } + } + + beforeEach(async () => { + compiler = webpack(config); + + server = new OverrideServer( + { + port, + }, + compiler, + ); + + await server.start(); + + ({ page, browser } = await runBrowser()); + }); + + afterEach(async () => { + await browser.close(); + await server.stop(); + }); + + it("should disable client entry", async () => { + const response = await page.goto(`http://127.0.0.1:${port}/main.js`, { + waitUntil: "networkidle0", + }); + + expect(response.status()).toMatchSnapshot("response status"); + + const content = await response.text(); + expect(content).toContain("CustomClientEntry.js"); + expect(content).toContain("CustomClientHotEntry.js"); + }); + }); + describe("webSocketTransport", () => { const clientModes = [ { diff --git a/test/fixtures/custom-client/CustomClientEntry.js b/test/fixtures/custom-client/CustomClientEntry.js new file mode 100644 index 0000000000..ee237e53f6 --- /dev/null +++ b/test/fixtures/custom-client/CustomClientEntry.js @@ -0,0 +1,3 @@ +"use strict"; + +console.log("custom client entry"); diff --git a/test/fixtures/custom-client/CustomClientHotEntry.js b/test/fixtures/custom-client/CustomClientHotEntry.js new file mode 100644 index 0000000000..6a574ef5c5 --- /dev/null +++ b/test/fixtures/custom-client/CustomClientHotEntry.js @@ -0,0 +1,3 @@ +"use strict"; + +console.log("custom client hot entry"); diff --git a/types/lib/Server.d.ts b/types/lib/Server.d.ts index 19e8e342fb..27bc0f33a2 100644 --- a/types/lib/Server.d.ts +++ b/types/lib/Server.d.ts @@ -1230,6 +1230,14 @@ declare class Server< * @returns {T} */ private getServerTransport; + /** + * @returns {string} + */ + getClientEntry(): string; + /** + * @returns {string | void} + */ + getClientHotEntry(): string | void; /** * @private * @returns {void}