这是indexloc提供的服务,不要输入任何密码
Skip to content
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
33 changes: 18 additions & 15 deletions napi/__test__/resolver.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -238,25 +238,28 @@ test('resolve pnpm package', (t) => {
const resolver = new ResolverFactory({
aliasFields: ['browser'],
});
t.deepEqual(resolver.sync(pnpmProjectPath, 'styled-components'), {
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',
),
},
);
});
1 change: 1 addition & 0 deletions napi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export interface ResolveResult {
error?: string;
/** "type" field in the package.json file */
moduleType?: string;
packageJsonPath?: string;
}

/**
Expand Down
15 changes: 13 additions & 2 deletions napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct ResolveResult {
pub error: Option<String>,
/// "type" field in the package.json file
pub module_type: Option<String>,
pub package_json_path: Option<String>,
}

fn resolve(resolver: &Resolver, path: &Path, request: &str) -> ResolveResult {
Expand All @@ -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()) },
}
}

Expand Down Expand Up @@ -76,11 +86,12 @@ impl ResolverFactory {
#[napi(constructor)]
pub fn new(options: Option<NapiResolveOptions>) -> 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)) }
Expand Down
26 changes: 13 additions & 13 deletions napi/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ pub struct TsconfigOptions {
pub references: Option<Either<String, Vec<String>>>,
}

impl Into<oxc_resolver::Restriction> for Restriction {
fn into(self) -> oxc_resolver::Restriction {
match (self.path, self.regex) {
impl From<Restriction> for oxc_resolver::Restriction {
fn from(val: Restriction) -> Self {
match (val.path, val.regex) {
(None, None) => {
panic!("Should specify path or regex")
}
Expand All @@ -217,21 +217,21 @@ impl Into<oxc_resolver::Restriction> for Restriction {
}
}

impl Into<oxc_resolver::EnforceExtension> for EnforceExtension {
fn into(self) -> oxc_resolver::EnforceExtension {
match self {
impl From<EnforceExtension> 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,
}
}
}

impl Into<oxc_resolver::TsconfigOptions> for TsconfigOptions {
fn into(self) -> oxc_resolver::TsconfigOptions {
impl From<TsconfigOptions> 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
}
Expand All @@ -250,9 +250,9 @@ impl Into<oxc_resolver::TsconfigOptions> for TsconfigOptions {
type StrOrStrListType = Either<String, Vec<String>>;
pub struct StrOrStrList(pub StrOrStrListType);

impl Into<Vec<String>> for StrOrStrList {
fn into(self) -> Vec<String> {
match self {
impl From<StrOrStrList> for Vec<String> {
fn from(val: StrOrStrList) -> Self {
match val {
StrOrStrList(Either::A(s)) => Vec::from([s]),
StrOrStrList(Either::B(a)) => a,
}
Expand Down
Loading