Skip to content

feat: Add ResolverFactory hooks for webpack compatibility #10964

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
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
41 changes: 39 additions & 2 deletions packages/rspack/src/ResolverFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as binding from "@rspack/binding";
import * as liteTapable from "@rspack/lite-tapable";
import { Resolver } from "./Resolver";
import { type Resolve, getRawResolve } from "./config";

Expand All @@ -10,6 +11,17 @@ type ResolveOptionsWithDependencyType = Resolve & {
export class ResolverFactory {
#binding: binding.JsResolverFactory;

hooks: {
resolveOptions: liteTapable.SyncWaterfallHook<
[ResolveOptionsWithDependencyType, { type: string }]
>;
resolver: liteTapable.HookMap<
liteTapable.SyncHook<
[Resolver, ResolveOptionsWithDependencyType, { type: string }]
>
>;
};

static __to_binding(
resolver_factory: ResolverFactory
): binding.JsResolverFactory {
Expand All @@ -18,20 +30,45 @@ export class ResolverFactory {

constructor(pnp: boolean) {
this.#binding = new binding.JsResolverFactory(pnp);
this.hooks = {
resolveOptions: new liteTapable.SyncWaterfallHook([
"resolveOptions",
"context"
]),
resolver: new liteTapable.HookMap(
() =>
new liteTapable.SyncHook(["resolver", "resolveOptions", "context"])
)
};
}

get(
type: string,
resolveOptions?: ResolveOptionsWithDependencyType
): Resolver {
// Prepare context for hooks
const context = { type };

// Apply resolveOptions hook to allow modification of resolve options
const resolveOptionsToUse = this.hooks.resolveOptions.call(
resolveOptions || {},
context
);

const { dependencyCategory, resolveToContext, ...resolve } =
resolveOptions || {};
resolveOptionsToUse;

const binding = this.#binding.get(type, {
...getRawResolve(resolve),
dependencyCategory,
resolveToContext
});
return new Resolver(binding);

const resolver = new Resolver(binding);

// Call resolver hook to allow plugins to access the created resolver
this.hooks.resolver.for(type).call(resolver, resolveOptionsToUse, context);

return resolver;
}
}
Loading