metro-bundler: ModuleResolution: remove loadAsFileOrThrow

Reviewed By: cpojer

Differential Revision: D5406612

fbshipit-source-id: bd20e1c9aa23b6988ab02c8c0459335874d7d346
This commit is contained in:
Jean Lauliac 2017-07-13 06:15:34 -07:00 committed by Facebook Github Bot
parent 392a129b56
commit bc75b94b7e
1 changed files with 26 additions and 64 deletions

View File

@ -178,21 +178,11 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
package_.root, package_.root,
path.relative(packageName, realModuleName), path.relative(packageName, realModuleName),
); );
return tryResolveSync( return this._loadAsFileOrDirOrThrow(
() => potentialModulePath,
this._loadAsFileOrThrow( fromModule,
potentialModulePath, toModuleName,
fromModule, platform,
toModuleName,
platform,
),
() =>
this._loadAsDirOrThrow(
potentialModulePath,
fromModule,
toModuleName,
platform,
),
); );
} }
@ -227,22 +217,11 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
if (realModuleName === false) { if (realModuleName === false) {
return this._getEmptyModule(fromModule, toModuleName); return this._getEmptyModule(fromModule, toModuleName);
} }
return this._loadAsFileOrDirOrThrow(
return tryResolveSync( potentialModulePath,
() => fromModule,
this._loadAsFileOrThrow( toModuleName,
realModuleName, platform,
fromModule,
toModuleName,
platform,
),
() =>
this._loadAsDirOrThrow(
realModuleName,
fromModule,
toModuleName,
platform,
),
); );
} }
@ -338,21 +317,11 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
platform: string | null, platform: string | null,
): ?TModule { ): ?TModule {
try { try {
return tryResolveSync( return this._loadAsFileOrDirOrThrow(
() => searchPath,
this._loadAsFileOrThrow( fromModule,
searchPath, toModuleName,
fromModule, platform,
toModuleName,
platform,
),
() =>
this._loadAsDirOrThrow(
searchPath,
fromModule,
toModuleName,
platform,
),
); );
} catch (error) { } catch (error) {
if (error.type !== 'UnableToResolveError') { if (error.type !== 'UnableToResolveError') {
@ -369,31 +338,24 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
* This function is meant to be a temporary proxy for _loadAsFile until * This function is meant to be a temporary proxy for _loadAsFile until
* the callsites switch to that tracking structure. * the callsites switch to that tracking structure.
*/ */
_loadAsFileOrThrow( _loadAsFileOrDirOrThrow(
basePath: string, potentialModulePath: string,
fromModule: TModule, fromModule: TModule,
toModule: string, toModuleName: string,
platform: string | null, platform: string | null,
): TModule { ): TModule {
const dirPath = path.dirname(basePath); const dirPath = path.dirname(potentialModulePath);
const fileNameHint = path.basename(basePath); const fileNameHint = path.basename(potentialModulePath);
const result = this._loadAsFile(dirPath, fileNameHint, platform); const result = this._loadAsFile(dirPath, fileNameHint, platform);
if (result.type === 'resolved') { if (result.type === 'resolved') {
return result.module; return result.module;
} }
if (result.candidates.type === 'asset') { return this._loadAsDirOrThrow(
const msg = potentialModulePath,
`Directory \`${dirPath}' doesn't contain asset ` + fromModule,
`\`${result.candidates.name}'`; toModuleName,
throw new UnableToResolveError(fromModule, toModule, msg); platform,
} );
invariant(result.candidates.type === 'sources', 'invalid candidate type');
const msg =
`Could not resolve the base path \`${basePath}' into a module. The ` +
`folder \`${dirPath}' was searched for one of these files: ` +
result.candidates.fileNames.map(filePath => `\`${filePath}'`).join(', ') +
'.';
throw new UnableToResolveError(fromModule, toModule, msg);
} }
_loadAsFile( _loadAsFile(