Skip to content

Commit

Permalink
Merge pull request #2 from web-infra-dev/fix/alias-require-resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder authored Sep 25, 2024
2 parents c40006c + ec8c1b8 commit 3757efa
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ var overlay =
: {
send: function send() {}
};
/* Rspack dev server runtime client */
var onSocketMessage = {
hot: function hot() {
if (parsedResourceQuery.hot === "false") {
Expand Down
10 changes: 10 additions & 0 deletions src/alias.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const Module = require("module");
const MODULE_MAP: Record<string, typeof Module._resolveFilename> = {};
const RESOLVER_MAP: Record<string, typeof require.resolve> = {};
export const addResolveAlias = (
name: string,
Expand All @@ -18,6 +20,13 @@ export const addResolveAlias = (
id,
options
])) as typeof require.resolve;
MODULE_MAP[modulePath] = Module._resolveFilename;
Module._resolveFilename = Module._resolveFilename = (request: string, mod: NodeModule, ...args: unknown[]) => {
if (mod.filename === modulePath && aliasMap[request]) {
return aliasMap[request];
}
return MODULE_MAP[modulePath](request, mod, ...args);
};
};

export const removeResolveAlias = (name: string) => {
Expand All @@ -30,6 +39,7 @@ export const removeResolveAlias = (name: string) => {
throw new Error("Failed to resolve webpack-dev-server");
}
if (RESOLVER_MAP[modulePath]) {
Module._resolveFilename = RESOLVER_MAP[modulePath]!;
m.require.resolve = RESOLVER_MAP[modulePath]!;
delete RESOLVER_MAP[modulePath];
}
Expand Down
6 changes: 6 additions & 0 deletions tests/e2e/__snapshots__/client.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ exports[`client option configure client entry should disable client entry: page

exports[`client option configure client entry should disable client entry: response status 1`] = `200`;

exports[`client option configure client entry should redirect client entry to rspack: console messages 1`] = `[]`;

exports[`client option configure client entry should redirect client entry to rspack: page errors 1`] = `[]`;

exports[`client option configure client entry should redirect client entry to rspack: response status 1`] = `200`;

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

exports[`client option default behaviour responds with a 200 status code for /ws path: page errors 1`] = `[]`;
Expand Down
60 changes: 60 additions & 0 deletions tests/e2e/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,66 @@ describe("client option", () => {
});
});

describe("configure client entry", () => {
let compiler;
let server;
let page;
let browser;
let pageErrors;
let consoleMessages;

beforeEach(async () => {
compiler = webpack({
...config,
devtool: false
});

server = new Server(
{
port
},
compiler
);

await server.start();

({ page, browser } = await runBrowser());

pageErrors = [];
consoleMessages = [];
});

afterEach(async () => {
await browser.close();
await server.stop();
});

it("should redirect client entry to rspack", async () => {
page
.on("console", message => {
consoleMessages.push(message);
})
.on("pageerror", error => {
pageErrors.push(error);
});

const response = await page.goto(`http://127.0.0.1:${port}/main.js`, {
waitUntil: "networkidle0"
});

expect(await response.text()).toContain("/* Rspack dev server runtime client */")

expect(response.status()).toMatchSnapshot("response status");

expect(consoleMessages.map(message => message.text())).toMatchSnapshot(
"console messages"
);

expect(pageErrors).toMatchSnapshot("page errors");
});
});


describe("webSocketTransport", () => {
const clientModes = [
{
Expand Down

0 comments on commit 3757efa

Please sign in to comment.