You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+35-40Lines changed: 35 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,19 +9,18 @@
9
9
10
10
> Module resolution utilities for Node.js (based on previous work in [unjs/mlly](https://github.com/unjs/mlly), [wooorm/import-meta-resolve](https://github.com/wooorm/import-meta-resolve), and the upstream [Node.js](https://github.com/nodejs/node) implementation).
11
11
12
-
This library exposes an API similar to [`import.meta.resolve`](https://nodejs.org/api/esm.html#importmetaresolvespecifier) based on Node.js upstream implementation and [resolution algorithm](https://nodejs.org/api/esm.html#esm_resolution_algorithm). It supports all built-in functionalities—`package.json`, import maps, export maps, CJS, and ESM—with some additions:
12
+
This library exposes an API similar to [`import.meta.resolve`](https://nodejs.org/api/esm.html#importmetaresolvespecifier) based on Node.js's upstream implementation and [resolution algorithm](https://nodejs.org/api/esm.html#esm_resolution_algorithm). It supports all built-in functionalities—import maps, export maps, CJS, and ESM—with some additions:
13
13
14
14
- Pure JS with no native dependencies (only Node.js is required).
15
-
- Stable and versioned behavior.
16
15
- Throws an error if the resolved path does not exist in the filesystem.
17
16
- Can override the default [conditions](#conditions).
18
17
- Can resolve [from](#from) one or more parent URLs.
19
-
- Can resolve with implicit `/index`[suffix](#suffixes) as fallback.
20
-
- Can resolve with implicit [extensions](#extensions) if not provided.
18
+
- Can resolve with implicit `/index`[suffixes](#suffixes) as a fallback.
19
+
- Can resolve with implicit [extensions](#extensions) if as fallback.
21
20
22
21
## Usage
23
22
24
-
Install package:
23
+
Install the package:
25
24
26
25
```sh
27
26
# ✨ Auto-detect (npm, yarn, pnpm, bun, deno)
@@ -52,59 +51,55 @@ Differences between `resolveModuleURL` and `resolveModulePath`:
52
51
53
52
-`resolveModuleURL` returns a URL string like `file:///app/dep.mjs`.
54
53
-`resolveModulePath` returns an absolute path like `/app/dep.mjs`.
55
-
- If the resolved URL does not use the `file://` scheme (for example, `data:` or `node:`), it will throw an error.
56
-
57
-
## Performance tips
58
-
59
-
Resolution can be an expensive operation with various fallbacks and filesystem checks. By making stricter assumptions, we can reduce this.
60
-
61
-
### Disable [`suffixes`](#suffixes) and [`extensions`](#extensions)
62
-
63
-
Use `{ suffixes: [], extensions: [] }` and make sure resolve is always called with `./utils/index.ts` instead of `./utils`.
64
-
65
-
### Use explicit module extensions `.mjs` or `.cjs` instead of `.js`
66
-
67
-
This allows resolution fast-path to skip reading the closest `package.json` for the [`type`](https://nodejs.org/api/packages.html#type).
68
-
69
-
### Make sure [`from`](#from) are explicit file URLs
70
-
71
-
The paths set for [`from`](#from) should be absolute paths to the file that is requesting resolve.
72
-
If it is set to an absolute path, resolver needs to first stat it to see if it is a file or directory.
73
-
If input is `file://`, a URL or ends with `/`, resolver can skip this check.
54
+
- If the resolved URL does not use the `file://` scheme (e.g., `data:` or `node:`), it will throw an error.
74
55
75
56
## Resolve options
76
57
77
58
### `from`
78
59
79
-
- Default: (current working directory)
60
+
A URL, path, or array of URLs/paths to resolve the module from.
80
61
81
-
A URL, path or array of URLs/paths to resolve module against them.
62
+
If not provided, the resolution will start from the current working directory, but setting it is highly recommended.
82
63
83
-
You can use `import.meta.url` for `from` to make behavior like`import.meta.resolve()`.
64
+
You can use `import.meta.url` for `from` to mimic the behavior of`import.meta.resolve()`.
84
65
85
-
### `conditions`
66
+
> [!TIP]
67
+
> For better performance, ensure the value is a `file://` URL.
68
+
>
69
+
> If it is set to an absolute path, the resolver first checks the filesystem to determine if it is a file or directory.
70
+
> If the input is a `file://` URL or ends with `/`, the resolver can skip this check.
86
71
87
-
- Default: `["node", "import"]`
72
+
### `conditions`
88
73
89
-
Conditions to apply when resolving package exports.
74
+
Conditions to apply when resolving package exports (default: `["node", "import"]`).
90
75
91
76
### `suffixes`
92
77
93
-
- Default: `["/index"]`
78
+
Suffixes to check as fallbacks (default: `["/index"]`).
94
79
95
-
Suffixes to check as fallback.
96
-
97
-
> [!NOTE]
98
-
> For performance, suffix fallbacks are skipped if input itself ends with the same suffix.
80
+
> [!TIP]
81
+
> Suffix fallbacks are skipped if the input ends with the same suffix.
82
+
>
83
+
> For better performance, always use explicit `/index` when needed.
84
+
>
85
+
> You can disable suffix fallbacks by setting `suffixes: []`.
0 commit comments