packager: ResolutionRequest.js: sync _resolveNodeDependency()

Summary: Moar synchronicity.

Reviewed By: davidaurelio

Differential Revision: D4756495

fbshipit-source-id: 4e0758ba8b55bd25a24d79dcc8ac4ace101e2ae8
This commit is contained in:
Jean Lauliac 2017-03-27 09:43:11 -07:00 committed by Facebook Github Bot
parent 4d9826eb64
commit 62c07e39dc
1 changed files with 79 additions and 75 deletions

View File

@ -132,11 +132,12 @@ class ResolutionRequest {
&& !(isRelativeImport(toModuleName) || isAbsolutePath(toModuleName))) {
return this._tryResolve(
() => Promise.resolve().then(() => this._resolveHasteDependency(fromModule, toModuleName)),
() => this._resolveNodeDependency(fromModule, toModuleName)
() => Promise.resolve().then(() => this._resolveNodeDependency(fromModule, toModuleName))
).then(cacheResult);
}
return this._resolveNodeDependency(fromModule, toModuleName)
return Promise.resolve()
.then(() => this._resolveNodeDependency(fromModule, toModuleName))
.then(cacheResult);
}
@ -329,8 +330,7 @@ class ResolutionRequest {
);
}
_resolveNodeDependency(fromModule: Module, toModuleName: string): Promise<Module> {
return Promise.resolve().then(() => {
_resolveNodeDependency(fromModule: Module, toModuleName: string): Module {
if (isRelativeImport(toModuleName) || isAbsolutePath(toModuleName)) {
return this._resolveFileOrDir(fromModule, toModuleName);
}
@ -377,25 +377,13 @@ class ResolutionRequest {
}
}
let p = Promise.reject(new UnableToResolveError(fromModule, toModuleName));
searchQueue.forEach(potentialModulePath => {
p = this._tryResolve(
() => this._tryResolve(
() => p,
() => Promise.resolve().then(
() => this._loadAsFile(potentialModulePath, fromModule, toModuleName),
),
),
() => Promise.resolve().then(
() => this._loadAsDir(potentialModulePath, fromModule, toModuleName),
),
);
});
return p.catch(error => {
if (error.type !== 'UnableToResolveError') {
throw error;
for (let i = 0; i < searchQueue.length; ++i) {
const resolvedModule = this._tryResolveNodeDep(searchQueue[i], fromModule, toModuleName);
if (resolvedModule != null) {
return resolvedModule;
}
}
const hint = searchQueue.length ? ' or in these directories:' : '';
throw new UnableToResolveError(
fromModule,
@ -408,8 +396,24 @@ class ResolutionRequest {
` 2. Delete the \`node_modules\` folder: \`rm -rf node_modules && npm install\`.\n` +
' 3. Reset packager cache: `rm -fr $TMPDIR/react-*` or `npm start -- --reset-cache`.'
);
});
});
}
/**
* This is written as a separate function because "try..catch" blocks cause
* the entire surrounding function to be deoptimized.
*/
_tryResolveNodeDep(searchPath: string, fromModule: Module, toModuleName: string): ?Module {
try {
return tryResolveSync(
() => this._loadAsFile(searchPath, fromModule, toModuleName),
() => this._loadAsDir(searchPath, fromModule, toModuleName),
);
} catch (error) {
if (error.type !== 'UnableToResolveError') {
throw error;
}
return null;
}
}
_loadAsFile(potentialModulePath: string, fromModule: Module, toModule: string): Module {