fix(turbopack): allow `#/` prefixed subpath import specifiers (#94461)
## What?
Fixes Turbopack failing to resolve imports using the common `"#/*":
"./src/*"` pattern in `package.json` `imports` field.
## Why?
The resolver had an early-rejection check that incorrectly blocked all
specifiers starting with `#/`:
```rust
if specifier == "#" || specifier.starts_with("#/") || specifier.ends_with(/) {
// emit error, return unresolvable
}
```
Node.js and webpack (enhanced-resolve) both support the widely-used
TypeScript pattern:
```json
"imports": {
"#/*": "./src/*"
}
```
When a user writes `import x from '#/greeting'`, the specifier
`#/greeting` starts with `#/`, so Turbopack was erroneously rejecting it
before attempting the `imports` field lookup. Only bare `#` and
directory-trailing `/` specifiers should be rejected early —
`#/something` is perfectly valid and should reach the `imports` field
resolver.
Since Turbopack is a drop-in webpack replacement in Next.js, it should
support the same resolution patterns. The `AliasMap` already handles
wildcard pattern matching correctly once the early rejection is bypassed
— removing the `starts_with("#/")` guard is sufficient.
## How?
- Remove `specifier.starts_with("#/")` from the early rejection
condition in `resolve_package_internal_with_imports_field`
- Add a test fixture (`subpath-imports-slash`) that validates `#/*`
wildcard imports resolve correctly
## Testing
New test case in
`turbopack/crates/turbopack-tests/tests/execution/turbopack/resolving/subpath-imports-slash`:
```json
{ "imports": { "#/*": "./src/*" } }
```
```js
import greeting from '#/greeting.js'
it('should resolve #/* subpath imports', () => {
expect(greeting).toBe('hello')
})
```
Closes #94290
Showing
Please
register
or
sign in
to comment