packager: ResolutionRequest: keep option object as it is

Reviewed By: davidaurelio

Differential Revision: D4970219

fbshipit-source-id: 0e8a0b5e29452497e162d548229b086e0f91a516
This commit is contained in:
Jean Lauliac 2017-05-02 03:32:05 -07:00 committed by Facebook Github Bot
parent 8d373f3186
commit cc5997a2b1

View File

@ -90,44 +90,13 @@ function tryResolveSync<T>(action: () => T, secondaryAction: () => T): T {
} }
class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> { class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
_dirExists: DirExistsFn;
_entryPath: string;
_extraNodeModules: ?Object;
_hasteFS: HasteFS;
_helpers: DependencyGraphHelpers;
_immediateResolutionCache: {[key: string]: TModule}; _immediateResolutionCache: {[key: string]: TModule};
_matchFiles: MatchFilesByDirAndPattern; _options: Options<TModule, TPackage>;
_moduleCache: ModuleishCache<TModule, TPackage>;
_moduleMap: ModuleMap;
_platform: ?string;
_platforms: Set<string>;
_preferNativePlatform: boolean;
static emptyModule: string; static emptyModule: string;
constructor({ constructor(options: Options<TModule, TPackage>) {
dirExists, this._options = options;
entryPath,
extraNodeModules,
hasteFS,
helpers,
matchFiles,
moduleCache,
moduleMap,
platform,
platforms,
preferNativePlatform,
}: Options<TModule, TPackage>) {
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;
this._resetResolutionCache(); this._resetResolutionCache();
} }
@ -153,7 +122,7 @@ class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
return result; return result;
}; };
if (!this._helpers.isNodeModulesDir(fromModule.path) if (!this._options.helpers.isNodeModulesDir(fromModule.path)
&& !(isRelativeImport(toModuleName) || isAbsolutePath(toModuleName))) { && !(isRelativeImport(toModuleName) || isAbsolutePath(toModuleName))) {
const result = tryResolveSync( const result = tryResolveSync(
() => this._resolveHasteDependency(fromModule, toModuleName), () => this._resolveHasteDependency(fromModule, toModuleName),
@ -184,7 +153,7 @@ class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
onProgress?: ?(finishedModules: number, totalModules: number) => mixed, onProgress?: ?(finishedModules: number, totalModules: number) => mixed,
recursive: boolean, recursive: boolean,
}) { }) {
const entry = this._moduleCache.getModule(this._entryPath); const entry = this._options.moduleCache.getModule(this._options.entryPath);
response.pushDependency(entry); response.pushDependency(entry);
let totalModules = 1; let totalModules = 1;
@ -367,10 +336,10 @@ class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
realModuleName = toModuleName; realModuleName = toModuleName;
} }
const modulePath = this._moduleMap const modulePath = this._options.moduleMap
.getModule(realModuleName, this._platform, /* supportsNativePlatform */ true); .getModule(realModuleName, this._options.platform, /* supportsNativePlatform */ true);
if (modulePath != null) { if (modulePath != null) {
const module = this._moduleCache.getModule(modulePath); const module = this._options.moduleCache.getModule(modulePath);
/* temporary until we strengthen the typing */ /* temporary until we strengthen the typing */
invariant(module.type === 'Module', 'expected Module type'); invariant(module.type === 'Module', 'expected Module type');
return module; return module;
@ -379,8 +348,8 @@ class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
let packageName = realModuleName; let packageName = realModuleName;
let packagePath; let packagePath;
while (packageName && packageName !== '.') { while (packageName && packageName !== '.') {
packagePath = this._moduleMap packagePath = this._options.moduleMap
.getPackage(packageName, this._platform, /* supportsNativePlatform */ true); .getPackage(packageName, this._options.platform, /* supportsNativePlatform */ true);
if (packagePath != null) { if (packagePath != null) {
break; break;
} }
@ -389,7 +358,7 @@ class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
if (packagePath != null) { if (packagePath != null) {
const package_ = this._moduleCache.getPackage(packagePath); const package_ = this._options.moduleCache.getPackage(packagePath);
/* temporary until we strengthen the typing */ /* temporary until we strengthen the typing */
invariant(package_.type === 'Package', 'expected Package type'); invariant(package_.type === 'Package', 'expected Package type');
@ -472,19 +441,19 @@ class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
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._options.dirExists(searchPath)) {
searchQueue.push( searchQueue.push(
path.join(searchPath, realModuleName) path.join(searchPath, realModuleName)
); );
} }
} }
if (this._extraNodeModules) { if (this._options.extraNodeModules) {
const {_extraNodeModules} = this; const {extraNodeModules} = this._options;
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));
} }
} }
@ -529,9 +498,9 @@ class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
} }
_loadAsFile(potentialModulePath: string, fromModule: TModule, toModule: string): TModule { _loadAsFile(potentialModulePath: string, fromModule: TModule, toModule: string): TModule {
if (this._helpers.isAssetFile(potentialModulePath)) { if (this._options.helpers.isAssetFile(potentialModulePath)) {
const dirname = path.dirname(potentialModulePath); const dirname = path.dirname(potentialModulePath);
if (!this._dirExists(dirname)) { if (!this._options.dirExists(dirname)) {
throw new UnableToResolveError( throw new UnableToResolveError(
fromModule, fromModule,
toModule, toModule,
@ -539,35 +508,35 @@ class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
); );
} }
const {name, type} = getAssetDataFromName(potentialModulePath, this._platforms); const {name, type} = getAssetDataFromName(potentialModulePath, this._options.platforms);
let pattern = '^' + name + '(@[\\d\\.]+x)?'; let pattern = '^' + name + '(@[\\d\\.]+x)?';
if (this._platform != null) { if (this._options.platform != null) {
pattern += '(\\.' + this._platform + ')?'; pattern += '(\\.' + this._options.platform + ')?';
} }
pattern += '\\.' + type + '$'; 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 // We arbitrarly grab the lowest, because scale selection will happen
// somewhere else. Always the lowest so that it's stable between builds. // somewhere else. Always the lowest so that it's stable between builds.
const assetFile = getArrayLowestItem(assetFiles); const assetFile = getArrayLowestItem(assetFiles);
if (assetFile) { if (assetFile) {
return this._moduleCache.getAssetModule(assetFile); return this._options.moduleCache.getAssetModule(assetFile);
} }
} }
let file; let file;
if (this._hasteFS.exists(potentialModulePath)) { if (this._options.hasteFS.exists(potentialModulePath)) {
file = potentialModulePath; file = potentialModulePath;
} else if (this._platform != null && } else if (this._options.platform != null &&
this._hasteFS.exists(potentialModulePath + '.' + this._platform + '.js')) { this._options.hasteFS.exists(potentialModulePath + '.' + this._options.platform + '.js')) {
file = potentialModulePath + '.' + this._platform + '.js'; file = potentialModulePath + '.' + this._options.platform + '.js';
} else if (this._preferNativePlatform && } else if (this._preferNativePlatform &&
this._hasteFS.exists(potentialModulePath + '.native.js')) { this._options.hasteFS.exists(potentialModulePath + '.native.js')) {
file = 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'; file = potentialModulePath + '.js';
} else if (this._hasteFS.exists(potentialModulePath + '.json')) { } else if (this._options.hasteFS.exists(potentialModulePath + '.json')) {
file = potentialModulePath + '.json'; file = potentialModulePath + '.json';
} else { } else {
throw new UnableToResolveError( throw new UnableToResolveError(
@ -577,11 +546,11 @@ class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
); );
} }
return this._moduleCache.getModule(file); return this._options.moduleCache.getModule(file);
} }
_loadAsDir(potentialDirPath: string, fromModule: TModule, toModule: string): TModule { _loadAsDir(potentialDirPath: string, fromModule: TModule, toModule: string): TModule {
if (!this._dirExists(potentialDirPath)) { if (!this._options.dirExists(potentialDirPath)) {
throw new UnableToResolveError( throw new UnableToResolveError(
fromModule, fromModule,
toModule, toModule,
@ -590,8 +559,8 @@ class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
} }
const packageJsonPath = path.join(potentialDirPath, 'package.json'); const packageJsonPath = path.join(potentialDirPath, 'package.json');
if (this._hasteFS.exists(packageJsonPath)) { if (this._options.hasteFS.exists(packageJsonPath)) {
const main = this._moduleCache.getPackage(packageJsonPath).getMain(); const main = this._options.moduleCache.getPackage(packageJsonPath).getMain();
return tryResolveSync( return tryResolveSync(
() => this._loadAsFile(main, fromModule, toModule), () => this._loadAsFile(main, fromModule, toModule),
() => this._loadAsDir(main, fromModule, toModule), () => this._loadAsDir(main, fromModule, toModule),