Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(napi): expose module type info in ResolveResult #223

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion napi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ export interface NapiResolveOptions {
export interface ResolveResult {
path?: string
error?: string
/** "type" field in the package.json file */
moduleType?: string
}

/**
Expand All @@ -199,7 +201,7 @@ export interface Restriction {
regex?: string
}

export function sync(path: string, request: string): ResolveResult
export declare function sync(path: string, request: string): ResolveResult

/**
* Tsconfig Options
Expand Down
9 changes: 8 additions & 1 deletion napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,22 @@ mod tracing;
pub struct ResolveResult {
pub path: Option<String>,
pub error: Option<String>,
/// "type" field in the package.json file
pub module_type: Option<String>,
}

fn resolve(resolver: &Resolver, path: &Path, request: &str) -> ResolveResult {
match resolver.resolve(path, request) {
Ok(resolution) => ResolveResult {
path: Some(resolution.full_path().to_string_lossy().to_string()),
error: None,
module_type: resolution
.package_json()
.and_then(|p| p.r#type.as_ref())
.and_then(|t| t.as_str())
.map(|t| t.to_string()),
},
Err(err) => ResolveResult { path: None, error: Some(err.to_string()) },
Err(err) => ResolveResult { path: None, module_type: None, error: Some(err.to_string()) },
}
}

Expand Down