Skip to content

Commit e678d0b

Browse files
authored
feat(napi): expose module type info in ResolveResult (#223)
1 parent 5d4421a commit e678d0b

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

napi/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ export interface NapiResolveOptions {
188188
export interface ResolveResult {
189189
path?: string
190190
error?: string
191+
/** "type" field in the package.json file */
192+
moduleType?: string
191193
}
192194

193195
/**
@@ -199,7 +201,7 @@ export interface Restriction {
199201
regex?: string
200202
}
201203

202-
export function sync(path: string, request: string): ResolveResult
204+
export declare function sync(path: string, request: string): ResolveResult
203205

204206
/**
205207
* Tsconfig Options

napi/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,22 @@ mod tracing;
2323
pub struct ResolveResult {
2424
pub path: Option<String>,
2525
pub error: Option<String>,
26+
/// "type" field in the package.json file
27+
pub module_type: Option<String>,
2628
}
2729

2830
fn resolve(resolver: &Resolver, path: &Path, request: &str) -> ResolveResult {
2931
match resolver.resolve(path, request) {
3032
Ok(resolution) => ResolveResult {
3133
path: Some(resolution.full_path().to_string_lossy().to_string()),
3234
error: None,
35+
module_type: resolution
36+
.package_json()
37+
.and_then(|p| p.r#type.as_ref())
38+
.and_then(|t| t.as_str())
39+
.map(|t| t.to_string()),
3340
},
34-
Err(err) => ResolveResult { path: None, error: Some(err.to_string()) },
41+
Err(err) => ResolveResult { path: None, module_type: None, error: Some(err.to_string()) },
3542
}
3643
}
3744

0 commit comments

Comments
 (0)