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
{{ message }}
Repository navigation
Commit f3b08dd
Browse filesBrowse the repository at this point in the historyBrowse files
Rename the positional `resolve`/`resolveSync`/`resolvePromise` parameters to `parent`/`specifier` (from `path`/`request`) for ESM-aligned naming and document them in the README. Purely cosmetic — arguments are positional, so there is no behavior or API change.
Async Node-style resolver using the built-in defaults (`conditionNames: ["node"]`, `extensions: [".js", ".json", ".node"]`). `context` is optional; when omitted, a built-in Node context is used.
65
+
Async Node-style resolver using the built-in defaults (`conditionNames: ["node"]`, `extensions: [".js", ".json", ".node"]`). Both `context` and `resolveContext` are optional, so the function accepts four shapes:
resolve(parent, specifier, callback); // most common
72
+
```
73
+
74
+
The `parent` / `specifier` naming mirrors the ECMAScript resolution terms (as in `import.meta.resolve(specifier, parent)`): `parent` is the location you resolve **from**, and `specifier` is the string being resolved.
75
+
76
+
##### Arguments — what each one is and when to pass it
77
+
78
+
| Argument | Required | What it is | When to pass it |
|`context`| no | Extra info about _who_ is requesting, e.g. `{ environments: ["node+es3+es5+process+native"] }`. Plugins can read arbitrary keys off it. When omitted, a built-in Node context is used. | Pass it only when a plugin you use reads from it. Most callers omit it. |
81
+
|`parent`| yes | The absolute directory to resolve **from** — the location the `specifier` is relative to (the ECMAScript "parent", historically called the "lookup start path"). | Always. |
82
+
|`specifier`| yes | The string to resolve — a relative specifier (`./utils`), a bare module (`lodash/merge`), an alias, etc. (the ECMAScript "specifier", historically called the "request"). | Always. |
83
+
|`resolveContext`| no | A collector object the resolver writes into / reads from: `fileDependencies`, `contextDependencies`, `missingDependencies` (`Set`s it fills in), `log` (a function it calls for trace output). | Pass it when you need to know which files/dirs were touched (e.g. to set up a watcher) or want a log of the resolution steps. |
84
+
|`callback`| yes |`(err, result, resolveRequest) => void`. `result` is the resolved absolute path (or `false` if the request resolves to an ignored module). `resolveRequest` is the full request object with `path`, `query`, `fragment`, `descriptionFilePath`, etc. | Always. |
85
+
86
+
##### Minimal form — just `parent`, `specifier`, `callback`
66
87
67
88
```js
68
89
constresolve=require("enhanced-resolve");
69
90
70
91
resolve(__dirname, "./utils", (err, result) => {
71
-
// result === "/abs/path/to/utils.js"
92
+
if (err) throw err;
93
+
result; // === "/abs/path/to/utils.js"
94
+
});
95
+
```
96
+
97
+
##### Inspecting the full result via the third callback argument
##### With `resolveContext` — collect dependencies and a log
110
+
111
+
Pass a `resolveContext` when you need the resolver to report back _what it looked at_. The `Set`s you provide are filled in during resolution, and `log` is called with each step:
112
+
113
+
```js
114
+
constfileDependencies=newSet();
115
+
constmissingDependencies=newSet();
116
+
constcontextDependencies=newSet();
117
+
constlogs= [];
118
+
119
+
resolve(
120
+
__dirname,
121
+
"./utils",
122
+
{
123
+
fileDependencies, // files that were read/checked
124
+
missingDependencies, // paths that were probed but didn't exist
125
+
contextDependencies, // directories that were read
126
+
log: (msg) =>logs.push(msg), // trace of each resolution step
127
+
},
128
+
(err, result) => {
129
+
result; // === "/abs/path/to/utils.js"
130
+
fileDependencies; // Set { ".../utils.js", ".../package.json", ... }
131
+
missingDependencies; // Set { ".../utils", ".../utils.json", ... } (candidates tried)
132
+
// `fileDependencies` / `missingDependencies` are exactly what you'd feed
133
+
// a file watcher so a rebuild re-runs when any of them changes.
134
+
},
135
+
);
136
+
```
137
+
138
+
##### With a custom `context`
139
+
140
+
Pass a `context` when a plugin reads from it. The default resolver only uses `environments`, but custom plugins may key off anything you put here:
Lower-level factory. Returns a `Resolver` whose `resolve`, `resolveSync`, and `resolvePromise` methods accept `(context, path, request, resolveContext, [callback])`. Use this when you need a reusable resolver instance or access to its `hooks` (see the [Plugins](#plugins) section). `fileSystem` is required here — the high-level `resolve.create` defaults it for you.
201
+
Lower-level factory. Returns a `Resolver` whose `resolve`, `resolveSync`, and `resolvePromise` methods accept `(context, parent, specifier, resolveContext, [callback])`. Use this when you need a reusable resolver instance or access to its `hooks` (see the [Plugins](#plugins) section). `fileSystem` is required here — the high-level `resolve.create` defaults it for you.
0 commit comments