Skip to content

Commit 69ab48c

Browse files
committed
refactor!: allow reorder "" suffix
1 parent 0fe2d1d commit 69ab48c

File tree

3 files changed

+17
-21
lines changed

3 files changed

+17
-21
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This library exposes an API similar to [`import.meta.resolve`](https://nodejs.or
1313
- Throws an error (or [try](#try)) if the resolved path does not exist in the filesystem.
1414
- Can override the default [conditions](#conditions).
1515
- Can resolve [from](#from) one or more parent URLs.
16-
- Can resolve with implicit `/index` [suffixes](#suffixes) as fallback.
16+
- Can resolve with custom [suffixes](#suffixes).
1717
- Can resolve with implicit [extensions](#extensions) as fallback.
1818

1919
## Usage
@@ -66,7 +66,7 @@ You can create a custom resolver instance with default [options](#resolve-option
6666
import { createResolver } from "exsolve";
6767

6868
const { resolveModuleURL, resolveModulePath } = createResolver({
69-
suffixes: ["/index"],
69+
suffixes: ["", "/index"],
7070
extensions: [".mjs", ".cjs", ".js", ".mts", ".cts", ".ts", ".json"],
7171
conditions: ["node", "import", "production"],
7272
});
@@ -166,14 +166,14 @@ const src = resolveModulePath("./src/index", {
166166
167167
### `suffixes`
168168

169-
Additional path suffixes to check as fallbacks.
169+
Path suffixes to check.
170170

171171
**Example:**
172172

173173
```ts
174174
// "/app/src/utils/index.ts"
175175
const src = resolveModulePath("./src/utils", {
176-
suffixes: ["/index"],
176+
suffixes: ["", "/index"],
177177
extensions: [".mjs", ".cjs", ".js"],
178178
});
179179
```
@@ -190,7 +190,7 @@ Resolve cache (enabled by default with a shared global object).
190190

191191
Can be set to `false` to disable or a custom `Map` to bring your own cache object.
192192

193-
See [cache](#cache) for more info.
193+
See [cache](#resolve-cache) for more info.
194194

195195
## Other Performance Tips
196196

src/resolve.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type ResolveOptions = {
4141
cache?: boolean | Map<string, unknown>;
4242

4343
/**
44-
* Additional file extensions to check as fallbacks.
44+
* Additional file extensions to check as fallback.
4545
* These are used only if the input does not have an explicit extension.
4646
* For better performance, use explicit extensions.
4747
*/
@@ -55,8 +55,11 @@ export type ResolveOptions = {
5555
conditions?: string[];
5656

5757
/**
58-
* Additional path suffixes to check as fallbacks.
59-
* Suffix fallbacks are skipped if the input ends with the same suffix.
58+
* Path suffixes to check.
59+
* Suffixes are skipped if the input ends with the same suffix.
60+
*
61+
* @example ["", "/index"]
62+
*
6063
* For better performance, use explicit suffix like `/index` when needed.
6164
*/
6265
suffixes?: string[];
@@ -162,26 +165,19 @@ export function resolveModuleURL<O extends ResolveOptions>(
162165
const urls: URL[] = _normalizeResolveParents(options?.from);
163166
let resolved: URL | undefined;
164167

165-
const extensionsToCheck = (extname(id) === "" /* no extension */ &&
166-
options?.extensions) || [""];
168+
const suffixesToCheck = options?.suffixes || [""];
167169

168-
const suffixesToCheck = ["", ...(options?.suffixes || [])];
170+
const extensionsToCheck =
171+
extname(id) === "" /* no extension */
172+
? ["", ...(options?.extensions || [])]
173+
: [""];
169174

170175
for (const url of urls) {
171-
// Try simple resolve
172-
resolved = _tryModuleResolve(id, url, conditionsSet);
173-
if (resolved) {
174-
break;
175-
}
176-
// Try other extensions and suffixes if not found
177176
for (const suffix of suffixesToCheck) {
178177
if (suffix && id.endsWith(suffix)) {
179178
continue;
180179
}
181180
for (const extension of extensionsToCheck) {
182-
if (!suffix && !extension) {
183-
continue;
184-
}
185181
resolved = _tryModuleResolve(
186182
`${id}${suffix}`.replace(/\/+/g, "/") + extension,
187183
url,

test/resolve.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const tests = [
2121

2222
const extensions = [".mjs", ".cjs", ".js", ".mts", ".cts", ".ts", ".json"];
2323

24-
const suffixes = ["/index"];
24+
const suffixes = ["", "/index"];
2525

2626
describe("resolveModuleURL", () => {
2727
for (const test of tests) {

0 commit comments

Comments
 (0)