Skip to content

Commit a4996af

Browse files
authored
chore: document directory is an absolute path for resolve(directory, specifier) (#206)
relates #201
1 parent fe9a10a commit a4996af

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

napi/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,19 @@ impl ResolverFactory {
7575
self.resolver.clear_cache();
7676
}
7777

78+
/// Synchronously resolve `specifier` at an absolute path to a `directory`.
7879
#[allow(clippy::needless_pass_by_value)]
7980
#[napi]
80-
pub fn sync(&self, path: String, request: String) -> ResolveResult {
81-
let path = PathBuf::from(path);
81+
pub fn sync(&self, directory: String, request: String) -> ResolveResult {
82+
let path = PathBuf::from(directory);
8283
resolve(&self.resolver, &path, &request)
8384
}
8485

86+
/// Asynchronously resolve `specifier` at an absolute path to a `directory`.
8587
#[allow(clippy::needless_pass_by_value)]
8688
#[napi(js_name = "async")]
87-
pub async fn resolve_async(&self, path: String, request: String) -> ResolveResult {
88-
let path = PathBuf::from(path);
89+
pub async fn resolve_async(&self, directory: String, request: String) -> ResolveResult {
90+
let path = PathBuf::from(directory);
8991
let resolver = self.resolver.clone();
9092
tokio::spawn(async move { resolve(&resolver, &path, &request) }).await.unwrap()
9193
}

npm/README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,28 @@
22

33
See
44

5-
* index.d.ts for `resolveSync` and `ResolverFactory` API.
5+
* `index.d.ts` for `resolveSync` and `ResolverFactory` API.
66
* [README.md](https://github.com/oxc-project/oxc-resolver?tab=readme-ov-file#oxc-resolver) for options.
77

8-
## ESM
8+
## API
9+
10+
`resolve(directory, specifier)` - resolve `specifier` at an absolute path to a `directory`.
11+
12+
### `directory`
13+
14+
An **absolute** path to a directory where the specifier is resolved against.
15+
16+
For CommonJS modules, it is the `__dirname` variable that contains the absolute path to the folder containing current module.
17+
18+
For ECMAScript modules, it is the value of `import.meta.url`.
19+
20+
Behavior is undefined when given a path to a file.
21+
22+
### `specifier`
23+
24+
The string passed to `require` or `import`, i.e. `require("specifier")` or `import "specifier"`
25+
26+
## ESM Example
927

1028
```javascript
1129
import path from 'path';

src/lib.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
156156
self.cache.clear();
157157
}
158158

159-
/// Resolve `specifier` at an absolute `path`.
159+
/// Resolve `specifier` at an absolute path to a `directory`.
160160
///
161161
/// A specifier is the string passed to require or import, i.e. `require("specifier")` or `import "specifier"`.
162162
///
163-
/// `path` must be an **absolute** path to a directory where the specifier is resolved against.
163+
/// `directory` must be an **absolute** path to a directory where the specifier is resolved against.
164164
/// For CommonJS modules, it is the `__dirname` variable that contains the absolute path to the folder containing current module.
165165
/// For ECMAScript modules, it is the value of `import.meta.url`.
166166
///
@@ -169,11 +169,11 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
169169
/// * See [ResolveError]
170170
pub fn resolve<P: AsRef<Path>>(
171171
&self,
172-
path: P,
172+
directory: P,
173173
specifier: &str,
174174
) -> Result<Resolution, ResolveError> {
175175
let mut ctx = Ctx::default();
176-
self.resolve_tracing(path.as_ref(), specifier, &mut ctx)
176+
self.resolve_tracing(directory.as_ref(), specifier, &mut ctx)
177177
}
178178

179179
/// Resolve `specifier` at absolute `path` with [ResolveContext]
@@ -183,13 +183,13 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
183183
/// * See [ResolveError]
184184
pub fn resolve_with_context<P: AsRef<Path>>(
185185
&self,
186-
path: P,
186+
directory: P,
187187
specifier: &str,
188188
resolve_context: &mut ResolveContext,
189189
) -> Result<Resolution, ResolveError> {
190190
let mut ctx = Ctx::default();
191191
ctx.init_file_dependencies();
192-
let result = self.resolve_tracing(path.as_ref(), specifier, &mut ctx);
192+
let result = self.resolve_tracing(directory.as_ref(), specifier, &mut ctx);
193193
if let Some(deps) = &mut ctx.file_dependencies {
194194
resolve_context.file_dependencies.extend(deps.drain(..));
195195
}
@@ -202,19 +202,19 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
202202
/// Wrap `resolve_impl` with `tracing` information
203203
fn resolve_tracing(
204204
&self,
205-
path: &Path,
205+
directory: &Path,
206206
specifier: &str,
207207
ctx: &mut Ctx,
208208
) -> Result<Resolution, ResolveError> {
209-
let span = tracing::debug_span!("resolve", path = ?path, specifier = specifier);
209+
let span = tracing::debug_span!("resolve", path = ?directory, specifier = specifier);
210210
let _enter = span.enter();
211-
let r = self.resolve_impl(path, specifier, ctx);
211+
let r = self.resolve_impl(directory, specifier, ctx);
212212
match &r {
213213
Ok(r) => {
214-
tracing::debug!(options = ?self.options, path = ?path, specifier = specifier, ret = ?r.path);
214+
tracing::debug!(options = ?self.options, path = ?directory, specifier = specifier, ret = ?r.path);
215215
}
216216
Err(err) => {
217-
tracing::debug!(options = ?self.options, path = ?path, specifier = specifier, err = ?err);
217+
tracing::debug!(options = ?self.options, path = ?directory, specifier = specifier, err = ?err);
218218
}
219219
};
220220
r

0 commit comments

Comments
 (0)