packager: ResolutionRequest.js: _loadAsDir and _loadAsFile sync

Reviewed By: davidaurelio

Differential Revision: D4754090

fbshipit-source-id: 84ad1d988bf097d3094d90f3738ce64cc523879c
This commit is contained in:
Jean Lauliac 2017-03-23 11:07:08 -07:00 коммит произвёл Facebook Github Bot
Родитель 10918680eb
Коммит f06384b1b7
1 изменённых файлов: 91 добавлений и 75 удалений

Просмотреть файл

@ -51,6 +51,22 @@ type Options = {
preferNativePlatform: boolean, preferNativePlatform: boolean,
}; };
/**
* It may not be a great pattern to leverage exception just for "trying" things
* out, notably for performance. We should consider replacing these functions
* to be nullable-returning, or being better stucture to the algorithm.
*/
function tryResolveSync<T>(action: () => T, secondaryAction: () => T): T {
try {
return action();
} catch (error) {
if (error.type !== 'UnableToResolveError') {
throw error;
}
return secondaryAction();
}
}
class ResolutionRequest { class ResolutionRequest {
_dirExists: DirExistsFn; _dirExists: DirExistsFn;
_entryPath: string; _entryPath: string;
@ -268,7 +284,7 @@ class ResolutionRequest {
package_.root, package_.root,
path.relative(packageName, realModuleName) path.relative(packageName, realModuleName)
); );
return this._tryResolve( return tryResolveSync(
() => this._loadAsFile( () => this._loadAsFile(
potentialModulePath, potentialModulePath,
fromModule, fromModule,
@ -310,7 +326,7 @@ class ResolutionRequest {
); );
} }
return this._tryResolve( return tryResolveSync(
() => this._loadAsFile(realModuleName, fromModule, toModuleName), () => this._loadAsFile(realModuleName, fromModule, toModuleName),
() => this._loadAsDir(realModuleName, fromModule, toModuleName) () => this._loadAsDir(realModuleName, fromModule, toModuleName)
); );
@ -371,9 +387,13 @@ class ResolutionRequest {
p = this._tryResolve( p = this._tryResolve(
() => this._tryResolve( () => this._tryResolve(
() => p, () => p,
() => Promise.resolve().then(
() => this._loadAsFile(potentialModulePath, fromModule, toModuleName), () => this._loadAsFile(potentialModulePath, fromModule, toModuleName),
), ),
() => this._loadAsDir(potentialModulePath, fromModule, toModuleName) ),
() => Promise.resolve().then(
() => this._loadAsDir(potentialModulePath, fromModule, toModuleName),
),
); );
}); });
@ -398,8 +418,7 @@ class ResolutionRequest {
} }
} }
_loadAsFile(potentialModulePath: string, fromModule: Module, toModule: string): Promise<Module> { _loadAsFile(potentialModulePath: string, fromModule: Module, toModule: string): Module {
return Promise.resolve().then(() => {
if (this._helpers.isAssetFile(potentialModulePath)) { if (this._helpers.isAssetFile(potentialModulePath)) {
let dirname = path.dirname(potentialModulePath); let dirname = path.dirname(potentialModulePath);
if (!this._dirExists(dirname)) { if (!this._dirExists(dirname)) {
@ -455,11 +474,9 @@ class ResolutionRequest {
} }
return this._moduleCache.getModule(file); return this._moduleCache.getModule(file);
});
} }
_loadAsDir(potentialDirPath: string, fromModule: Module, toModule: string) { _loadAsDir(potentialDirPath: string, fromModule: Module, toModule: string): Module {
return Promise.resolve().then(() => {
if (!this._dirExists(potentialDirPath)) { if (!this._dirExists(potentialDirPath)) {
throw new UnableToResolveError( throw new UnableToResolveError(
fromModule, fromModule,
@ -471,7 +488,7 @@ class ResolutionRequest {
const packageJsonPath = path.join(potentialDirPath, 'package.json'); const packageJsonPath = path.join(potentialDirPath, 'package.json');
if (this._hasteFS.exists(packageJsonPath)) { if (this._hasteFS.exists(packageJsonPath)) {
const main = this._moduleCache.getPackage(packageJsonPath).getMain(); const main = this._moduleCache.getPackage(packageJsonPath).getMain();
return this._tryResolve( return tryResolveSync(
() => this._loadAsFile(main, fromModule, toModule), () => this._loadAsFile(main, fromModule, toModule),
() => this._loadAsDir(main, fromModule, toModule), () => this._loadAsDir(main, fromModule, toModule),
); );
@ -482,7 +499,6 @@ class ResolutionRequest {
fromModule, fromModule,
toModule, toModule,
); );
});
} }
_resetResolutionCache() { _resetResolutionCache() {