Skip to content

Commit 704bceb

Browse files
committed
implement more request key rewriting
1 parent 70699cf commit 704bceb

File tree

2 files changed

+35
-27
lines changed
  • turbopack/crates
    • turbopack-core/src/resolve
    • turbopack-tests/tests/execution/turbopack/resolving/exports-field-pattern/input

2 files changed

+35
-27
lines changed

turbopack/crates/turbopack-core/src/resolve/mod.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2965,14 +2965,14 @@ async fn handle_exports_imports_field(
29652965
if let Some(result_path) = result_path.with_normalized_path() {
29662966
let request = Request::parse(Pattern::Concatenation(vec![
29672967
Pattern::Constant(rcstr!("./")),
2968-
result_path,
2968+
result_path.clone(),
29692969
]))
2970-
.to_resolved()
2970+
.resolve()
29712971
.await?;
29722972

29732973
let resolve_result = Box::pin(resolve_internal_inline(
29742974
package_path.clone(),
2975-
*request,
2975+
request,
29762976
options,
29772977
))
29782978
.await?;
@@ -2982,14 +2982,24 @@ async fn handle_exports_imports_field(
29822982
} else {
29832983
match map_key {
29842984
AliasKey::Exact => resolve_result.with_request(map_prefix.clone().into()),
2985-
AliasKey::Wildcard { suffix } => {
2986-
// Because of the assertion in AliasMapLookupIterator, `req` is of the form:
2985+
AliasKey::Wildcard { .. } => {
2986+
// - `req` is the user's request (key of the export map)
2987+
// - `result_path` is the final request (value of the export map), so
2988+
// effectively `'{foo}*{bar}'`
2989+
2990+
// Because of the assertion in AliasMapLookupIterator, `req` is of the
2991+
// form:
29872992
// - "prefix...<dynamic>" or
29882993
// - "prefix...<dynamic>...suffix"
2989-
bail!(
2990-
"unimplemented pattern {} into wildcard exports field \
2991-
'{map_prefix}*{suffix}'",
2992-
req.describe_as_string()
2994+
2995+
let mut old_request_key = result_path;
2996+
// Remove the Pattern::Constant(rcstr!("./")), from above again
2997+
old_request_key.push_front(rcstr!("./").into());
2998+
let new_request_key = req.clone();
2999+
3000+
resolve_result.with_replaced_request_key_pattern(
3001+
Pattern::new(old_request_key),
3002+
Pattern::new(new_request_key),
29933003
)
29943004
}
29953005
}

turbopack/crates/turbopack-tests/tests/execution/turbopack/resolving/exports-field-pattern/input/index.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ function requireRoot(s) {
44
function requireExact(s) {
55
return require(`pkg/exact-${s}`)
66
}
7-
// TODO unimplemented pattern './wildcard-' <dynamic> into wildcard exports field './wildcard-*'
8-
// function requireWildcardSuffix(s) {
9-
// return require(`pkg/wildcard-suffix-${s}`)
10-
// }
11-
// TODO unimplemented pattern './wildcard-' <dynamic> into wildcard exports field './wildcard-*'
12-
// function requireWildcard(s) {
13-
// return require(`pkg/wildcard-${s}`)
14-
// }
7+
function requireWildcardSuffix(s) {
8+
return require(`pkg/wildcard-suffix-${s}`)
9+
}
10+
function requireWildcard(s) {
11+
return require(`pkg/wildcard-${s}`)
12+
}
1513
function requireExactAConstantSuffix(s) {
1614
return require(`pkg/${s}exact-a`)
1715
}
@@ -23,17 +21,17 @@ it('should correctly handle dynamic requests into exports field (exact)', () =>
2321
expect(requireExact('c').default).toBe('c')
2422
})
2523

26-
// it('should correctly handle dynamic requests into exports field (wildcard with suffix)', () => {
27-
// expect(requireWildcardSuffix('a').default).toBe('a')
28-
// expect(requireWildcardSuffix('b').default).toBe('b')
29-
// expect(requireWildcardSuffix('c').default).toBe('c')
30-
// })
24+
it('should correctly handle dynamic requests into exports field (wildcard with suffix)', () => {
25+
expect(requireWildcardSuffix('a').default).toBe('a')
26+
expect(requireWildcardSuffix('b').default).toBe('b')
27+
expect(requireWildcardSuffix('c').default).toBe('c')
28+
})
3129

32-
// it('should correctly handle dynamic requests into exports field (wildcard)', () => {
33-
// expect(requireWildcard('a').default).toBe('a')
34-
// expect(requireWildcard('b').default).toBe('b')
35-
// expect(requireWildcard('c').default).toBe('c')
36-
// })
30+
it('should correctly handle dynamic requests into exports field (wildcard)', () => {
31+
expect(requireWildcard('a').default).toBe('a')
32+
expect(requireWildcard('b').default).toBe('b')
33+
expect(requireWildcard('c').default).toBe('c')
34+
})
3735

3836
it('should correctly handle dynamic requests into exports field (empty dynamic prefix)', () => {
3937
// TODO it currently only returns a single entry

0 commit comments

Comments
 (0)