Skip to content

Commit 6cb5b89

Browse files
authored
feat: support resolving path with extra query (#54)
1 parent d4265fb commit 6cb5b89

File tree

6 files changed

+39
-0
lines changed

6 files changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const add = (a, b) => a + b
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const equal = (a, b) => a === b
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const minus = (a, b) => a - b
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "export-query",
3+
"exports": {
4+
"./add": "./add.js?query1?query2",
5+
"./minus": "./minus.js?query?extra",
6+
"./equal": "./equal.js?query"
7+
}
8+
}

src/lib.rs

+23
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,29 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
936936
if let Some(path) = self.load_as_file_or_directory(cached_path, "", ctx)? {
937937
return Ok(Some(path));
938938
}
939+
940+
let mut path_str = cached_path.path().to_str();
941+
942+
// 3. If the RESOLVED_PATH contains `?``, it could be a path with query
943+
// so try to resolve it as a file or directory without the query,
944+
// but also `?` is a valid character in a path, so we should try from right to left.
945+
while let Some(s) = path_str {
946+
if let Some((before, _)) = s.rsplit_once('?') {
947+
if (self.load_as_file_or_directory(
948+
&self.cache.value(Path::new(before)),
949+
"",
950+
ctx,
951+
)?)
952+
.is_some()
953+
{
954+
return Ok(Some(cached_path.clone()));
955+
}
956+
path_str = Some(before);
957+
} else {
958+
break;
959+
}
960+
}
961+
939962
// 3. THROW "not found"
940963
Err(ResolveError::NotFound(specifier.to_string()))
941964
}

src/tests/exports_field.rs

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ fn test_simple() {
1212
let f2 = super::fixture().join("exports-field2");
1313
let f4 = super::fixture().join("exports-field-error");
1414
let f5 = super::fixture().join("imports-exports-wildcard");
15+
let f6 = super::fixture().join("export-query");
1516

1617
let resolver = Resolver::new(ResolveOptions {
1718
extensions: vec![".js".into()],
@@ -43,6 +44,10 @@ fn test_simple() {
4344
("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")),
4445
("should resolve with wildcard pattern #8", f5.clone(), "m/middle-4/f/nested", f5.join("node_modules/m/src/middle-4/f/f.js")),
4546
("should resolve with wildcard pattern #9", f5.clone(), "m/middle-5/f$/$", f5.join("node_modules/m/src/middle-5/f$/$.js")),
47+
("should resolve with query string #10", f6.clone(), "export-query/add", f6.join("add.js?query1?query2")),
48+
// Sadly we can not use real `minus.js?query` and `equal.js?query` file due to Windows: invalid path
49+
("should resolve with query string #10", f6.clone(), "export-query/minus", f6.join("minus.js?query?extra")),
50+
("should resolve with query string #10", f6.clone(), "export-query/equal", f6.join("equal.js?query")),
4651
];
4752

4853
// Not needed or snapshot:

0 commit comments

Comments
 (0)