mirror of https://github.com/status-im/metro.git
metro-bundler: ModuleResolution: inline loadAsDirOrThrow
Reviewed By: cpojer, davidaurelio Differential Revision: D5407426 fbshipit-source-id: 0e45b671d6696745ea37b99071f4e5bc2a6b4106
This commit is contained in:
parent
bc75b94b7e
commit
4a9c7b9f30
|
@ -346,15 +346,27 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
|
||||||
): TModule {
|
): TModule {
|
||||||
const dirPath = path.dirname(potentialModulePath);
|
const dirPath = path.dirname(potentialModulePath);
|
||||||
const fileNameHint = path.basename(potentialModulePath);
|
const fileNameHint = path.basename(potentialModulePath);
|
||||||
const result = this._loadAsFile(dirPath, fileNameHint, platform);
|
const fileResult = this._loadAsFile(dirPath, fileNameHint, platform);
|
||||||
if (result.type === 'resolved') {
|
if (fileResult.type === 'resolved') {
|
||||||
return result.module;
|
return fileResult.module;
|
||||||
}
|
}
|
||||||
return this._loadAsDirOrThrow(
|
const dirResult = this._loadAsDir(potentialModulePath, platform);
|
||||||
potentialModulePath,
|
if (dirResult.type === 'resolved') {
|
||||||
|
return dirResult.module;
|
||||||
|
}
|
||||||
|
if (dirResult.candidates.type === 'package') {
|
||||||
|
throw new UnableToResolveError(
|
||||||
|
fromModule,
|
||||||
|
toModuleName,
|
||||||
|
`could not resolve \`${potentialModulePath}' as a folder: it ` +
|
||||||
|
'contained a package, but its "main" could not be resolved',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
invariant(dirResult.candidates.type === 'index', 'invalid candidate type');
|
||||||
|
throw new UnableToResolveError(
|
||||||
fromModule,
|
fromModule,
|
||||||
toModuleName,
|
toModuleName,
|
||||||
platform,
|
`could not resolve \`${potentialModulePath}' as a file nor as a folder`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -473,37 +485,6 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Same as `_loadAsDir`, but throws instead of returning candidates in case of
|
|
||||||
* failure. We want to migrate all the callsites to `_loadAsDir` eventually.
|
|
||||||
*/
|
|
||||||
_loadAsDirOrThrow(
|
|
||||||
potentialDirPath: string,
|
|
||||||
fromModule: TModule,
|
|
||||||
toModuleName: string,
|
|
||||||
platform: string | null,
|
|
||||||
): TModule {
|
|
||||||
const result = this._loadAsDir(potentialDirPath, platform);
|
|
||||||
if (result.type === 'resolved') {
|
|
||||||
return result.module;
|
|
||||||
}
|
|
||||||
if (result.candidates.type === 'package') {
|
|
||||||
throw new UnableToResolveError(
|
|
||||||
fromModule,
|
|
||||||
toModuleName,
|
|
||||||
`could not resolve \`${potentialDirPath}' as a folder: it contained ` +
|
|
||||||
'a package, but its "main" could not be resolved',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
invariant(result.candidates.type === 'index', 'invalid candidate type');
|
|
||||||
throw new UnableToResolveError(
|
|
||||||
fromModule,
|
|
||||||
toModuleName,
|
|
||||||
`could not resolve \`${potentialDirPath}' as a folder: it did not ` +
|
|
||||||
'contain a package, nor an index file',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Try to resolve a potential path as if it was a directory-based module.
|
* Try to resolve a potential path as if it was a directory-based module.
|
||||||
* Either this is a directory that contains a package, or that the directory
|
* Either this is a directory that contains a package, or that the directory
|
||||||
|
|
Loading…
Reference in New Issue