diff --git a/packages/metro-bundler/src/ModuleGraph/node-haste/Module.js b/packages/metro-bundler/src/ModuleGraph/node-haste/Module.js index 5d361df3..6e00ea6d 100644 --- a/packages/metro-bundler/src/ModuleGraph/node-haste/Module.js +++ b/packages/metro-bundler/src/ModuleGraph/node-haste/Module.js @@ -13,10 +13,10 @@ import type {CachedReadResult, ReadResult} from '../../node-haste/Module'; import type {TransformedCodeFile} from '../types.flow'; -import type {ModuleCache} from './node-haste.flow'; +import type ModuleCache from './ModuleCache'; module.exports = class Module { - hasteID: Promise; + hasteID: ?string; moduleCache: ModuleCache; name: Promise; path: string; @@ -27,9 +27,9 @@ module.exports = class Module { moduleCache: ModuleCache, info: TransformedCodeFile, ) { - this.hasteID = Promise.resolve(info.hasteID); + this.hasteID = info.hasteID; this.moduleCache = moduleCache; - this.name = this.hasteID.then(name => name || getName(path)); + this.name = Promise.resolve(this.hasteID || getName(path)); this.path = path; this.type = 'Module'; } @@ -51,7 +51,7 @@ module.exports = class Module { } isHaste() { - return this.hasteID.then(Boolean); + return Boolean(this.hasteID); } hash() { diff --git a/packages/metro-bundler/src/ModuleGraph/node-haste/ModuleCache.js b/packages/metro-bundler/src/ModuleGraph/node-haste/ModuleCache.js index 73bda233..0062e32d 100644 --- a/packages/metro-bundler/src/ModuleGraph/node-haste/ModuleCache.js +++ b/packages/metro-bundler/src/ModuleGraph/node-haste/ModuleCache.js @@ -34,11 +34,11 @@ module.exports = class ModuleCache { this.packages = new Map(); } - getAssetModule(path: string) { + getAssetModule(path: string): Module { return this.getModule(path); } - getModule(path: string) { + getModule(path: string): Module { let m = this.modules.get(path); if (!m) { m = new Module(path, this, this.getTransformedFile(path)); @@ -47,7 +47,7 @@ module.exports = class ModuleCache { return m; } - getPackage(path: string) { + getPackage(path: string): Package { let p = this.packages.get(path); if (!p) { p = new Package(path, this.getPackageData(path)); @@ -64,7 +64,7 @@ module.exports = class ModuleCache { return pkg; } - getPackageOf(filePath: string) { + getPackageOf(filePath: string): ?Package { const candidate = this._getClosestPackage(filePath); return candidate != null ? this.getPackage(candidate) : null; } diff --git a/packages/metro-bundler/src/ModuleGraph/node-haste/Package.js b/packages/metro-bundler/src/ModuleGraph/node-haste/Package.js index 0f8ca641..c6f42d45 100644 --- a/packages/metro-bundler/src/ModuleGraph/node-haste/Package.js +++ b/packages/metro-bundler/src/ModuleGraph/node-haste/Package.js @@ -49,12 +49,12 @@ module.exports = class Package { return path.join(this.root, main); } - getName() { + getName(): Promise { return Promise.resolve(nullthrows(this.data.name)); } - isHaste() { - return Promise.resolve(!!this.data.name); + isHaste(): boolean { + return !!this.data.name; } redirectRequire(name: string) { diff --git a/packages/metro-bundler/src/ModuleGraph/node-haste/node-haste.js b/packages/metro-bundler/src/ModuleGraph/node-haste/node-haste.js index 38fc8799..26c008f3 100644 --- a/packages/metro-bundler/src/ModuleGraph/node-haste/node-haste.js +++ b/packages/metro-bundler/src/ModuleGraph/node-haste/node-haste.js @@ -31,6 +31,8 @@ const ResolutionRequest = require('../../node-haste/DependencyGraph/ResolutionRe const defaults = require('../../../defaults'); +import type {Moduleish, Packageish} from '../../node-haste/DependencyGraph/ResolutionRequest'; + type ResolveOptions = {| assetExts: Extensions, extraNodeModules: {[id: string]: string}, @@ -45,7 +47,7 @@ const platforms = new Set(defaults.platforms); * a jest-haste-map's ModuleMap instance. Eventually, though, we'll * want to figure out how to reunify and get rid of `HasteMap`. */ -function getFakeModuleMap(hasteMap: HasteMap) { +function getFakeModuleMap(hasteMap: HasteMap) { return { getModule(name: string, platform: ?string): ?string { const module = hasteMap.getModule(name, platform); @@ -58,7 +60,7 @@ function getFakeModuleMap(hasteMap: HasteMap) { }; } -const nullModule = { +const nullModule: Moduleish = { path: '/', getPackage() {}, hash() { @@ -66,6 +68,8 @@ const nullModule = { }, readCached() { throw new Error('not implemented'); }, readFresh() { return Promise.reject(new Error('not implemented')); }, + isHaste() { throw new Error('not implemented'); }, + getName() { throw new Error('not implemented'); }, }; exports.createResolveFn = function(options: ResolveOptions): ResolveFn { diff --git a/packages/metro-bundler/src/node-haste/DependencyGraph/HasteMap.js b/packages/metro-bundler/src/node-haste/DependencyGraph/HasteMap.js index 6201fb94..262efb18 100644 --- a/packages/metro-bundler/src/node-haste/DependencyGraph/HasteMap.js +++ b/packages/metro-bundler/src/node-haste/DependencyGraph/HasteMap.js @@ -5,7 +5,11 @@ * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. + * + * @flow + * @format */ + 'use strict'; const EventEmitter = require('events'); @@ -18,15 +22,37 @@ const GENERIC_PLATFORM = 'generic'; const NATIVE_PLATFORM = 'native'; const PACKAGE_JSON = path.sep + 'package.json'; -class HasteMap extends EventEmitter { +import type {Moduleish, Packageish, ModuleishCache} from './ResolutionRequest'; +import type DependencyGraphHelpers from './DependencyGraphHelpers'; + +type Options = {| + extensions: Array, + files: Array, + helpers: DependencyGraphHelpers, + moduleCache: ModuleishCache, + platforms: Set, + preferNativePlatform: boolean, +|}; + +class HasteMap extends EventEmitter { + + _extensions: Array; + _files: Array; + _helpers: DependencyGraphHelpers; + _map: {}; + _moduleCache: ModuleishCache; + _packages: {}; + _platforms: Set; + _preferNativePlatform: boolean; + constructor({ extensions, files, - moduleCache, - preferNativePlatform, helpers, + moduleCache, platforms, - }) { + preferNativePlatform, + }: Options) { super(); this._extensions = extensions; this._files = files; @@ -35,8 +61,8 @@ class HasteMap extends EventEmitter { this._platforms = platforms; this._preferNativePlatform = preferNativePlatform; - this._processHastePackage = throat(1, this._processHastePackage.bind(this)); - this._processHasteModule = throat(1, this._processHasteModule.bind(this)); + (this: any)._processHastePackage = throat(1, this._processHastePackage.bind(this)); + (this: any)._processHasteModule = throat(1, this._processHasteModule.bind(this)); } build() { @@ -60,7 +86,7 @@ class HasteMap extends EventEmitter { return this._files; } - processFileChange(type, absPath) { + processFileChange(type: string, absPath: string) { return Promise.resolve().then(() => { /*eslint no-labels: 0 */ let invalidated; @@ -96,7 +122,7 @@ class HasteMap extends EventEmitter { }); } - getModule(name, platform = null): Module { + getModule(name: string, platform: ?string): ?TModule { const modulesMap = this._map[name]; if (modulesMap == null) { return null; @@ -117,11 +143,11 @@ class HasteMap extends EventEmitter { return module; } - getPackage(name): Package { + getPackage(name: string): TPackage { return this._packages[name]; } - _processHasteModule(file, previousName) { + _processHasteModule(file: string, previousName: ?string) { const module = this._moduleCache.getModule(file); return Promise.resolve().then(() => { const isHaste = module.isHaste(); @@ -136,7 +162,7 @@ class HasteMap extends EventEmitter { }); } - _processHastePackage(file, previousName) { + _processHastePackage(file: string, previousName: ?string) { const p = this._moduleCache.getPackage(file); return Promise.resolve().then(() => { const isHaste = p.isHaste(); @@ -157,7 +183,7 @@ class HasteMap extends EventEmitter { }); } - _updateHasteMap(name, mod) { + _updateHasteMap(name: string, mod: TModule | TPackage) { let existingModule; if (mod.type === 'Package') { diff --git a/packages/metro-bundler/src/node-haste/DependencyGraph/ResolutionRequest.js b/packages/metro-bundler/src/node-haste/DependencyGraph/ResolutionRequest.js index 7118f545..5178d693 100644 --- a/packages/metro-bundler/src/node-haste/DependencyGraph/ResolutionRequest.js +++ b/packages/metro-bundler/src/node-haste/DependencyGraph/ResolutionRequest.js @@ -49,21 +49,26 @@ export type ModuleMap = { ): ?string, }; -type Packageish = { +export type Packageish = { + isHaste(): boolean, + getName(): Promise, + path: string, redirectRequire(toModuleName: string): string | false, getMain(): string, +root: string, }; -type Moduleish = { +export type Moduleish = { +path: string, + isHaste(): boolean, + getName(): Promise, getPackage(): ?Packageish, hash(): string, readCached(transformOptions: TransformWorkerOptions): CachedReadResult, readFresh(transformOptions: TransformWorkerOptions): Promise, }; -type ModuleishCache = { +export type ModuleishCache = { getPackage( name: string, platform?: string, diff --git a/packages/metro-bundler/src/node-haste/Package.js b/packages/metro-bundler/src/node-haste/Package.js index 6e3f3efb..01904704 100644 --- a/packages/metro-bundler/src/node-haste/Package.js +++ b/packages/metro-bundler/src/node-haste/Package.js @@ -59,8 +59,8 @@ class Package { return path.join(this.root, main); } - isHaste(): Promise { - return Promise.resolve().then(() => !!this.read().name); + isHaste(): boolean { + return !!this.read().name; } getName(): Promise {