-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from inottn/fix/7
- Loading branch information
Showing
1 changed file
with
21 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,51 @@ | ||
const Module = require("module"); | ||
const Module = require("node:module"); | ||
const MODULE_MAP: Record<string, typeof Module._resolveFilename> = {}; | ||
const RESOLVER_MAP: Record<string, typeof require.resolve> = {}; | ||
|
||
export const addResolveAlias = ( | ||
name: string, | ||
aliasMap: Record<string, string> | ||
aliasMap: Record<string, string>, | ||
) => { | ||
const modulePath = require.resolve(name); | ||
if (RESOLVER_MAP[modulePath]) { | ||
if (modulePath in RESOLVER_MAP) { | ||
throw new Error(`Should not add resolve alias to ${name} again.`); | ||
} | ||
const m = require.cache[modulePath]; | ||
if (!m) { | ||
throw new Error("Failed to resolve webpack-dev-server."); | ||
} | ||
RESOLVER_MAP[modulePath] = m.require.resolve; | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
m.require.resolve = ((id: string, options?: any) => | ||
aliasMap[id] || | ||
RESOLVER_MAP[modulePath]!.apply(m.require, [ | ||
RESOLVER_MAP[modulePath].apply(m.require, [ | ||
id, | ||
options | ||
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); | ||
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) => { | ||
const modulePath = require.resolve(name); | ||
if (!RESOLVER_MAP[modulePath]) { | ||
if (!(modulePath in RESOLVER_MAP)) { | ||
return; | ||
} | ||
const m = require.cache[modulePath]; | ||
if (!m) { | ||
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]; | ||
} | ||
|
||
Module._resolveFilename = MODULE_MAP[modulePath]; | ||
m.require.resolve = RESOLVER_MAP[modulePath]; | ||
delete RESOLVER_MAP[modulePath]; | ||
}; |