Skip to content

Commit

Permalink
perf(resolver): remove intermediate pcall util (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Feb 21, 2024
1 parent 99606ea commit 4469e7b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 23 deletions.
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

0 comments on commit 4469e7b

Please sign in to comment.