Skip to content

Commit

Permalink
feat: added getClientEntry and getClientHotEntry methods to get c…
Browse files Browse the repository at this point in the history
…lients entries
  • Loading branch information
LingyuCoder authored Sep 26, 2024
1 parent 2b7d318 commit dc642a8
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 7 deletions.
30 changes: 23 additions & 7 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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}
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/__snapshots__/client.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -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`] = `[]`;
Expand Down
54 changes: 54 additions & 0 deletions test/e2e/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/custom-client/CustomClientEntry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

console.log("custom client entry");
3 changes: 3 additions & 0 deletions test/fixtures/custom-client/CustomClientHotEntry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

console.log("custom client hot entry");
8 changes: 8 additions & 0 deletions types/lib/Server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,14 @@ declare class Server<
* @returns {T}
*/
private getServerTransport;
/**
* @returns {string}
*/
getClientEntry(): string;
/**
* @returns {string | void}
*/
getClientHotEntry(): string | void;
/**
* @private
* @returns {void}
Expand Down

0 comments on commit dc642a8

Please sign in to comment.