From cc5997a2b1cb8a2b66a9b2b7a12e391ad2bb0a6f Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Tue, 2 May 2017 03:32:05 -0700 Subject: [PATCH] packager: ResolutionRequest: keep option object as it is Reviewed By: davidaurelio Differential Revision: D4970219 fbshipit-source-id: 0e8a0b5e29452497e162d548229b086e0f91a516 --- .../DependencyGraph/ResolutionRequest.js | 101 ++++++------------ 1 file changed, 35 insertions(+), 66 deletions(-) diff --git a/packager/src/node-haste/DependencyGraph/ResolutionRequest.js b/packager/src/node-haste/DependencyGraph/ResolutionRequest.js index 201affb38..6e2a910c3 100644 --- a/packager/src/node-haste/DependencyGraph/ResolutionRequest.js +++ b/packager/src/node-haste/DependencyGraph/ResolutionRequest.js @@ -90,44 +90,13 @@ function tryResolveSync(action: () => T, secondaryAction: () => T): T { } class ResolutionRequest { - _dirExists: DirExistsFn; - _entryPath: string; - _extraNodeModules: ?Object; - _hasteFS: HasteFS; - _helpers: DependencyGraphHelpers; + _immediateResolutionCache: {[key: string]: TModule}; - _matchFiles: MatchFilesByDirAndPattern; - _moduleCache: ModuleishCache; - _moduleMap: ModuleMap; - _platform: ?string; - _platforms: Set; - _preferNativePlatform: boolean; + _options: Options; static emptyModule: string; - constructor({ - dirExists, - entryPath, - extraNodeModules, - hasteFS, - helpers, - matchFiles, - moduleCache, - moduleMap, - platform, - platforms, - preferNativePlatform, - }: Options) { - this._dirExists = dirExists; - this._entryPath = entryPath; - this._extraNodeModules = extraNodeModules; - this._hasteFS = hasteFS; - this._helpers = helpers; - this._matchFiles = matchFiles; - this._moduleCache = moduleCache; - this._moduleMap = moduleMap; - this._platform = platform; - this._platforms = platforms; - this._preferNativePlatform = preferNativePlatform; + constructor(options: Options) { + this._options = options; this._resetResolutionCache(); } @@ -153,7 +122,7 @@ class ResolutionRequest { return result; }; - if (!this._helpers.isNodeModulesDir(fromModule.path) + if (!this._options.helpers.isNodeModulesDir(fromModule.path) && !(isRelativeImport(toModuleName) || isAbsolutePath(toModuleName))) { const result = tryResolveSync( () => this._resolveHasteDependency(fromModule, toModuleName), @@ -184,7 +153,7 @@ class ResolutionRequest { onProgress?: ?(finishedModules: number, totalModules: number) => mixed, recursive: boolean, }) { - const entry = this._moduleCache.getModule(this._entryPath); + const entry = this._options.moduleCache.getModule(this._options.entryPath); response.pushDependency(entry); let totalModules = 1; @@ -367,10 +336,10 @@ class ResolutionRequest { realModuleName = toModuleName; } - const modulePath = this._moduleMap - .getModule(realModuleName, this._platform, /* supportsNativePlatform */ true); + const modulePath = this._options.moduleMap + .getModule(realModuleName, this._options.platform, /* supportsNativePlatform */ true); if (modulePath != null) { - const module = this._moduleCache.getModule(modulePath); + const module = this._options.moduleCache.getModule(modulePath); /* temporary until we strengthen the typing */ invariant(module.type === 'Module', 'expected Module type'); return module; @@ -379,8 +348,8 @@ class ResolutionRequest { let packageName = realModuleName; let packagePath; while (packageName && packageName !== '.') { - packagePath = this._moduleMap - .getPackage(packageName, this._platform, /* supportsNativePlatform */ true); + packagePath = this._options.moduleMap + .getPackage(packageName, this._options.platform, /* supportsNativePlatform */ true); if (packagePath != null) { break; } @@ -389,7 +358,7 @@ class ResolutionRequest { if (packagePath != null) { - const package_ = this._moduleCache.getPackage(packagePath); + const package_ = this._options.moduleCache.getPackage(packagePath); /* temporary until we strengthen the typing */ invariant(package_.type === 'Package', 'expected Package type'); @@ -472,19 +441,19 @@ class ResolutionRequest { currDir !== '.' && currDir !== realPath.parse(fromModule.path).root; currDir = path.dirname(currDir)) { const searchPath = path.join(currDir, 'node_modules'); - if (this._dirExists(searchPath)) { + if (this._options.dirExists(searchPath)) { searchQueue.push( path.join(searchPath, realModuleName) ); } } - if (this._extraNodeModules) { - const {_extraNodeModules} = this; + if (this._options.extraNodeModules) { + const {extraNodeModules} = this._options; const bits = toModuleName.split(path.sep); const packageName = bits[0]; - if (_extraNodeModules[packageName]) { - bits[0] = _extraNodeModules[packageName]; + if (extraNodeModules[packageName]) { + bits[0] = extraNodeModules[packageName]; searchQueue.push(path.join.apply(path, bits)); } } @@ -529,9 +498,9 @@ class ResolutionRequest { } _loadAsFile(potentialModulePath: string, fromModule: TModule, toModule: string): TModule { - if (this._helpers.isAssetFile(potentialModulePath)) { + if (this._options.helpers.isAssetFile(potentialModulePath)) { const dirname = path.dirname(potentialModulePath); - if (!this._dirExists(dirname)) { + if (!this._options.dirExists(dirname)) { throw new UnableToResolveError( fromModule, toModule, @@ -539,35 +508,35 @@ class ResolutionRequest { ); } - const {name, type} = getAssetDataFromName(potentialModulePath, this._platforms); + const {name, type} = getAssetDataFromName(potentialModulePath, this._options.platforms); let pattern = '^' + name + '(@[\\d\\.]+x)?'; - if (this._platform != null) { - pattern += '(\\.' + this._platform + ')?'; + if (this._options.platform != null) { + pattern += '(\\.' + this._options.platform + ')?'; } pattern += '\\.' + type + '$'; - const assetFiles = this._matchFiles(dirname, new RegExp(pattern)); + 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._moduleCache.getAssetModule(assetFile); + return this._options.moduleCache.getAssetModule(assetFile); } } let file; - if (this._hasteFS.exists(potentialModulePath)) { + if (this._options.hasteFS.exists(potentialModulePath)) { file = potentialModulePath; - } else if (this._platform != null && - this._hasteFS.exists(potentialModulePath + '.' + this._platform + '.js')) { - file = potentialModulePath + '.' + this._platform + '.js'; + } else if (this._options.platform != null && + this._options.hasteFS.exists(potentialModulePath + '.' + this._options.platform + '.js')) { + file = potentialModulePath + '.' + this._options.platform + '.js'; } else if (this._preferNativePlatform && - this._hasteFS.exists(potentialModulePath + '.native.js')) { + this._options.hasteFS.exists(potentialModulePath + '.native.js')) { file = potentialModulePath + '.native.js'; - } else if (this._hasteFS.exists(potentialModulePath + '.js')) { + } else if (this._options.hasteFS.exists(potentialModulePath + '.js')) { file = potentialModulePath + '.js'; - } else if (this._hasteFS.exists(potentialModulePath + '.json')) { + } else if (this._options.hasteFS.exists(potentialModulePath + '.json')) { file = potentialModulePath + '.json'; } else { throw new UnableToResolveError( @@ -577,11 +546,11 @@ class ResolutionRequest { ); } - return this._moduleCache.getModule(file); + return this._options.moduleCache.getModule(file); } _loadAsDir(potentialDirPath: string, fromModule: TModule, toModule: string): TModule { - if (!this._dirExists(potentialDirPath)) { + if (!this._options.dirExists(potentialDirPath)) { throw new UnableToResolveError( fromModule, toModule, @@ -590,8 +559,8 @@ class ResolutionRequest { } const packageJsonPath = path.join(potentialDirPath, 'package.json'); - if (this._hasteFS.exists(packageJsonPath)) { - const main = this._moduleCache.getPackage(packageJsonPath).getMain(); + if (this._options.hasteFS.exists(packageJsonPath)) { + const main = this._options.moduleCache.getPackage(packageJsonPath).getMain(); return tryResolveSync( () => this._loadAsFile(main, fromModule, toModule), () => this._loadAsDir(main, fromModule, toModule),