Skip to content

Commit 3757efa

Browse files
authored
Merge pull request #2 from web-infra-dev/fix/alias-require-resolve
2 parents c40006c + ec8c1b8 commit 3757efa

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

client/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ var overlay =
216216
: {
217217
send: function send() {}
218218
};
219+
/* Rspack dev server runtime client */
219220
var onSocketMessage = {
220221
hot: function hot() {
221222
if (parsedResourceQuery.hot === "false") {

src/alias.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const Module = require("module");
2+
const MODULE_MAP: Record<string, typeof Module._resolveFilename> = {};
13
const RESOLVER_MAP: Record<string, typeof require.resolve> = {};
24
export const addResolveAlias = (
35
name: string,
@@ -18,6 +20,13 @@ export const addResolveAlias = (
1820
id,
1921
options
2022
])) as typeof require.resolve;
23+
MODULE_MAP[modulePath] = Module._resolveFilename;
24+
Module._resolveFilename = Module._resolveFilename = (request: string, mod: NodeModule, ...args: unknown[]) => {
25+
if (mod.filename === modulePath && aliasMap[request]) {
26+
return aliasMap[request];
27+
}
28+
return MODULE_MAP[modulePath](request, mod, ...args);
29+
};
2130
};
2231

2332
export const removeResolveAlias = (name: string) => {
@@ -30,6 +39,7 @@ export const removeResolveAlias = (name: string) => {
3039
throw new Error("Failed to resolve webpack-dev-server");
3140
}
3241
if (RESOLVER_MAP[modulePath]) {
42+
Module._resolveFilename = RESOLVER_MAP[modulePath]!;
3343
m.require.resolve = RESOLVER_MAP[modulePath]!;
3444
delete RESOLVER_MAP[modulePath];
3545
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ exports[`client option configure client entry should disable client entry: page
66

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

9+
exports[`client option configure client entry should redirect client entry to rspack: console messages 1`] = `[]`;
10+
11+
exports[`client option configure client entry should redirect client entry to rspack: page errors 1`] = `[]`;
12+
13+
exports[`client option configure client entry should redirect client entry to rspack: response status 1`] = `200`;
14+
915
exports[`client option default behaviour responds with a 200 status code for /ws path: console messages 1`] = `[]`;
1016

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

tests/e2e/client.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,66 @@ describe("client option", () => {
193193
});
194194
});
195195

196+
describe("configure client entry", () => {
197+
let compiler;
198+
let server;
199+
let page;
200+
let browser;
201+
let pageErrors;
202+
let consoleMessages;
203+
204+
beforeEach(async () => {
205+
compiler = webpack({
206+
...config,
207+
devtool: false
208+
});
209+
210+
server = new Server(
211+
{
212+
port
213+
},
214+
compiler
215+
);
216+
217+
await server.start();
218+
219+
({ page, browser } = await runBrowser());
220+
221+
pageErrors = [];
222+
consoleMessages = [];
223+
});
224+
225+
afterEach(async () => {
226+
await browser.close();
227+
await server.stop();
228+
});
229+
230+
it("should redirect client entry to rspack", async () => {
231+
page
232+
.on("console", message => {
233+
consoleMessages.push(message);
234+
})
235+
.on("pageerror", error => {
236+
pageErrors.push(error);
237+
});
238+
239+
const response = await page.goto(`http://127.0.0.1:${port}/main.js`, {
240+
waitUntil: "networkidle0"
241+
});
242+
243+
expect(await response.text()).toContain("/* Rspack dev server runtime client */")
244+
245+
expect(response.status()).toMatchSnapshot("response status");
246+
247+
expect(consoleMessages.map(message => message.text())).toMatchSnapshot(
248+
"console messages"
249+
);
250+
251+
expect(pageErrors).toMatchSnapshot("page errors");
252+
});
253+
});
254+
255+
196256
describe("webSocketTransport", () => {
197257
const clientModes = [
198258
{

0 commit comments

Comments
 (0)