Skip to content

Commit 949f80c

Browse files
committed
chore: update docs
1 parent f57e220 commit 949f80c

File tree

1 file changed

+35
-40
lines changed

1 file changed

+35
-40
lines changed

README.md

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@
99

1010
> 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).
1111
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:
1313

1414
- Pure JS with no native dependencies (only Node.js is required).
15-
- Stable and versioned behavior.
1615
- Throws an error if the resolved path does not exist in the filesystem.
1716
- Can override the default [conditions](#conditions).
1817
- 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.
2120

2221
## Usage
2322

24-
Install package:
23+
Install the package:
2524

2625
```sh
2726
# ✨ Auto-detect (npm, yarn, pnpm, bun, deno)
@@ -52,59 +51,55 @@ Differences between `resolveModuleURL` and `resolveModulePath`:
5251

5352
- `resolveModuleURL` returns a URL string like `file:///app/dep.mjs`.
5453
- `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.
7455

7556
## Resolve options
7657

7758
### `from`
7859

79-
- Default: (current working directory)
60+
A URL, path, or array of URLs/paths to resolve the module from.
8061

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.
8263

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()`.
8465

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.
8671
87-
- Default: `["node", "import"]`
72+
### `conditions`
8873

89-
Conditions to apply when resolving package exports.
74+
Conditions to apply when resolving package exports (default: `["node", "import"]`).
9075

9176
### `suffixes`
9277

93-
- Default: `["/index"]`
78+
Suffixes to check as fallbacks (default: `["/index"]`).
9479

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: []`.
9986
10087
### `extensions`
10188

102-
- Default `[".mjs", ".cjs", ".js", ".mts", ".cts", ".ts", ".json"]`
89+
Additional file extensions to check as fallbacks (default: `[".mjs", ".cjs", ".js", ".mts", ".cts", ".ts", ".json"]`).
90+
91+
> [!TIP]
92+
> Extension fallbacks are only checked if the input does not have an explicit extension.
93+
>
94+
> For better performance, ensure the input ends with an explicit extension.
95+
>
96+
> You can disable extension fallbacks by setting `extensions: []`.
97+
98+
## Other Performance Tips
10399

104-
Additional file extensions to consider when resolving modules.
100+
**Use explicit module extensions `.mjs` or `.cjs` instead of `.js`:**
105101

106-
> [!NOTE]
107-
> For performance, extension fallbacks are only checked if input does not have an explicit extension.
102+
This allows the resolution fast path to skip reading the closest `package.json` for the [`type`](https://nodejs.org/api/packages.html#type).
108103

109104
## Development
110105

@@ -113,7 +108,7 @@ Additional file extensions to consider when resolving modules.
113108
<summary>local development</summary>
114109

115110
- Clone this repository
116-
- Install latest LTS version of [Node.js](https://nodejs.org/en/)
111+
- Install the latest LTS version of [Node.js](https://nodejs.org/en/)
117112
- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
118113
- Install dependencies using `pnpm install`
119114
- Run interactive tests using `pnpm dev`

0 commit comments

Comments
 (0)