packager: ResolutionRequest.js: sync _resolveFileOrDir

Reviewed By: davidaurelio

Differential Revision: D4754138

fbshipit-source-id: d19792a726220a673dead1c8c6cdf487e34a6808
This commit is contained in:
Jean Lauliac 2017-03-23 11:07:10 -07:00 committed by Facebook Github Bot
parent 25c82f2e8c
commit aec6307c4c
1 changed files with 96 additions and 101 deletions

View File

@ -302,120 +302,115 @@ class ResolutionRequest {
}); });
} }
_redirectRequire(fromModule: Module, modulePath: string) { _redirectRequire(fromModule: Module, modulePath: string): string | false {
return Promise.resolve(fromModule.getPackage()).then(p => { const pck = fromModule.getPackage();
if (p) { if (pck) {
return p.redirectRequire(modulePath); return pck.redirectRequire(modulePath);
} }
return modulePath; return modulePath;
});
} }
_resolveFileOrDir(fromModule: Module, toModuleName: string) { _resolveFileOrDir(fromModule: Module, toModuleName: string): Module {
const potentialModulePath = isAbsolutePath(toModuleName) ? const potentialModulePath = isAbsolutePath(toModuleName) ?
resolveWindowsPath(toModuleName) : resolveWindowsPath(toModuleName) :
path.join(path.dirname(fromModule.path), toModuleName); path.join(path.dirname(fromModule.path), toModuleName);
return this._redirectRequire(fromModule, potentialModulePath).then( const realModuleName = this._redirectRequire(fromModule, potentialModulePath);
realModuleName => { if (realModuleName === false) {
if (realModuleName === false) { return this._loadAsFile(
return this._loadAsFile( ResolutionRequest.emptyModule,
ResolutionRequest.emptyModule, fromModule,
fromModule, toModuleName,
toModuleName, );
); }
}
return tryResolveSync( return tryResolveSync(
() => this._loadAsFile(realModuleName, fromModule, toModuleName), () => this._loadAsFile(realModuleName, fromModule, toModuleName),
() => this._loadAsDir(realModuleName, fromModule, toModuleName) () => this._loadAsDir(realModuleName, fromModule, toModuleName)
);
}
); );
} }
_resolveNodeDependency(fromModule: Module, toModuleName: string) { _resolveNodeDependency(fromModule: Module, toModuleName: string): Promise<Module> {
if (isRelativeImport(toModuleName) || isAbsolutePath(toModuleName)) { return Promise.resolve().then(() => {
return this._resolveFileOrDir(fromModule, toModuleName); if (isRelativeImport(toModuleName) || isAbsolutePath(toModuleName)) {
} else { return this._resolveFileOrDir(fromModule, toModuleName);
return this._redirectRequire(fromModule, toModuleName).then( }
realModuleName => { const realModuleName = this._redirectRequire(fromModule, toModuleName);
// exclude // exclude
if (realModuleName === false) { if (realModuleName === false) {
return this._loadAsFile( return this._loadAsFile(
ResolutionRequest.emptyModule, ResolutionRequest.emptyModule,
fromModule, fromModule,
toModuleName, toModuleName,
); );
} }
if (isRelativeImport(realModuleName) || isAbsolutePath(realModuleName)) { if (isRelativeImport(realModuleName) || isAbsolutePath(realModuleName)) {
// derive absolute path /.../node_modules/fromModuleDir/realModuleName // derive absolute path /.../node_modules/fromModuleDir/realModuleName
const fromModuleParentIdx = fromModule.path.lastIndexOf('node_modules' + path.sep) + 13; const fromModuleParentIdx = fromModule.path.lastIndexOf('node_modules' + path.sep) + 13;
const fromModuleDir = fromModule.path.slice( const fromModuleDir = fromModule.path.slice(
0, 0,
fromModule.path.indexOf(path.sep, fromModuleParentIdx), fromModule.path.indexOf(path.sep, fromModuleParentIdx),
); );
const absPath = path.join(fromModuleDir, realModuleName); const absPath = path.join(fromModuleDir, realModuleName);
return this._resolveFileOrDir(fromModule, absPath); return this._resolveFileOrDir(fromModule, absPath);
} }
const searchQueue = []; const searchQueue = [];
for (let currDir = path.dirname(fromModule.path); for (let currDir = path.dirname(fromModule.path);
currDir !== '.' && currDir !== realPath.parse(fromModule.path).root; currDir !== '.' && currDir !== realPath.parse(fromModule.path).root;
currDir = path.dirname(currDir)) { currDir = path.dirname(currDir)) {
const searchPath = path.join(currDir, 'node_modules'); const searchPath = path.join(currDir, 'node_modules');
if (this._dirExists(searchPath)) { if (this._dirExists(searchPath)) {
searchQueue.push( searchQueue.push(
path.join(searchPath, realModuleName) path.join(searchPath, realModuleName)
); );
} }
} }
if (this._extraNodeModules) { if (this._extraNodeModules) {
const {_extraNodeModules} = this; const {_extraNodeModules} = this;
const bits = toModuleName.split(path.sep); const bits = toModuleName.split(path.sep);
const packageName = bits[0]; const packageName = bits[0];
if (_extraNodeModules[packageName]) { if (_extraNodeModules[packageName]) {
bits[0] = _extraNodeModules[packageName]; bits[0] = _extraNodeModules[packageName];
searchQueue.push(path.join.apply(path, bits)); searchQueue.push(path.join.apply(path, bits));
} }
} }
let p = Promise.reject(new UnableToResolveError(fromModule, toModuleName)); let p = Promise.reject(new UnableToResolveError(fromModule, toModuleName));
searchQueue.forEach(potentialModulePath => { searchQueue.forEach(potentialModulePath => {
p = this._tryResolve( p = this._tryResolve(
() => this._tryResolve( () => this._tryResolve(
() => p, () => p,
() => Promise.resolve().then( () => Promise.resolve().then(
() => this._loadAsFile(potentialModulePath, fromModule, toModuleName), () => this._loadAsFile(potentialModulePath, fromModule, toModuleName),
), ),
), ),
() => Promise.resolve().then( () => Promise.resolve().then(
() => this._loadAsDir(potentialModulePath, fromModule, toModuleName), () => this._loadAsDir(potentialModulePath, fromModule, toModuleName),
), ),
); );
}); });
return p.catch(error => { return p.catch(error => {
if (error.type !== 'UnableToResolveError') { if (error.type !== 'UnableToResolveError') {
throw error; throw error;
} }
const hint = searchQueue.length ? ' or in these directories:' : ''; const hint = searchQueue.length ? ' or in these directories:' : '';
throw new UnableToResolveError( throw new UnableToResolveError(
fromModule, fromModule,
toModuleName, toModuleName,
`Module does not exist in the module map${hint}\n` + `Module does not exist in the module map${hint}\n` +
searchQueue.map(searchPath => ` ${path.dirname(searchPath)}\n`).join(', ') + '\n' + searchQueue.map(searchPath => ` ${path.dirname(searchPath)}\n`).join(', ') + '\n' +
`This might be related to https://github.com/facebook/react-native/issues/4968\n` + `This might be related to https://github.com/facebook/react-native/issues/4968\n` +
`To resolve try the following:\n` + `To resolve try the following:\n` +
` 1. Clear watchman watches: \`watchman watch-del-all\`.\n` + ` 1. Clear watchman watches: \`watchman watch-del-all\`.\n` +
` 2. Delete the \`node_modules\` folder: \`rm -rf node_modules && npm install\`.\n` + ` 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`.' ' 3. Reset packager cache: `rm -fr $TMPDIR/react-*` or `npm start -- --reset-cache`.'
); );
}); });
}); });
}
} }
_loadAsFile(potentialModulePath: string, fromModule: Module, toModule: string): Module { _loadAsFile(potentialModulePath: string, fromModule: Module, toModule: string): Module {