Skip to content

Commit 146899e

Browse files
committed
refactor: fully migrate to exsolve for module resolution
1 parent 964bc29 commit 146899e

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"confbox": "^0.1.8",
3636
"defu": "^6.1.4",
3737
"dotenv": "^16.4.7",
38-
"exsolve": "^0.4.0",
38+
"exsolve": "^0.4.1",
3939
"giget": "^1.2.5",
4040
"jiti": "^2.4.2",
4141
"ohash": "^2.0.5",

pnpm-lock.yaml

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/loader.ts

+23-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { existsSync } from "node:fs";
22
import { readFile, rm } from "node:fs/promises";
3+
import { pathToFileURL } from "node:url";
34
import { homedir } from "node:os";
45
import { resolve, extname, dirname, basename, join } from "pathe";
6+
import { resolveModulePath } from "exsolve";
57
import { createJiti } from "jiti";
6-
import { fileURLToPath } from "mlly";
78
import * as rc9 from "rc9";
89
import { defu } from "defu";
910
import { findWorkspaceDir, readPackageJSON } from "pkg-types";
@@ -31,7 +32,7 @@ const ASYNC_LOADERS = {
3132
".toml": () => import("confbox/toml").then((r) => r.parseTOML),
3233
} as const;
3334

34-
export const SUPPORTED_EXTENSIONS = [
35+
export const SUPPORTED_EXTENSIONS = Object.freeze([
3536
// with jiti
3637
".js",
3738
".ts",
@@ -46,7 +47,7 @@ export const SUPPORTED_EXTENSIONS = [
4647
".yaml",
4748
".yml",
4849
".toml",
49-
] as const;
50+
]) as unknown as string[];
5051

5152
export async function loadConfig<
5253
T extends UserInputConfig = UserInputConfig,
@@ -355,15 +356,9 @@ async function resolveConfig<
355356
source = cloned.dir;
356357
}
357358

358-
// Util to try resolving a module
359-
const tryResolve = (id: string) => {
360-
const resolved = options.jiti!.esmResolve(id, { try: true });
361-
return resolved ? fileURLToPath(resolved) : undefined;
362-
};
363-
364359
// Try resolving as npm package
365360
if (NPM_PACKAGE_RE.test(source)) {
366-
source = tryResolve(source) || source;
361+
source = tryResolve(source, options) || source;
367362
}
368363

369364
// Import from local fs
@@ -382,9 +377,12 @@ async function resolveConfig<
382377
};
383378

384379
res.configFile =
385-
tryResolve(resolve(cwd, source)) ||
386-
tryResolve(resolve(cwd, ".config", source.replace(/\.config$/, ""))) ||
387-
tryResolve(resolve(cwd, ".config", source)) ||
380+
tryResolve(resolve(cwd, source), options) ||
381+
tryResolve(
382+
resolve(cwd, ".config", source.replace(/\.config$/, "")),
383+
options,
384+
) ||
385+
tryResolve(resolve(cwd, ".config", source), options) ||
388386
source;
389387

390388
if (!existsSync(res.configFile!)) {
@@ -432,3 +430,15 @@ async function resolveConfig<
432430

433431
return res;
434432
}
433+
434+
// --- internal ---
435+
436+
function tryResolve(id: string, options: LoadConfigOptions<any, any>) {
437+
return resolveModulePath(id, {
438+
try: true,
439+
from: pathToFileURL(join(options.cwd || ".", options.configFile || "/")),
440+
suffixes: ["", "/index"],
441+
extensions: SUPPORTED_EXTENSIONS,
442+
cache: false,
443+
});
444+
}

src/update.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,13 @@ export async function updateConfig(
7979

8080
// --- Internal ---
8181

82-
function tryResolve(path: string, cwd: string, exts: readonly string[]) {
82+
function tryResolve(path: string, cwd: string, extensions: string[]) {
8383
return resolveModulePath(path, {
8484
try: true,
8585
from: join(cwd, "/"),
86-
extensions: exts as string[],
86+
extensions,
87+
suffixes: ["", "/index"],
88+
cache: false,
8789
});
8890
}
8991

0 commit comments

Comments
 (0)