mirror of https://github.com/status-im/metro.git
ModuleResolution: switch _resolveHasteDependency to non-throwing resolveFileOrDir()
Summary: This changeset alters the Haste resolution logic to be in sync with the new refactored pieces (resolveFile, etc.) Next steps involve extracting this function outside of the class so as to reduce the surface API and promote composability. Reviewed By: cpojer Differential Revision: D6645302 fbshipit-source-id: 6ee00fb52025cc0d332e57f837b46e8323faf6f4
This commit is contained in:
parent
af1e6e3298
commit
7cab9a02c9
|
@ -126,23 +126,22 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
|
||||||
}
|
}
|
||||||
|
|
||||||
_resolveHasteDependency(
|
_resolveHasteDependency(
|
||||||
fromModule: TModule,
|
moduleName: string,
|
||||||
toModuleName: string,
|
|
||||||
platform: string | null,
|
platform: string | null,
|
||||||
): TModule {
|
): Result<FileResolution, void> {
|
||||||
const modulePath = this._options.moduleMap.getModule(
|
const modulePath = this._options.moduleMap.getModule(
|
||||||
toModuleName,
|
moduleName,
|
||||||
platform,
|
platform,
|
||||||
/* supportsNativePlatform */ true,
|
/* supportsNativePlatform */ true,
|
||||||
);
|
);
|
||||||
if (modulePath != null) {
|
if (modulePath != null) {
|
||||||
const module = this._options.moduleCache.getModule(modulePath);
|
return {
|
||||||
/* temporary until we strengthen the typing */
|
type: 'resolved',
|
||||||
invariant(module.type === 'Module', 'expected Module type');
|
resolution: {type: 'sourceFile', filePath: modulePath},
|
||||||
return module;
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let packageName = toModuleName;
|
let packageName = moduleName;
|
||||||
let packageJsonPath;
|
let packageJsonPath;
|
||||||
while (packageName && packageName !== '.') {
|
while (packageName && packageName !== '.') {
|
||||||
packageJsonPath = this._options.moduleMap.getPackage(
|
packageJsonPath = this._options.moduleMap.getPackage(
|
||||||
|
@ -156,32 +155,28 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
|
||||||
packageName = path.dirname(packageName);
|
packageName = path.dirname(packageName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (packageJsonPath != null) {
|
if (packageJsonPath == null) {
|
||||||
|
return failedFor();
|
||||||
|
}
|
||||||
// FIXME: `moduleCache.getModule` should return the directory path, not
|
// FIXME: `moduleCache.getModule` should return the directory path, not
|
||||||
// the `package.json` file path, because the directory is what's really
|
// the `package.json` file path, because the directory is what's really
|
||||||
// representing the package.
|
// representing the package.
|
||||||
const packageDirPath = path.dirname(packageJsonPath);
|
const packageDirPath = path.dirname(packageJsonPath);
|
||||||
// FIXME: if we're trying to require the package main module (ie.
|
|
||||||
// packageName === toModuleName), don't do as if it could be a
|
|
||||||
// "FileOrDir", call directly the `resolvePackage` function! Otherwise we
|
|
||||||
// might actually be grabbing a completely unrelated file.
|
|
||||||
const potentialModulePath = path.join(
|
|
||||||
packageDirPath,
|
|
||||||
path.relative(packageName, toModuleName),
|
|
||||||
);
|
|
||||||
return this._loadAsFileOrDirOrThrow(
|
|
||||||
potentialModulePath,
|
|
||||||
fromModule,
|
|
||||||
toModuleName,
|
|
||||||
platform,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new UnableToResolveError(
|
const pathInModule = moduleName.substring(packageName.length + 1);
|
||||||
fromModule.path,
|
const potentialModulePath = path.join(packageDirPath, pathInModule);
|
||||||
toModuleName,
|
|
||||||
'Unable to resolve dependency',
|
const context = {
|
||||||
);
|
...this._options,
|
||||||
|
getPackageMainPath: this._getPackageMainPath,
|
||||||
|
};
|
||||||
|
const result = resolveFileOrDir(context, potentialModulePath, platform);
|
||||||
|
if (result.type === 'resolved') {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
const {candidates} = result;
|
||||||
|
const opts = {moduleName, packageName, pathInModule, candidates};
|
||||||
|
throw new MissingFileInHastePackageError(opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
_redirectRequire(fromModule: TModule, modulePath: string): string | false {
|
_redirectRequire(fromModule: TModule, modulePath: string): string | false {
|
||||||
|
@ -246,16 +241,12 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
|
||||||
// At that point we only have module names that
|
// At that point we only have module names that
|
||||||
// aren't relative paths nor absolute paths.
|
// aren't relative paths nor absolute paths.
|
||||||
if (allowHaste) {
|
if (allowHaste) {
|
||||||
try {
|
const result = this._resolveHasteDependency(
|
||||||
return this._resolveHasteDependency(
|
|
||||||
fromModule,
|
|
||||||
normalizePath(realModuleName),
|
normalizePath(realModuleName),
|
||||||
platform,
|
platform,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
if (result.type === 'resolved') {
|
||||||
if (!(error instanceof UnableToResolveError)) {
|
return this._getFileResolvedModule(result.resolution);
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -381,6 +372,30 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class MissingFileInHastePackageError extends Error {
|
||||||
|
candidates: FileAndDirCandidates;
|
||||||
|
moduleName: string;
|
||||||
|
packageName: string;
|
||||||
|
pathInModule: string;
|
||||||
|
|
||||||
|
constructor(opts: {|
|
||||||
|
+candidates: FileAndDirCandidates,
|
||||||
|
+moduleName: string,
|
||||||
|
+packageName: string,
|
||||||
|
+pathInModule: string,
|
||||||
|
|}) {
|
||||||
|
super(
|
||||||
|
`While resolving module \`${opts.moduleName}\`, ` +
|
||||||
|
`the Haste package \`${opts.packageName}\` was found. However the ` +
|
||||||
|
`module \`${opts.pathInModule}\` could not be found within ` +
|
||||||
|
`the package. Indeed, none of these files exist:\n\n` +
|
||||||
|
` * \`${formatFileCandidates(opts.candidates.file)}\`\n` +
|
||||||
|
` * \`${formatFileCandidates(opts.candidates.dir)}\``,
|
||||||
|
);
|
||||||
|
Object.assign(this, opts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type FileOrDirContext = FileContext & {
|
type FileOrDirContext = FileContext & {
|
||||||
/**
|
/**
|
||||||
* This should return the path of the "main" module of the specified
|
* This should return the path of the "main" module of the specified
|
||||||
|
@ -644,7 +659,7 @@ function resolveSourceFileForExt(
|
||||||
if (context.doesFileExist(filePath)) {
|
if (context.doesFileExist(filePath)) {
|
||||||
return filePath;
|
return filePath;
|
||||||
}
|
}
|
||||||
context.candidateExts.push(filePath);
|
context.candidateExts.push(extension);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue