Skip to content

Commit e3ff091

Browse files
authored
Merge pull request #9 from inottn/fix/7
2 parents d0be767 + 9706888 commit e3ff091

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/alias.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,51 @@
1-
const Module = require("module");
1+
const Module = require("node:module");
22
const MODULE_MAP: Record<string, typeof Module._resolveFilename> = {};
33
const RESOLVER_MAP: Record<string, typeof require.resolve> = {};
4+
45
export const addResolveAlias = (
56
name: string,
6-
aliasMap: Record<string, string>
7+
aliasMap: Record<string, string>,
78
) => {
89
const modulePath = require.resolve(name);
9-
if (RESOLVER_MAP[modulePath]) {
10+
if (modulePath in RESOLVER_MAP) {
1011
throw new Error(`Should not add resolve alias to ${name} again.`);
1112
}
1213
const m = require.cache[modulePath];
1314
if (!m) {
1415
throw new Error("Failed to resolve webpack-dev-server.");
1516
}
1617
RESOLVER_MAP[modulePath] = m.require.resolve;
18+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
1719
m.require.resolve = ((id: string, options?: any) =>
1820
aliasMap[id] ||
19-
RESOLVER_MAP[modulePath]!.apply(m.require, [
21+
RESOLVER_MAP[modulePath].apply(m.require, [
2022
id,
21-
options
23+
options,
2224
])) as typeof require.resolve;
2325
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);
26+
Module._resolveFilename = (
27+
request: string,
28+
mod: NodeModule,
29+
...args: unknown[]
30+
) => {
31+
if (mod.filename === modulePath && aliasMap[request]) {
32+
return aliasMap[request];
33+
}
34+
return MODULE_MAP[modulePath](request, mod, ...args);
2935
};
3036
};
3137

3238
export const removeResolveAlias = (name: string) => {
3339
const modulePath = require.resolve(name);
34-
if (!RESOLVER_MAP[modulePath]) {
40+
if (!(modulePath in RESOLVER_MAP)) {
3541
return;
3642
}
3743
const m = require.cache[modulePath];
3844
if (!m) {
3945
throw new Error("Failed to resolve webpack-dev-server");
4046
}
41-
if (RESOLVER_MAP[modulePath]) {
42-
Module._resolveFilename = RESOLVER_MAP[modulePath]!;
43-
m.require.resolve = RESOLVER_MAP[modulePath]!;
44-
delete RESOLVER_MAP[modulePath];
45-
}
47+
48+
Module._resolveFilename = MODULE_MAP[modulePath];
49+
m.require.resolve = RESOLVER_MAP[modulePath];
50+
delete RESOLVER_MAP[modulePath];
4651
};

0 commit comments

Comments
 (0)