From 28ae88661a77fb0f6e0d79b85cbb5705100b511a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Wed, 12 Feb 2025 21:36:58 +0800 Subject: [PATCH 1/3] feat: expose `package_json_path` --- napi/src/lib.rs | 15 +++++++++++++-- napi/src/options.rs | 26 +++++++++++++------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/napi/src/lib.rs b/napi/src/lib.rs index 9593c50c..0365ea60 100644 --- a/napi/src/lib.rs +++ b/napi/src/lib.rs @@ -25,6 +25,7 @@ pub struct ResolveResult { pub error: Option, /// "type" field in the package.json file pub module_type: Option, + pub package_json_path: Option, } fn resolve(resolver: &Resolver, path: &Path, request: &str) -> ResolveResult { @@ -33,8 +34,17 @@ fn resolve(resolver: &Resolver, path: &Path, request: &str) -> ResolveResult { path: Some(resolution.full_path().to_string_lossy().to_string()), error: None, module_type: resolution.package_json().and_then(|p| p.r#type()).map(|t| t.to_string()), + package_json_path: resolution + .package_json() + .and_then(|p| p.path().to_str()) + .map(|p| p.to_string()), + }, + Err(err) => ResolveResult { + path: None, + module_type: None, + error: Some(err.to_string()), + package_json_path: None, }, - Err(err) => ResolveResult { path: None, module_type: None, error: Some(err.to_string()) }, } } @@ -76,11 +86,12 @@ impl ResolverFactory { #[napi(constructor)] pub fn new(options: Option) -> Self { init_tracing(); - let options = options.map_or_else(|| ResolveOptions::default(), Self::normalize_options); + let options = options.map_or_else(ResolveOptions::default, Self::normalize_options); Self { resolver: Arc::new(Resolver::new(options)) } } #[napi] + #[allow(clippy::should_implement_trait)] pub fn default() -> Self { let default_options = ResolveOptions::default(); Self { resolver: Arc::new(Resolver::new(default_options)) } diff --git a/napi/src/options.rs b/napi/src/options.rs index 39983ace..a817461d 100644 --- a/napi/src/options.rs +++ b/napi/src/options.rs @@ -202,9 +202,9 @@ pub struct TsconfigOptions { pub references: Option>>, } -impl Into for Restriction { - fn into(self) -> oxc_resolver::Restriction { - match (self.path, self.regex) { +impl From for oxc_resolver::Restriction { + fn from(val: Restriction) -> Self { + match (val.path, val.regex) { (None, None) => { panic!("Should specify path or regex") } @@ -217,9 +217,9 @@ impl Into for Restriction { } } -impl Into for EnforceExtension { - fn into(self) -> oxc_resolver::EnforceExtension { - match self { +impl From for oxc_resolver::EnforceExtension { + fn from(val: EnforceExtension) -> Self { + match val { EnforceExtension::Auto => oxc_resolver::EnforceExtension::Auto, EnforceExtension::Enabled => oxc_resolver::EnforceExtension::Enabled, EnforceExtension::Disabled => oxc_resolver::EnforceExtension::Disabled, @@ -227,11 +227,11 @@ impl Into for EnforceExtension { } } -impl Into for TsconfigOptions { - fn into(self) -> oxc_resolver::TsconfigOptions { +impl From for oxc_resolver::TsconfigOptions { + fn from(val: TsconfigOptions) -> Self { oxc_resolver::TsconfigOptions { - config_file: PathBuf::from(self.config_file), - references: match self.references { + config_file: PathBuf::from(val.config_file), + references: match val.references { Some(Either::A(string)) if string.as_str() == "auto" => { oxc_resolver::TsconfigReferences::Auto } @@ -250,9 +250,9 @@ impl Into for TsconfigOptions { type StrOrStrListType = Either>; pub struct StrOrStrList(pub StrOrStrListType); -impl Into> for StrOrStrList { - fn into(self) -> Vec { - match self { +impl From for Vec { + fn from(val: StrOrStrList) -> Self { + match val { StrOrStrList(Either::A(s)) => Vec::from([s]), StrOrStrList(Either::B(a)) => a, } From 4ee42730785824db93699d31fb20e819d8ce4b30 Mon Sep 17 00:00:00 2001 From: Boshen Date: Fri, 14 Feb 2025 18:01:14 +0800 Subject: [PATCH 2/3] u --- napi/__test__/resolver.spec.mjs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/napi/__test__/resolver.spec.mjs b/napi/__test__/resolver.spec.mjs index 910345cc..ac896845 100644 --- a/napi/__test__/resolver.spec.mjs +++ b/napi/__test__/resolver.spec.mjs @@ -239,6 +239,7 @@ test('resolve pnpm package', (t) => { aliasFields: ['browser'], }); t.deepEqual(resolver.sync(pnpmProjectPath, 'styled-components'), { + packageJsonPath: join(rootDir, 'fixtures/pnpm/node_modules/styled-components/package.json'), path: join( rootDir, 'node_modules/.pnpm/styled-components@6.1.1_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/styled-components/dist/styled-components.browser.cjs.js', @@ -257,6 +258,7 @@ test('resolve pnpm package', (t) => { rootDir, 'node_modules/.pnpm/react@18.3.1/node_modules/react/index.js', ), + packageJsonPath: join(rootDir, 'fixtures/pnpm/node_modules/styled-components/package.json'), }, ); }); From 082a3793f944d175b088cb729a07315453d50615 Mon Sep 17 00:00:00 2001 From: Boshen Date: Fri, 14 Feb 2025 18:58:36 +0800 Subject: [PATCH 3/3] u --- napi/__test__/resolver.spec.mjs | 35 +++++++++++++++++---------------- napi/index.d.ts | 1 + 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/napi/__test__/resolver.spec.mjs b/napi/__test__/resolver.spec.mjs index ac896845..67a3edb3 100644 --- a/napi/__test__/resolver.spec.mjs +++ b/napi/__test__/resolver.spec.mjs @@ -238,27 +238,28 @@ test('resolve pnpm package', (t) => { const resolver = new ResolverFactory({ aliasFields: ['browser'], }); - t.deepEqual(resolver.sync(pnpmProjectPath, 'styled-components'), { - packageJsonPath: join(rootDir, 'fixtures/pnpm/node_modules/styled-components/package.json'), - path: join( + + const styledComponents = resolver.sync(pnpmProjectPath, 'styled-components'); + t.deepEqual( + styledComponents.path, + join( rootDir, 'node_modules/.pnpm/styled-components@6.1.1_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/styled-components/dist/styled-components.browser.cjs.js', ), - }); + ); + + const react = resolver.sync( + join( + rootDir, + 'node_modules/.pnpm/styled-components@6.1.1_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/styled-components', + ), + 'react', + ); t.deepEqual( - resolver.sync( - join( - rootDir, - 'node_modules/.pnpm/styled-components@6.1.1_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/styled-components', - ), - 'react', + react.path, + join( + rootDir, + 'node_modules/.pnpm/react@18.3.1/node_modules/react/index.js', ), - { - path: join( - rootDir, - 'node_modules/.pnpm/react@18.3.1/node_modules/react/index.js', - ), - packageJsonPath: join(rootDir, 'fixtures/pnpm/node_modules/styled-components/package.json'), - }, ); }); diff --git a/napi/index.d.ts b/napi/index.d.ts index 1df5044a..bca30f39 100644 --- a/napi/index.d.ts +++ b/napi/index.d.ts @@ -190,6 +190,7 @@ export interface ResolveResult { error?: string; /** "type" field in the package.json file */ moduleType?: string; + packageJsonPath?: string; } /**