packager: ResolutionRequest: simplify asset resolution

Summary: I think we don't really need to check the directory beforehand, the function to find asset will just return an empty array. As for the error message, I tried adding a require of an asset file somewhere, but due to the way the ResolutionRequest algo work, it generates a final error message that unrealted ("Directory ... does not exist", instead of the "asset does not exist"). I plan to revamp the way errors are handled such as the error message clearly identifies what file paths have been inspected.

Reviewed By: davidaurelio

Differential Revision: D5028118

fbshipit-source-id: 496472001c0a3d4192bfef4a0c8a0dc8a9a0fa82
This commit is contained in:
Jean Lauliac 2017-05-10 04:34:10 -07:00 committed by Facebook Github Bot
parent 99661854de
commit 353bfba978
1 changed files with 25 additions and 24 deletions

View File

@ -500,30 +500,7 @@ class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
_loadAsFile(potentialModulePath: string, fromModule: TModule, toModule: string): TModule {
if (this._options.helpers.isAssetFile(potentialModulePath)) {
const dirname = path.dirname(potentialModulePath);
if (!this._options.dirExists(dirname)) {
throw new UnableToResolveError(
fromModule,
toModule,
`Directory ${dirname} doesn't exist`,
);
}
const {name, type} = getAssetDataFromName(potentialModulePath, this._options.platforms);
let pattern = '^' + name + '(@[\\d\\.]+x)?';
if (this._options.platform != null) {
pattern += '(\\.' + this._options.platform + ')?';
}
pattern += '\\.' + type + '$';
const assetFiles = this._options.matchFiles(dirname, new RegExp(pattern));
// We arbitrarly grab the lowest, because scale selection will happen
// somewhere else. Always the lowest so that it's stable between builds.
const assetFile = getArrayLowestItem(assetFiles);
if (assetFile) {
return this._options.moduleCache.getAssetModule(assetFile);
}
return this._loadAsAssetFile(potentialModulePath, fromModule, toModule);
}
let file;
@ -567,6 +544,30 @@ class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
return this._options.moduleCache.getModule(file);
}
_loadAsAssetFile(potentialModulePath: string, fromModule: TModule, toModule: string): TModule {
const {name, type} = getAssetDataFromName(potentialModulePath, this._options.platforms);
let pattern = '^' + name + '(@[\\d\\.]+x)?';
if (this._options.platform != null) {
pattern += '(\\.' + this._options.platform + ')?';
}
pattern += '\\.' + type + '$';
const dirname = path.dirname(potentialModulePath);
const assetFiles = this._options.matchFiles(dirname, new RegExp(pattern));
// We arbitrarly grab the lowest, because scale selection will happen
// somewhere else. Always the lowest so that it's stable between builds.
const assetFile = getArrayLowestItem(assetFiles);
if (assetFile) {
return this._options.moduleCache.getAssetModule(assetFile);
}
throw new UnableToResolveError(
fromModule,
toModule,
`Directory \`${dirname}' doesn't contain asset \`${name}'`,
);
}
_loadAsDir(potentialDirPath: string, fromModule: TModule, toModule: string): TModule {
if (!this._options.dirExists(potentialDirPath)) {
throw new UnableToResolveError(