@@ -30,7 +30,6 @@ import formatFileCandidates from './errors/formatFileCandidates';
30
30
import { getPackageEntryPoint } from './PackageResolve' ;
31
31
import { resolvePackageTargetFromExports } from './PackageExportsResolve' ;
32
32
import resolveAsset from './resolveAsset' ;
33
- import invariant from 'invariant' ;
34
33
35
34
function resolve (
36
35
context : ResolutionContext ,
@@ -51,7 +50,11 @@ function resolve(
51
50
}
52
51
53
52
if ( isRelativeImport ( moduleName ) || path . isAbsolute ( moduleName ) ) {
54
- return resolveModulePath ( context , moduleName , platform ) ;
53
+ const result = resolvePackage ( context , moduleName , platform ) ;
54
+ if ( result . type === 'failed' ) {
55
+ throw new FailedToResolvePathError ( result . candidates ) ;
56
+ }
57
+ return result . resolution ;
55
58
}
56
59
57
60
const realModuleName = context . redirectModulePath ( moduleName ) ;
@@ -75,7 +78,11 @@ function resolve(
75
78
originModulePath . indexOf ( path . sep , fromModuleParentIdx ) ,
76
79
) ;
77
80
const absPath = path . join ( originModuleDir , realModuleName ) ;
78
- return resolveModulePath ( context , absPath , platform ) ;
81
+ const result = resolvePackage ( context , absPath , platform ) ;
82
+ if ( result . type === 'failed' ) {
83
+ throw new FailedToResolvePathError ( result . candidates ) ;
84
+ }
85
+ return result . resolution ;
79
86
}
80
87
81
88
if ( context . allowHaste && ! isDirectImport ) {
@@ -151,19 +158,26 @@ function resolveModulePath(
151
158
context : ResolutionContext ,
152
159
toModuleName : string ,
153
160
platform : string | null ,
154
- ) : Resolution {
161
+ ) : Result < Resolution , FileAndDirCandidates > {
155
162
const modulePath = path . isAbsolute ( toModuleName )
156
163
? resolveWindowsPath ( toModuleName )
157
164
: path . join ( path . dirname ( context . originModulePath ) , toModuleName ) ;
158
165
const redirectedPath = context . redirectModulePath ( modulePath ) ;
159
166
if ( redirectedPath === false ) {
160
- return { type : 'empty' } ;
167
+ return resolvedAs ( { type : 'empty' } ) ;
161
168
}
162
- const result = resolvePackage ( context , redirectedPath , platform ) ;
163
- if ( result . type === 'resolved' ) {
164
- return result . resolution ;
169
+
170
+ const dirPath = path . dirname ( redirectedPath ) ;
171
+ const fileName = path . basename ( redirectedPath ) ;
172
+ const fileResult = resolveFile ( context , dirPath , fileName , platform ) ;
173
+ if ( fileResult . type === 'resolved' ) {
174
+ return fileResult ;
165
175
}
166
- throw new FailedToResolvePathError ( result . candidates ) ;
176
+ const dirResult = resolvePackageEntryPoint ( context , redirectedPath , platform ) ;
177
+ if ( dirResult . type === 'resolved' ) {
178
+ return dirResult ;
179
+ }
180
+ return failedFor ( { file : fileResult . candidates , dir : dirResult . candidates } ) ;
167
181
}
168
182
169
183
/**
@@ -192,7 +206,7 @@ function resolveHasteName(
192
206
const packageDirPath = path . dirname ( packageJsonPath ) ;
193
207
const pathInModule = moduleName . substring ( packageName . length + 1 ) ;
194
208
const potentialModulePath = path . join ( packageDirPath , pathInModule ) ;
195
- const result = resolvePackage ( context , potentialModulePath , platform ) ;
209
+ const result = resolveModulePath ( context , potentialModulePath , platform ) ;
196
210
if ( result . type === 'resolved' ) {
197
211
return result ;
198
212
}
@@ -240,11 +254,6 @@ function resolvePackage(
240
254
modulePath : string ,
241
255
platform : string | null ,
242
256
) : Result < Resolution , FileAndDirCandidates > {
243
- invariant (
244
- path . isAbsolute ( modulePath ) ,
245
- 'resolvePackage expects an absolute module path' ,
246
- ) ;
247
-
248
257
if ( context . unstable_enablePackageExports ) {
249
258
const pkg = context . getPackageForModule ( modulePath ) ;
250
259
const exportsField = pkg ?. packageJson . exports ;
@@ -283,17 +292,7 @@ function resolvePackage(
283
292
}
284
293
}
285
294
286
- const dirPath = path . dirname ( modulePath ) ;
287
- const fileName = path . basename ( modulePath ) ;
288
- const fileResult = resolveFile ( context , dirPath , fileName , platform ) ;
289
- if ( fileResult . type === 'resolved' ) {
290
- return fileResult ;
291
- }
292
- const dirResult = resolvePackageEntryPoint ( context , modulePath , platform ) ;
293
- if ( dirResult . type === 'resolved' ) {
294
- return dirResult ;
295
- }
296
- return failedFor ( { file : fileResult . candidates , dir : dirResult . candidates } ) ;
295
+ return resolveModulePath ( context , modulePath , platform ) ;
297
296
}
298
297
299
298
/**
0 commit comments