Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support resolving path with extra query #54

Merged
merged 1 commit into from
Mar 27, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const add = (a, b) => a + b
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const equal = (a, b) => a === b
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const minus = (a, b) => a - b
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "export-query",
"exports": {
"./add": "./add.js?query1?query2",
"./minus": "./minus.js?query?extra",
"./equal": "./equal.js?query"
}
}
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,29 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
if let Some(path) = self.load_as_file_or_directory(cached_path, "", ctx)? {
return Ok(Some(path));
}

let mut path_str = cached_path.path().to_str();

// 3. If the RESOLVED_PATH contains `?``, it could be a path with query
// so try to resolve it as a file or directory without the query,
// but also `?` is a valid character in a path, so we should try from right to left.
while let Some(s) = path_str {
if let Some((before, _)) = s.rsplit_once('?') {
if (self.load_as_file_or_directory(
&self.cache.value(Path::new(before)),
"",
ctx,
)?)
.is_some()
{
return Ok(Some(cached_path.clone()));
}
path_str = Some(before);
} else {
break;
}
}

// 3. THROW "not found"
Err(ResolveError::NotFound(specifier.to_string()))
}
Expand Down
5 changes: 5 additions & 0 deletions src/tests/exports_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn test_simple() {
let f2 = super::fixture().join("exports-field2");
let f4 = super::fixture().join("exports-field-error");
let f5 = super::fixture().join("imports-exports-wildcard");
let f6 = super::fixture().join("export-query");

let resolver = Resolver::new(ResolveOptions {
extensions: vec![".js".into()],
Expand Down Expand Up @@ -43,6 +44,10 @@ fn test_simple() {
("should resolve with wildcard pattern #7", f5.clone(), "m/middle-3/nested/f", f5.join("node_modules/m/src/middle-3/nested/f/nested/f.js")),
("should resolve with wildcard pattern #8", f5.clone(), "m/middle-4/f/nested", f5.join("node_modules/m/src/middle-4/f/f.js")),
("should resolve with wildcard pattern #9", f5.clone(), "m/middle-5/f$/$", f5.join("node_modules/m/src/middle-5/f$/$.js")),
("should resolve with query string #10", f6.clone(), "export-query/add", f6.join("add.js?query1?query2")),
// Sadly we can not use real `minus.js?query` and `equal.js?query` file due to Windows: invalid path
("should resolve with query string #10", f6.clone(), "export-query/minus", f6.join("minus.js?query?extra")),
("should resolve with query string #10", f6.clone(), "export-query/equal", f6.join("equal.js?query")),
];

// Not needed or snapshot:
Expand Down
Loading