Skip to content

Commit

Permalink
fix: browser field resolving relative to path to itself (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Dec 17, 2023
1 parent b3d8f7c commit c4432a5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"module-b": "module-c",
"module-c": "module-c",
"./toString": "./lib/toString.js",
"./lib/main.js": "./lib/main.js",
".": false
},
"innerBrowser1": {
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,12 @@ impl<Fs: FileSystem + Default> ResolverGeneric<Fs> {
let Some(new_specifier) = package_json.resolve_browser_field(path, specifier)? else {
return Ok(None);
};
// Finish when resolving to self `{"./a.js": "./a.js"}`
if let Some(new_specifier) = new_specifier.strip_prefix("./") {
if path.ends_with(Path::new(new_specifier)) {
return Ok(Some(cached_path.clone()));
}
}
if specifier.is_some_and(|s| s == new_specifier) {
return Ok(None);
}
Expand Down
1 change: 1 addition & 0 deletions src/tests/browser_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ fn replace_file() {
("should check only alias field properties", f.clone(), "./toString", f.join("lib/toString.js")),
// not part of enhanced-resolve
("recursion", f.clone(), "module-c", f.join("node_modules/module-c.js")),
("resolve self", f.clone(), "./lib/main.js", f.join("lib/main.js")),
];

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

0 comments on commit c4432a5

Please # to comment.