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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,15 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
return Ok(self.cache.is_dir(&cached_path, ctx).then(|| cached_path.clone()));
}

// Perf: try LOAD_AS_DIRECTORY first. No modern package manager creates `node_modules/X.js`.
// Only load the file if it is targeting a `X/sub/dir`.
if specifier != package_name
&& !specifier.ends_with('/')
&& let Some(path) = self.load_as_file(&cached_path, tsconfig, ctx)?
{
return Ok(Some(path));
}
// Otherwise just load the directory.
// No modern package manager creates `node_modules/X.js`.
if self.cache.is_dir(&cached_path, ctx) {
if let Some(path) =
self.load_browser_field_or_alias(&cached_path, tsconfig, ctx)?
Expand All @@ -942,6 +950,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
if let Some(path) = self.load_as_directory(&cached_path, tsconfig, ctx)? {
return Ok(Some(path));
}
// Still need to try to load the file in case there are path aliases.
} else if let Some(path) = self.load_as_file(&cached_path, tsconfig, ctx)? {
return Ok(Some(path));
}
Expand Down
6 changes: 5 additions & 1 deletion src/tests/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ fn resolve() {
("handle fragment edge case (no fragment)", f.clone(), "./no#fragment/#/#", f.join("no#fragment/#/#.js")),
("handle fragment edge case (fragment)", f.clone(), "./no#fragment/#/", f.join("no.js#fragment/#/")),
("handle fragment escaping", f.clone(), "./no\0#fragment/\0#/\0##fragment", f.join("no#fragment/#/#.js#fragment")),

// Test `node_modules/X/foo/` and `node_modules/X/foo.js` precedence.
("file and dir precedence 1", f.clone(), "dir-and-file/foo", f.join("node_modules/dir-and-file/foo.js")),
("file and dir precedence 2", f.clone(), "@scope/dir-and-file/foo", f.join("node_modules/@scope/dir-and-file/foo.js")),
("file and dir precedence 1", f.clone(), "dir-and-file/foo/", f.join("node_modules/dir-and-file/foo/index.js")),
("file and dir precedence 2", f.clone(), "@scope/dir-and-file/foo/", f.join("node_modules/@scope/dir-and-file/foo/index.js")),
];

for (comment, path, request, expected) in pass {
Expand Down
Loading