Skip to content

Commit f65006a

Browse files
sxzzBoshen
andauthored
feat: expose package_json_path (#376)
--------- Co-authored-by: Boshen <[email protected]>
1 parent 753a47b commit f65006a

File tree

4 files changed

+45
-30
lines changed

4 files changed

+45
-30
lines changed

napi/__test__/resolver.spec.mjs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -238,25 +238,28 @@ test('resolve pnpm package', (t) => {
238238
const resolver = new ResolverFactory({
239239
aliasFields: ['browser'],
240240
});
241-
t.deepEqual(resolver.sync(pnpmProjectPath, 'styled-components'), {
242-
path: join(
241+
242+
const styledComponents = resolver.sync(pnpmProjectPath, 'styled-components');
243+
t.deepEqual(
244+
styledComponents.path,
245+
join(
243246
rootDir,
244247
'node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/styled-components/dist/styled-components.browser.cjs.js',
245248
),
246-
});
249+
);
250+
251+
const react = resolver.sync(
252+
join(
253+
rootDir,
254+
'node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/styled-components',
255+
),
256+
'react',
257+
);
247258
t.deepEqual(
248-
resolver.sync(
249-
join(
250-
rootDir,
251-
'node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/styled-components',
252-
),
253-
'react',
259+
react.path,
260+
join(
261+
rootDir,
262+
'node_modules/.pnpm/[email protected]/node_modules/react/index.js',
254263
),
255-
{
256-
path: join(
257-
rootDir,
258-
'node_modules/.pnpm/[email protected]/node_modules/react/index.js',
259-
),
260-
},
261264
);
262265
});

napi/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ export interface ResolveResult {
190190
error?: string;
191191
/** "type" field in the package.json file */
192192
moduleType?: string;
193+
packageJsonPath?: string;
193194
}
194195

195196
/**

napi/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct ResolveResult {
2525
pub error: Option<String>,
2626
/// "type" field in the package.json file
2727
pub module_type: Option<String>,
28+
pub package_json_path: Option<String>,
2829
}
2930

3031
fn resolve(resolver: &Resolver, path: &Path, request: &str) -> ResolveResult {
@@ -33,8 +34,17 @@ fn resolve(resolver: &Resolver, path: &Path, request: &str) -> ResolveResult {
3334
path: Some(resolution.full_path().to_string_lossy().to_string()),
3435
error: None,
3536
module_type: resolution.package_json().and_then(|p| p.r#type()).map(|t| t.to_string()),
37+
package_json_path: resolution
38+
.package_json()
39+
.and_then(|p| p.path().to_str())
40+
.map(|p| p.to_string()),
41+
},
42+
Err(err) => ResolveResult {
43+
path: None,
44+
module_type: None,
45+
error: Some(err.to_string()),
46+
package_json_path: None,
3647
},
37-
Err(err) => ResolveResult { path: None, module_type: None, error: Some(err.to_string()) },
3848
}
3949
}
4050

@@ -76,11 +86,12 @@ impl ResolverFactory {
7686
#[napi(constructor)]
7787
pub fn new(options: Option<NapiResolveOptions>) -> Self {
7888
init_tracing();
79-
let options = options.map_or_else(|| ResolveOptions::default(), Self::normalize_options);
89+
let options = options.map_or_else(ResolveOptions::default, Self::normalize_options);
8090
Self { resolver: Arc::new(Resolver::new(options)) }
8191
}
8292

8393
#[napi]
94+
#[allow(clippy::should_implement_trait)]
8495
pub fn default() -> Self {
8596
let default_options = ResolveOptions::default();
8697
Self { resolver: Arc::new(Resolver::new(default_options)) }

napi/src/options.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,9 @@ pub struct TsconfigOptions {
202202
pub references: Option<Either<String, Vec<String>>>,
203203
}
204204

205-
impl Into<oxc_resolver::Restriction> for Restriction {
206-
fn into(self) -> oxc_resolver::Restriction {
207-
match (self.path, self.regex) {
205+
impl From<Restriction> for oxc_resolver::Restriction {
206+
fn from(val: Restriction) -> Self {
207+
match (val.path, val.regex) {
208208
(None, None) => {
209209
panic!("Should specify path or regex")
210210
}
@@ -217,21 +217,21 @@ impl Into<oxc_resolver::Restriction> for Restriction {
217217
}
218218
}
219219

220-
impl Into<oxc_resolver::EnforceExtension> for EnforceExtension {
221-
fn into(self) -> oxc_resolver::EnforceExtension {
222-
match self {
220+
impl From<EnforceExtension> for oxc_resolver::EnforceExtension {
221+
fn from(val: EnforceExtension) -> Self {
222+
match val {
223223
EnforceExtension::Auto => oxc_resolver::EnforceExtension::Auto,
224224
EnforceExtension::Enabled => oxc_resolver::EnforceExtension::Enabled,
225225
EnforceExtension::Disabled => oxc_resolver::EnforceExtension::Disabled,
226226
}
227227
}
228228
}
229229

230-
impl Into<oxc_resolver::TsconfigOptions> for TsconfigOptions {
231-
fn into(self) -> oxc_resolver::TsconfigOptions {
230+
impl From<TsconfigOptions> for oxc_resolver::TsconfigOptions {
231+
fn from(val: TsconfigOptions) -> Self {
232232
oxc_resolver::TsconfigOptions {
233-
config_file: PathBuf::from(self.config_file),
234-
references: match self.references {
233+
config_file: PathBuf::from(val.config_file),
234+
references: match val.references {
235235
Some(Either::A(string)) if string.as_str() == "auto" => {
236236
oxc_resolver::TsconfigReferences::Auto
237237
}
@@ -250,9 +250,9 @@ impl Into<oxc_resolver::TsconfigOptions> for TsconfigOptions {
250250
type StrOrStrListType = Either<String, Vec<String>>;
251251
pub struct StrOrStrList(pub StrOrStrListType);
252252

253-
impl Into<Vec<String>> for StrOrStrList {
254-
fn into(self) -> Vec<String> {
255-
match self {
253+
impl From<StrOrStrList> for Vec<String> {
254+
fn from(val: StrOrStrList) -> Self {
255+
match val {
256256
StrOrStrList(Either::A(s)) => Vec::from([s]),
257257
StrOrStrList(Either::B(a)) => a,
258258
}

0 commit comments

Comments
 (0)