From 95b605aaea6de189501a28c4b531e9aa3b6d917a Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 11 Jul 2024 15:27:35 +0800 Subject: [PATCH] feat(napi): expose module type info in ResolveResult --- napi/index.d.ts | 4 +++- napi/src/lib.rs | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/napi/index.d.ts b/napi/index.d.ts index 56c6ceca..0bf6ee4c 100644 --- a/napi/index.d.ts +++ b/napi/index.d.ts @@ -188,6 +188,8 @@ export interface NapiResolveOptions { export interface ResolveResult { path?: string error?: string + /** "type" field in the package.json file */ + moduleType?: string } /** @@ -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 diff --git a/napi/src/lib.rs b/napi/src/lib.rs index 06e369d8..7cd839c5 100644 --- a/napi/src/lib.rs +++ b/napi/src/lib.rs @@ -23,6 +23,8 @@ mod tracing; pub struct ResolveResult { pub path: Option, pub error: Option, + /// "type" field in the package.json file + pub module_type: Option, } fn resolve(resolver: &Resolver, path: &Path, request: &str) -> ResolveResult { @@ -30,8 +32,13 @@ fn resolve(resolver: &Resolver, path: &Path, request: &str) -> ResolveResult { 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()) }, } }