diff --git a/packager/react-packager/src/node-haste/Module.js b/packager/react-packager/src/node-haste/Module.js index 2a27dccfb..864846aef 100644 --- a/packager/react-packager/src/node-haste/Module.js +++ b/packager/react-packager/src/node-haste/Module.js @@ -18,14 +18,16 @@ const isAbsolutePath = require('absolute-path'); const jsonStableStringify = require('json-stable-stringify'); const path = require('path'); -type Extractor = (sourceCode: string) => {deps: {sync: Array}}; +import type ModuleCache from './ModuleCache'; + +export type Extractor = (sourceCode: string) => {deps: {sync: Array}}; type TransformedCode = { code?: string, dependencies?: Array, dependencyOffsets?: Array, map?: string, }; -type TransformCode = ( +export type TransformCode = ( module: Module, sourceCode: string, transformOptions: mixed, @@ -35,7 +37,7 @@ type TransformCode = ( dependencyOffsets?: Array, map?: string, }>; -type Cache = { +export type Cache = { get( filePath: string, key: string, @@ -43,15 +45,16 @@ type Cache = { ): Promise, invalidate(filePath: string): void, }; -type Options = {cacheTransformResults?: boolean}; -type ModuleCache = { - getPackageForModule(m: Module): { - getName(): Promise, - root: string, - }, +export type Options = {cacheTransformResults?: boolean}; +export type FastFs = { + readFile: (filePath: string) => Promise, + closest: (innerFilePath: string, fileName: string) => string, + on: ( + event: 'change', + onChange: (type: string, filePath: string, root: string) => void, + ) => FastFs, }; -type FastFs = {readFile: (filePath: string) => Promise}; -type DepGraphHelpers = {isNodeModulesDir: (filePath: string) => boolean}; +export type DepGraphHelpers = {isNodeModulesDir: (filePath: string) => boolean}; class Module { diff --git a/packager/react-packager/src/node-haste/ModuleCache.js b/packager/react-packager/src/node-haste/ModuleCache.js index 74a059a8e..4cdee4811 100644 --- a/packager/react-packager/src/node-haste/ModuleCache.js +++ b/packager/react-packager/src/node-haste/ModuleCache.js @@ -5,6 +5,8 @@ * 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 */ 'use strict'; @@ -16,8 +18,29 @@ const Polyfill = require('./Polyfill'); const path = require('path'); +import type { + Cache, + DepGraphHelpers, + FastFs, + Extractor, + TransformCode, + Options as ModuleOptions, +} from './Module'; + class ModuleCache { + _moduleCache: {[filePath: string]: Module}; + _packageCache: {[filePath: string]: Package}; + _fastfs: FastFs; + _cache: Cache; + _extractRequires: Extractor; + _transformCode: TransformCode; + _depGraphHelpers: DepGraphHelpers; + _platforms: mixed; + _assetDependencies: mixed; + _moduleOptions: ModuleOptions; + _packageModuleMap: WeakMap; + constructor({ fastfs, cache, @@ -26,7 +49,15 @@ class ModuleCache { depGraphHelpers, assetDependencies, moduleOptions, - }, platforms) { + }: { + fastfs: FastFs, + cache: Cache, + extractRequires: Extractor, + transformCode: TransformCode, + depGraphHelpers: DepGraphHelpers, + assetDependencies: mixed, + moduleOptions: ModuleOptions, + }, platforms: mixed) { this._moduleCache = Object.create(null); this._packageCache = Object.create(null); this._fastfs = fastfs; @@ -42,7 +73,7 @@ class ModuleCache { fastfs.on('change', this._processFileChange.bind(this)); } - getModule(filePath) { + getModule(filePath: string) { if (!this._moduleCache[filePath]) { this._moduleCache[filePath] = new Module({ file: filePath, @@ -62,7 +93,7 @@ class ModuleCache { return this._moduleCache; } - getAssetModule(filePath) { + getAssetModule(filePath: string) { if (!this._moduleCache[filePath]) { this._moduleCache[filePath] = new AssetModule({ file: filePath, @@ -75,7 +106,7 @@ class ModuleCache { return this._moduleCache[filePath]; } - getPackage(filePath) { + getPackage(filePath: string) { if (!this._packageCache[filePath]) { this._packageCache[filePath] = new Package({ file: filePath, @@ -86,7 +117,7 @@ class ModuleCache { return this._packageCache[filePath]; } - getPackageForModule(module) { + getPackageForModule(module: Module): ?Package { if (this._packageModuleMap.has(module)) { const packagePath = this._packageModuleMap.get(module); if (this._packageCache[packagePath]) { @@ -105,7 +136,7 @@ class ModuleCache { return this.getPackage(packagePath); } - createPolyfill({file}) { + createPolyfill({file}: {file: string}) { return new Polyfill({ file, cache: this._cache,