Skip to content

Commit

Permalink
feat: improve Resolver withOptions function
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Jul 9, 2024
1 parent 1921919 commit 38dee5b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
11 changes: 8 additions & 3 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3761,6 +3761,9 @@ export function getRawLibrary(library: LibraryOptions): RawLibraryOptions;
// @public (undocumented)
export const getRawOptions: (options: RspackOptionsNormalized, compiler: Compiler) => RawOptions;

// @public (undocumented)
export function getRawResolve(resolve: Resolve): RawOptions["resolve"];

// @public (undocumented)
export type GlobalObject = z.infer<typeof globalObject>;

Expand Down Expand Up @@ -8206,8 +8209,9 @@ const resolveOptions: z.ZodType<ResolveOptions>;
type ResolveOptionsWithDependencyType = binding.RawResolveOptionsWithDependencyType;

// @public (undocumented)
type ResolveOptionsWithDependencyType_2 = Omit<binding.RawResolveOptionsWithDependencyType, "restrictions"> & {
restrictions?: (string | RegExp)[];
type ResolveOptionsWithDependencyType_2 = Resolve & {
dependencyCategory?: string;
resolveToContext?: boolean;
};

// @public (undocumented)
Expand All @@ -8220,7 +8224,7 @@ class Resolver {
// (undocumented)
resolveSync(context: object, path: string, request: string): string | false;
// (undocumented)
withOptions({ restrictions, ...rest }: ResolveOptionsWithDependencyType_2): Resolver;
withOptions({ dependencyCategory, resolveToContext, ...resolve }: ResolveOptionsWithDependencyType_2): Resolver;
}

// @public (undocumented)
Expand Down Expand Up @@ -8428,6 +8432,7 @@ declare namespace rspackExports {
SwcLoaderTransformConfig,
SwcLoaderTsParserConfig,
experiments,
getRawResolve,
getRawLibrary,
getRawChunkLoading,
LoaderContext,
Expand Down
29 changes: 18 additions & 11 deletions packages/rspack/src/Resolver.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import type * as binding from "@rspack/binding";
import { Resolve, getRawResolve } from "./config";

interface ResolveContext {}

type ErrorWithDetail = Error & { details?: string };

type ResolveOptionsWithDependencyType = Omit<
binding.RawResolveOptionsWithDependencyType,
"restrictions"
> & {
restrictions?: (string | RegExp)[];
type ResolveOptionsWithDependencyType = Resolve & {
dependencyCategory?: string;
resolveToContext?: boolean;
};

function isString(value: string | RegExp): value is string {
Expand Down Expand Up @@ -46,15 +45,23 @@ export class Resolver {
}

withOptions({
restrictions,
...rest
dependencyCategory,
resolveToContext,
...resolve
}: ResolveOptionsWithDependencyType): Resolver {
const bindingOptions: binding.RawResolveOptionsWithDependencyType = rest;
const rawResolve = getRawResolve(resolve);

// TODO: rspack_resolver is unimplemented regex
if (Array.isArray(restrictions)) {
bindingOptions.restrictions = restrictions.filter<string>(isString);
if (Array.isArray(rawResolve.restrictions)) {
rawResolve.restrictions =
rawResolve.restrictions.filter<string>(isString);
}
const binding = this.binding.withOptions(bindingOptions);

const binding = this.binding.withOptions({
dependencyCategory,
resolveToContext,
...rawResolve
});
return new Resolver(binding);
}
}
2 changes: 1 addition & 1 deletion packages/rspack/src/config/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function getRawTsConfig(
};
}

function getRawResolve(resolve: Resolve): RawOptions["resolve"] {
export function getRawResolve(resolve: Resolve): RawOptions["resolve"] {
return {
...resolve,
alias: getRawAlias(resolve.alias),
Expand Down

0 comments on commit 38dee5b

Please sign in to comment.