metro: clean up Module option, remove some from state

Reviewed By: davidaurelio

Differential Revision: D6435957

fbshipit-source-id: eaed8f5c1d4d18d6ff0bde34892f059a01fe1a65
This commit is contained in:
Jean Lauliac 2017-11-29 08:35:58 -08:00 committed by Facebook Github Bot
parent 575142d0c2
commit 86aba17329
5 changed files with 63 additions and 55 deletions

View File

@ -106,8 +106,8 @@ describe('traverseDependencies', function() {
// This pattern is not expected to match anything. // This pattern is not expected to match anything.
ignorePattern: /.^/, ignorePattern: /.^/,
maxWorkers: 1, maxWorkers: 1,
moduleOptions: {transformCache: require('TransformCaching').mocked()},
resetCache: true, resetCache: true,
transformCache: require('TransformCaching').mocked(),
transformCode: (module, sourceCode, transformOptions) => { transformCode: (module, sourceCode, transformOptions) => {
return new Promise(resolve => { return new Promise(resolve => {
let deps = {dependencies: [], dependencyOffsets: []}; let deps = {dependencies: [], dependencyOffsets: []};

View File

@ -43,7 +43,7 @@ type Options = {|
+getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>, +getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>,
+getTransformCacheKey: GetTransformCacheKey, +getTransformCacheKey: GetTransformCacheKey,
+globalTransformCache: ?GlobalTransformCache, +globalTransformCache: ?GlobalTransformCache,
+hasteImpl?: HasteImpl, +hasteImpl?: ?HasteImpl,
+maxWorkers: number, +maxWorkers: number,
+minifyCode: MinifyCode, +minifyCode: MinifyCode,
+postMinifyProcess: PostMinifyProcess, +postMinifyProcess: PostMinifyProcess,
@ -82,13 +82,9 @@ class Resolver {
forceNodeFilesystemAPI: false, forceNodeFilesystemAPI: false,
getTransformCacheKey: opts.getTransformCacheKey, getTransformCacheKey: opts.getTransformCacheKey,
globalTransformCache: opts.globalTransformCache, globalTransformCache: opts.globalTransformCache,
hasteImpl: opts.hasteImpl,
ignorePattern: opts.blacklistRE || / ^/ /* matches nothing */, ignorePattern: opts.blacklistRE || / ^/ /* matches nothing */,
maxWorkers: opts.maxWorkers, maxWorkers: opts.maxWorkers,
moduleOptions: {
hasteImpl: opts.hasteImpl,
resetCache: opts.resetCache,
transformCache: opts.transformCache,
},
platforms: opts.platforms, platforms: opts.platforms,
preferNativePlatform: true, preferNativePlatform: true,
providesModuleNodeModules: opts.providesModuleNodeModules, providesModuleNodeModules: opts.providesModuleNodeModules,
@ -96,6 +92,7 @@ class Resolver {
resetCache: opts.resetCache, resetCache: opts.resetCache,
roots: opts.projectRoots, roots: opts.projectRoots,
sourceExts: opts.sourceExts, sourceExts: opts.sourceExts,
transformCache: opts.transformCache,
transformCode: opts.transformCode, transformCode: opts.transformCode,
useWatchman: true, useWatchman: true,
watch: opts.watch, watch: opts.watch,

View File

@ -36,10 +36,13 @@ const {EventEmitter} = require('events');
import type {Options as JSTransformerOptions} from '../JSTransformer/worker'; import type {Options as JSTransformerOptions} from '../JSTransformer/worker';
import type {GlobalTransformCache} from '../lib/GlobalTransformCache'; import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
import type {GetTransformCacheKey} from '../lib/TransformCaching'; import type {
GetTransformCacheKey,
TransformCache,
} from '../lib/TransformCaching';
import type {Reporter} from '../lib/reporting'; import type {Reporter} from '../lib/reporting';
import type {ModuleMap} from './DependencyGraph/ModuleResolution'; import type {ModuleMap} from './DependencyGraph/ModuleResolution';
import type {Options as ModuleOptions, TransformCode} from './Module'; import type {TransformCode, HasteImpl} from './Module';
import type Package from './Package'; import type Package from './Package';
import type {HasteFS} from './types'; import type {HasteFS} from './types';
@ -50,16 +53,18 @@ type Options = {|
+forceNodeFilesystemAPI: boolean, +forceNodeFilesystemAPI: boolean,
+getTransformCacheKey: GetTransformCacheKey, +getTransformCacheKey: GetTransformCacheKey,
+globalTransformCache: ?GlobalTransformCache, +globalTransformCache: ?GlobalTransformCache,
+hasteImpl: ?HasteImpl,
+ignorePattern: RegExp, +ignorePattern: RegExp,
+maxWorkers: number, +maxWorkers: number,
+moduleOptions: ModuleOptions,
+platforms: Set<string>, +platforms: Set<string>,
+preferNativePlatform: boolean, +preferNativePlatform: boolean,
+providesModuleNodeModules: Array<string>, +providesModuleNodeModules: Array<string>,
+reporter: Reporter, +reporter: Reporter,
+resetCache: ?boolean,
+resetCache: boolean, +resetCache: boolean,
+roots: $ReadOnlyArray<string>, +roots: $ReadOnlyArray<string>,
+sourceExts: Array<string>, +sourceExts: Array<string>,
+transformCache: TransformCache,
+transformCode: TransformCode, +transformCode: TransformCode,
+useWatchman: boolean, +useWatchman: boolean,
+watch: boolean, +watch: boolean,
@ -196,7 +201,9 @@ class DependencyGraph extends EventEmitter {
getClosestPackage: this._getClosestPackage.bind(this), getClosestPackage: this._getClosestPackage.bind(this),
getTransformCacheKey: _opts.getTransformCacheKey, getTransformCacheKey: _opts.getTransformCacheKey,
globalTransformCache: _opts.globalTransformCache, globalTransformCache: _opts.globalTransformCache,
moduleOptions: _opts.moduleOptions, hasteImpl: _opts.hasteImpl,
resetCache: _opts.resetCache,
transformCache: _opts.transformCache,
reporter: _opts.reporter, reporter: _opts.reporter,
roots: _opts.roots, roots: _opts.roots,
transformCode: _opts.transformCode, transformCode: _opts.transformCode,

View File

@ -66,8 +66,10 @@ export type HasteImpl = {
}; };
export type Options = { export type Options = {
hasteImpl?: HasteImpl, globalTransformCache: ?GlobalTransformCache,
resetCache?: boolean, hasteImpl: ?HasteImpl,
reporter: Reporter,
resetCache: boolean,
transformCache: TransformCache, transformCache: TransformCache,
}; };
@ -75,11 +77,9 @@ export type ConstructorArgs = {
depGraphHelpers: DependencyGraphHelpers, depGraphHelpers: DependencyGraphHelpers,
file: string, file: string,
getTransformCacheKey: GetTransformCacheKey, getTransformCacheKey: GetTransformCacheKey,
globalTransformCache: ?GlobalTransformCache,
localPath: LocalPath, localPath: LocalPath,
moduleCache: ModuleCache, moduleCache: ModuleCache,
options: Options, options: Options,
reporter: Reporter,
transformCode: ?TransformCode, transformCode: ?TransformCode,
}; };
@ -95,8 +95,6 @@ class Module {
_getTransformCacheKey: GetTransformCacheKey; _getTransformCacheKey: GetTransformCacheKey;
_depGraphHelpers: DependencyGraphHelpers; _depGraphHelpers: DependencyGraphHelpers;
_options: Options; _options: Options;
_reporter: Reporter;
_globalCache: ?GlobalTransformCache;
_docBlock: ?DocBlock; _docBlock: ?DocBlock;
_hasteNameCache: ?{+hasteName: ?string}; _hasteNameCache: ?{+hasteName: ?string};
@ -105,17 +103,13 @@ class Module {
_readResultsByOptionsKey: Map<string, CachedReadResult>; _readResultsByOptionsKey: Map<string, CachedReadResult>;
static _transformCache: TransformCache;
constructor({ constructor({
depGraphHelpers, depGraphHelpers,
localPath, localPath,
file, file,
getTransformCacheKey, getTransformCacheKey,
globalTransformCache,
moduleCache, moduleCache,
options, options,
reporter,
transformCode, transformCode,
}: ConstructorArgs) { }: ConstructorArgs) {
if (!isAbsolutePath(file)) { if (!isAbsolutePath(file)) {
@ -131,8 +125,6 @@ class Module {
this._getTransformCacheKey = getTransformCacheKey; this._getTransformCacheKey = getTransformCacheKey;
this._depGraphHelpers = depGraphHelpers; this._depGraphHelpers = depGraphHelpers;
this._options = options || {}; this._options = options || {};
this._reporter = reporter;
this._globalCache = globalTransformCache;
this._readPromises = new Map(); this._readPromises = new Map();
this._readResultsByOptionsKey = new Map(); this._readResultsByOptionsKey = new Map();
@ -280,15 +272,15 @@ class Module {
async _getTransformedCode( async _getTransformedCode(
cacheProps: ReadTransformProps, cacheProps: ReadTransformProps,
): Promise<TransformedCode> { ): Promise<TransformedCode> {
const {_globalCache} = this; const globalCache = this._options.globalTransformCache;
if (_globalCache == null || !_globalCache.shouldFetch(cacheProps)) { if (globalCache == null || !globalCache.shouldFetch(cacheProps)) {
return await this._transformCodeFor(cacheProps); return await this._transformCodeFor(cacheProps);
} }
const globalCachedResult = await _globalCache.fetch(cacheProps); const globalCachedResult = await globalCache.fetch(cacheProps);
if (globalCachedResult != null) { if (globalCachedResult != null) {
return globalCachedResult; return globalCachedResult;
} }
return await this._transformAndStoreCodeGlobally(cacheProps, _globalCache); return await this._transformAndStoreCodeGlobally(cacheProps, globalCache);
} }
async _getAndCacheTransformedCode( async _getAndCacheTransformedCode(
@ -398,7 +390,7 @@ class Module {
transformOptionsKey, transformOptionsKey,
cacheOptions: { cacheOptions: {
resetCache: this._options.resetCache, resetCache: this._options.resetCache,
reporter: this._reporter, reporter: this._options.reporter,
}, },
}; };
} }

View File

@ -20,13 +20,30 @@ const Polyfill = require('./Polyfill');
const toLocalPath = require('./lib/toLocalPath'); const toLocalPath = require('./lib/toLocalPath');
import type {GlobalTransformCache} from '../lib/GlobalTransformCache'; import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
import type {GetTransformCacheKey} from '../lib/TransformCaching'; import type {
TransformCache,
GetTransformCacheKey,
} from '../lib/TransformCaching';
import type {Reporter} from '../lib/reporting'; import type {Reporter} from '../lib/reporting';
import type DependencyGraphHelpers from './DependencyGraph/DependencyGraphHelpers'; import type DependencyGraphHelpers from './DependencyGraph/DependencyGraphHelpers';
import type {TransformCode, Options as ModuleOptions} from './Module'; import type {TransformCode, HasteImpl} from './Module';
type GetClosestPackageFn = (filePath: string) => ?string; type GetClosestPackageFn = (filePath: string) => ?string;
type Options = {|
assetDependencies: Array<string>,
depGraphHelpers: DependencyGraphHelpers,
hasteImpl: ?HasteImpl,
getClosestPackage: GetClosestPackageFn,
getTransformCacheKey: GetTransformCacheKey,
globalTransformCache: ?GlobalTransformCache,
roots: $ReadOnlyArray<string>,
reporter: Reporter,
resetCache: boolean,
transformCache: TransformCache,
transformCode: TransformCode,
|};
class ModuleCache { class ModuleCache {
_assetDependencies: Array<string>; _assetDependencies: Array<string>;
_depGraphHelpers: DependencyGraphHelpers; _depGraphHelpers: DependencyGraphHelpers;
@ -34,46 +51,32 @@ class ModuleCache {
_getTransformCacheKey: GetTransformCacheKey; _getTransformCacheKey: GetTransformCacheKey;
_globalTransformCache: ?GlobalTransformCache; _globalTransformCache: ?GlobalTransformCache;
_moduleCache: {[filePath: string]: Module, __proto__: null}; _moduleCache: {[filePath: string]: Module, __proto__: null};
_moduleOptions: ModuleOptions;
_packageCache: {[filePath: string]: Package, __proto__: null}; _packageCache: {[filePath: string]: Package, __proto__: null};
_packageModuleMap: WeakMap<Module, string>; _packageModuleMap: WeakMap<Module, string>;
_platforms: Set<string>; _platforms: Set<string>;
_transformCode: TransformCode; _transformCode: TransformCode;
_reporter: Reporter; _reporter: Reporter;
_roots: $ReadOnlyArray<string>; _roots: $ReadOnlyArray<string>;
_opts: Options;
constructor( constructor(options: Options, platforms: Set<string>) {
{ const {
assetDependencies, assetDependencies,
depGraphHelpers, depGraphHelpers,
extractRequires,
getClosestPackage, getClosestPackage,
getTransformCacheKey, getTransformCacheKey,
globalTransformCache, globalTransformCache,
moduleOptions,
roots, roots,
reporter, reporter,
transformCode, transformCode,
}: {| } = options;
assetDependencies: Array<string>, this._opts = options;
depGraphHelpers: DependencyGraphHelpers,
getClosestPackage: GetClosestPackageFn,
getTransformCacheKey: GetTransformCacheKey,
globalTransformCache: ?GlobalTransformCache,
moduleOptions: ModuleOptions,
roots: $ReadOnlyArray<string>,
reporter: Reporter,
transformCode: TransformCode,
|},
platforms: Set<string>,
) {
this._assetDependencies = assetDependencies; this._assetDependencies = assetDependencies;
this._getClosestPackage = getClosestPackage; this._getClosestPackage = getClosestPackage;
this._getTransformCacheKey = getTransformCacheKey; this._getTransformCacheKey = getTransformCacheKey;
this._globalTransformCache = globalTransformCache; this._globalTransformCache = globalTransformCache;
this._depGraphHelpers = depGraphHelpers; this._depGraphHelpers = depGraphHelpers;
this._moduleCache = Object.create(null); this._moduleCache = Object.create(null);
this._moduleOptions = moduleOptions;
this._packageCache = Object.create(null); this._packageCache = Object.create(null);
this._packageModuleMap = new WeakMap(); this._packageModuleMap = new WeakMap();
this._platforms = platforms; this._platforms = platforms;
@ -88,11 +91,9 @@ class ModuleCache {
depGraphHelpers: this._depGraphHelpers, depGraphHelpers: this._depGraphHelpers,
file: filePath, file: filePath,
getTransformCacheKey: this._getTransformCacheKey, getTransformCacheKey: this._getTransformCacheKey,
globalTransformCache: this._globalTransformCache,
localPath: toLocalPath(this._roots, filePath), localPath: toLocalPath(this._roots, filePath),
moduleCache: this, moduleCache: this,
options: this._moduleOptions, options: this._getModuleOptions(),
reporter: this._reporter,
transformCode: this._transformCode, transformCode: this._transformCode,
}); });
} }
@ -111,16 +112,17 @@ class ModuleCache {
*/ */
this._moduleCache[filePath] = new AssetModule( this._moduleCache[filePath] = new AssetModule(
{ {
depGraphHelpers: this._depGraphHelpers,
dependencies: this._assetDependencies, dependencies: this._assetDependencies,
depGraphHelpers: this._depGraphHelpers,
file: filePath, file: filePath,
getTransformCacheKey: this._getTransformCacheKey, getTransformCacheKey: this._getTransformCacheKey,
globalTransformCache: null, globalTransformCache: null,
localPath: toLocalPath(this._roots, filePath), localPath: toLocalPath(this._roots, filePath),
moduleCache: this, moduleCache: this,
options: this._moduleOptions,
reporter: this._reporter, reporter: this._reporter,
resetCache: this._opts.resetCache,
transformCode: this._transformCode, transformCode: this._transformCode,
options: this._getModuleOptions(),
}, },
this._platforms, this._platforms,
); );
@ -164,7 +166,7 @@ class ModuleCache {
getTransformCacheKey: this._getTransformCacheKey, getTransformCacheKey: this._getTransformCacheKey,
localPath: toLocalPath(this._roots, file), localPath: toLocalPath(this._roots, file),
moduleCache: this, moduleCache: this,
options: this._moduleOptions, options: this._getModuleOptions(),
transformCode: this._transformCode, transformCode: this._transformCode,
}); });
} }
@ -179,6 +181,16 @@ class ModuleCache {
delete this._packageCache[filePath]; delete this._packageCache[filePath];
} }
} }
_getModuleOptions() {
return {
reporter: this._opts.reporter,
transformCache: this._opts.transformCache,
globalTransformCache: this._opts.globalTransformCache,
resetCache: this._opts.resetCache,
hasteImpl: this._opts.hasteImpl,
};
}
} }
module.exports = ModuleCache; module.exports = ModuleCache;