From 353bfba978d66d11bb736f1c0771132b65e7693c Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Wed, 10 May 2017 04:34:10 -0700 Subject: [PATCH] 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 --- .../DependencyGraph/ResolutionRequest.js | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/packages/metro-bundler/src/node-haste/DependencyGraph/ResolutionRequest.js b/packages/metro-bundler/src/node-haste/DependencyGraph/ResolutionRequest.js index 170d8e05..f4468fbe 100644 --- a/packages/metro-bundler/src/node-haste/DependencyGraph/ResolutionRequest.js +++ b/packages/metro-bundler/src/node-haste/DependencyGraph/ResolutionRequest.js @@ -500,30 +500,7 @@ class ResolutionRequest { _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 { 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(