Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: clean dev server code #7794

Merged
merged 4 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/rspack-dev-server/src/alias.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const RESOLVER_MAP: Record<string, typeof require.resolve> = {};
export const addResolveAlias = (
name: string,
aliasMap: Record<string, string>
) => {
if (RESOLVER_MAP[name]) {
throw new Error(`Should not add resolve alias to ${name} again.`);
}
const m = require.cache[require.resolve(name)];
if (!m) {
throw new Error("Failed to resolve webpack-dev-server.");
}
RESOLVER_MAP[name] = m.require.resolve;
m.require.resolve = ((id: string, options?: any) =>
aliasMap[id] ||
RESOLVER_MAP[name]!.apply(m.require, [
id,
options
])) as typeof require.resolve;
};

export const removeResolveAlias = (name: string) => {
if (!RESOLVER_MAP[name]) {
throw new Error(`Should add resolve alias to ${name} before removing.`);
}
const m = require.cache[require.resolve(name)];
if (!m) {
throw new Error("Failed to resolve webpack-dev-server");
}
if (RESOLVER_MAP[name]) {
m.require.resolve = RESOLVER_MAP[name]!;
delete RESOLVER_MAP[name];
}
};
Loading
Loading