RN ModuleCache.js: @flow

Reviewed By: davidaurelio

Differential Revision: D4117759

fbshipit-source-id: 158f6e50cb9211f87acce17047ed3e959e6c271f
This commit is contained in:
Jean Lauliac 2016-11-02 13:40:47 -07:00 committed by Facebook Github Bot
parent 94929b717c
commit 1baa2ab893
2 changed files with 51 additions and 17 deletions

View File

@ -18,14 +18,16 @@ const isAbsolutePath = require('absolute-path');
const jsonStableStringify = require('json-stable-stringify'); const jsonStableStringify = require('json-stable-stringify');
const path = require('path'); const path = require('path');
type Extractor = (sourceCode: string) => {deps: {sync: Array<string>}}; import type ModuleCache from './ModuleCache';
export type Extractor = (sourceCode: string) => {deps: {sync: Array<string>}};
type TransformedCode = { type TransformedCode = {
code?: string, code?: string,
dependencies?: Array<string>, dependencies?: Array<string>,
dependencyOffsets?: Array<number>, dependencyOffsets?: Array<number>,
map?: string, map?: string,
}; };
type TransformCode = ( export type TransformCode = (
module: Module, module: Module,
sourceCode: string, sourceCode: string,
transformOptions: mixed, transformOptions: mixed,
@ -35,7 +37,7 @@ type TransformCode = (
dependencyOffsets?: Array<number>, dependencyOffsets?: Array<number>,
map?: string, map?: string,
}>; }>;
type Cache = { export type Cache = {
get<T>( get<T>(
filePath: string, filePath: string,
key: string, key: string,
@ -43,15 +45,16 @@ type Cache = {
): Promise<T>, ): Promise<T>,
invalidate(filePath: string): void, invalidate(filePath: string): void,
}; };
type Options = {cacheTransformResults?: boolean}; export type Options = {cacheTransformResults?: boolean};
type ModuleCache = { export type FastFs = {
getPackageForModule(m: Module): { readFile: (filePath: string) => Promise<string>,
getName(): Promise<string>, closest: (innerFilePath: string, fileName: string) => string,
root: string, on: (
}, event: 'change',
onChange: (type: string, filePath: string, root: string) => void,
) => FastFs,
}; };
type FastFs = {readFile: (filePath: string) => Promise<string>}; export type DepGraphHelpers = {isNodeModulesDir: (filePath: string) => boolean};
type DepGraphHelpers = {isNodeModulesDir: (filePath: string) => boolean};
class Module { class Module {

View File

@ -5,6 +5,8 @@
* This source code is licensed under the BSD-style license found in the * 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 * 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. * of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/ */
'use strict'; 'use strict';
@ -16,8 +18,29 @@ const Polyfill = require('./Polyfill');
const path = require('path'); const path = require('path');
import type {
Cache,
DepGraphHelpers,
FastFs,
Extractor,
TransformCode,
Options as ModuleOptions,
} from './Module';
class ModuleCache { 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<Module, string>;
constructor({ constructor({
fastfs, fastfs,
cache, cache,
@ -26,7 +49,15 @@ class ModuleCache {
depGraphHelpers, depGraphHelpers,
assetDependencies, assetDependencies,
moduleOptions, moduleOptions,
}, platforms) { }: {
fastfs: FastFs,
cache: Cache,
extractRequires: Extractor,
transformCode: TransformCode,
depGraphHelpers: DepGraphHelpers,
assetDependencies: mixed,
moduleOptions: ModuleOptions,
}, platforms: mixed) {
this._moduleCache = Object.create(null); this._moduleCache = Object.create(null);
this._packageCache = Object.create(null); this._packageCache = Object.create(null);
this._fastfs = fastfs; this._fastfs = fastfs;
@ -42,7 +73,7 @@ class ModuleCache {
fastfs.on('change', this._processFileChange.bind(this)); fastfs.on('change', this._processFileChange.bind(this));
} }
getModule(filePath) { getModule(filePath: string) {
if (!this._moduleCache[filePath]) { if (!this._moduleCache[filePath]) {
this._moduleCache[filePath] = new Module({ this._moduleCache[filePath] = new Module({
file: filePath, file: filePath,
@ -62,7 +93,7 @@ class ModuleCache {
return this._moduleCache; return this._moduleCache;
} }
getAssetModule(filePath) { getAssetModule(filePath: string) {
if (!this._moduleCache[filePath]) { if (!this._moduleCache[filePath]) {
this._moduleCache[filePath] = new AssetModule({ this._moduleCache[filePath] = new AssetModule({
file: filePath, file: filePath,
@ -75,7 +106,7 @@ class ModuleCache {
return this._moduleCache[filePath]; return this._moduleCache[filePath];
} }
getPackage(filePath) { getPackage(filePath: string) {
if (!this._packageCache[filePath]) { if (!this._packageCache[filePath]) {
this._packageCache[filePath] = new Package({ this._packageCache[filePath] = new Package({
file: filePath, file: filePath,
@ -86,7 +117,7 @@ class ModuleCache {
return this._packageCache[filePath]; return this._packageCache[filePath];
} }
getPackageForModule(module) { getPackageForModule(module: Module): ?Package {
if (this._packageModuleMap.has(module)) { if (this._packageModuleMap.has(module)) {
const packagePath = this._packageModuleMap.get(module); const packagePath = this._packageModuleMap.get(module);
if (this._packageCache[packagePath]) { if (this._packageCache[packagePath]) {
@ -105,7 +136,7 @@ class ModuleCache {
return this.getPackage(packagePath); return this.getPackage(packagePath);
} }
createPolyfill({file}) { createPolyfill({file}: {file: string}) {
return new Polyfill({ return new Polyfill({
file, file,
cache: this._cache, cache: this._cache,