Skip to content

Commit ff4799e

Browse files
authored
fix: prefer file over package dir in node_modules (#116)
1 parent b782e89 commit ff4799e

File tree

7 files changed

+25
-2
lines changed

7 files changed

+25
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!node_modules

fixtures/prefer-file-over-pkg/node_modules/bar.js

Whitespace-only changes.

fixtures/prefer-file-over-pkg/node_modules/bar/index.js

Whitespace-only changes.

fixtures/prefer-file-over-pkg/node_modules/bar/package.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "prefer-file-over-pkg"
3+
}

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,13 @@ impl<C: Cache<Cp = FsCachedPath>> ResolverGeneric<C> {
781781
// may have a @scope/ prefix and the subpath begins with a slash (`/`).
782782
if !package_name.is_empty() {
783783
let cached_path = cached_path.normalize_with(package_name, self.cache.as_ref());
784+
// file is preferred over directory when specifier doesn't contains a slash which indicates a dir
785+
// node_modules/bar.js vs node_modules/bar/index.js
786+
if !specifier.contains('/') {
787+
if let Some(path) = self.load_as_file(&cached_path, ctx)? {
788+
return Ok(Some(path));
789+
}
790+
}
784791
// Try foo/node_modules/package_name
785792
if self.cache.is_dir(&cached_path, ctx) {
786793
// a. LOAD_PACKAGE_EXPORTS(X, DIR)

src/tests/resolve.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,20 @@ fn resolve_to_context() {
113113
#[test]
114114
fn resolve_hash_as_module() {
115115
let f = super::fixture();
116-
let resolver = Resolver::new(ResolveOptions::default());
116+
let resolver = Resolver::default();
117117
let resolution = resolver.resolve(f, "#a");
118118
assert_eq!(resolution, Err(ResolveError::NotFound("#a".into())));
119119
}
120120

121+
#[test]
122+
fn prefer_file_over_pkg() {
123+
let f = super::fixture_root();
124+
let fixture = f.join("prefer-file-over-pkg");
125+
let resolver = Resolver::default();
126+
let resolved_path = resolver.resolve(fixture.clone(), "bar").map(|r| r.full_path());
127+
assert_eq!(resolved_path, Ok(fixture.join("node_modules/bar.js")));
128+
}
129+
121130
#[cfg(windows)]
122131
#[test]
123132
fn resolve_normalized_on_windows() {
@@ -127,7 +136,7 @@ fn resolve_normalized_on_windows() {
127136
let absolute = f.join("./foo/index.js").normalize();
128137
let absolute_str = absolute.to_string_lossy();
129138
let normalized_absolute = absolute_str.replace('\\', "/");
130-
let resolver = Resolver::new(ResolveOptions::default());
139+
let resolver = Resolver::default();
131140

132141
let resolution = resolver.resolve(&f, &normalized_absolute).map(|r| r.full_path());
133142
assert_eq!(

0 commit comments

Comments
 (0)