mirror of https://github.com/status-im/metro.git
packager: ResolutionRequest: keep option object as it is
Reviewed By: davidaurelio Differential Revision: D4970219 fbshipit-source-id: 0e8a0b5e29452497e162d548229b086e0f91a516
This commit is contained in:
parent
ee0fc30814
commit
446b00018f
|
@ -90,44 +90,13 @@ function tryResolveSync<T>(action: () => T, secondaryAction: () => T): T {
|
|||
}
|
||||
|
||||
class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
|
||||
_dirExists: DirExistsFn;
|
||||
_entryPath: string;
|
||||
_extraNodeModules: ?Object;
|
||||
_hasteFS: HasteFS;
|
||||
_helpers: DependencyGraphHelpers;
|
||||
|
||||
_immediateResolutionCache: {[key: string]: TModule};
|
||||
_matchFiles: MatchFilesByDirAndPattern;
|
||||
_moduleCache: ModuleishCache<TModule, TPackage>;
|
||||
_moduleMap: ModuleMap;
|
||||
_platform: ?string;
|
||||
_platforms: Set<string>;
|
||||
_preferNativePlatform: boolean;
|
||||
_options: Options<TModule, TPackage>;
|
||||
static emptyModule: string;
|
||||
|
||||
constructor({
|
||||
dirExists,
|
||||
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;
|
||||
constructor(options: Options<TModule, TPackage>) {
|
||||
this._options = options;
|
||||
this._resetResolutionCache();
|
||||
}
|
||||
|
||||
|
@ -153,7 +122,7 @@ class ResolutionRequest<TModule: Moduleish, TPackage: Packageish> {
|
|||
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<TModule: Moduleish, TPackage: Packageish> {
|
|||
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<TModule: Moduleish, TPackage: Packageish> {
|
|||
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<TModule: Moduleish, TPackage: Packageish> {
|
|||
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<TModule: Moduleish, TPackage: Packageish> {
|
|||
|
||||
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<TModule: Moduleish, TPackage: Packageish> {
|
|||
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<TModule: Moduleish, TPackage: Packageish> {
|
|||
}
|
||||
|
||||
_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<TModule: Moduleish, TPackage: Packageish> {
|
|||
);
|
||||
}
|
||||
|
||||
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<TModule: Moduleish, TPackage: Packageish> {
|
|||
);
|
||||
}
|
||||
|
||||
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<TModule: Moduleish, TPackage: Packageish> {
|
|||
}
|
||||
|
||||
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),
|
||||
|
|
Loading…
Reference in New Issue