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

perf(resolver): remove intermediate pcall util #230

Merged
merged 5 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 0 additions & 20 deletions src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,6 @@ export function normalizeSlash(string_) {
return string_.replace(/\\/g, "/");
}

export function pcall<TFn extends (...args: any[]) => any>(
function_: TFn,
...arguments_: Parameters<TFn>
): Promise<ReturnType<TFn>> {
try {
return Promise.resolve(function_(...arguments_)).catch((error) =>
perr(error),
);
} catch (error) {
return perr(error);
}
}

export function perr(_error) {
const error = new Error(_error);
error.code = _error.code;
Error.captureStackTrace(error, pcall);
return Promise.reject(error);
}

export function isObject(value) {
return value !== null && typeof value === "object";
}
Expand Down
14 changes: 11 additions & 3 deletions src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isAbsolute, normalize } from "pathe";
import { moduleResolve } from "import-meta-resolve";
import { PackageJson, readPackageJSON } from "pkg-types";
import { fileURLToPath, normalizeid } from "./utils";
import { pcall, BUILTIN_MODULES } from "./_utils";
import { BUILTIN_MODULES } from "./_utils";

const DEFAULT_CONDITIONS_SET = new Set(["node", "import"]);
const DEFAULT_URL = pathToFileURL(process.cwd());
Expand Down Expand Up @@ -136,7 +136,11 @@ export function resolveSync(id: string, options?: ResolveOptions): string {
}

export function resolve(id: string, options?: ResolveOptions): Promise<string> {
return pcall(resolveSync, id, options);
try {
return Promise.resolve(resolveSync(id, options));
} catch (error) {
return Promise.reject(error);
}
}

export function resolvePathSync(id: string, options?: ResolveOptions): string {
Expand All @@ -147,7 +151,11 @@ export function resolvePath(
id: string,
options?: ResolveOptions,
): Promise<string> {
return pcall(resolvePathSync, id, options);
try {
return Promise.resolve(resolvePathSync(id, options));
} catch (error) {
return Promise.reject(error);
}
}

export function createResolve(defaults?: ResolveOptions) {
Expand Down