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.
ignorePattern: /.^/,
maxWorkers: 1,
moduleOptions: {transformCache: require('TransformCaching').mocked()},
resetCache: true,
transformCache: require('TransformCaching').mocked(),
transformCode: (module, sourceCode, transformOptions) => {
return new Promise(resolve => {
let deps = {dependencies: [], dependencyOffsets: []};

View File

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

View File

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

View File

@ -66,8 +66,10 @@ export type HasteImpl = {
};
export type Options = {
hasteImpl?: HasteImpl,
resetCache?: boolean,
globalTransformCache: ?GlobalTransformCache,
hasteImpl: ?HasteImpl,
reporter: Reporter,
resetCache: boolean,
transformCache: TransformCache,
};
@ -75,11 +77,9 @@ export type ConstructorArgs = {
depGraphHelpers: DependencyGraphHelpers,
file: string,
getTransformCacheKey: GetTransformCacheKey,
globalTransformCache: ?GlobalTransformCache,
localPath: LocalPath,
moduleCache: ModuleCache,
options: Options,
reporter: Reporter,
transformCode: ?TransformCode,
};
@ -95,8 +95,6 @@ class Module {
_getTransformCacheKey: GetTransformCacheKey;
_depGraphHelpers: DependencyGraphHelpers;
_options: Options;
_reporter: Reporter;
_globalCache: ?GlobalTransformCache;
_docBlock: ?DocBlock;
_hasteNameCache: ?{+hasteName: ?string};
@ -105,17 +103,13 @@ class Module {
_readResultsByOptionsKey: Map<string, CachedReadResult>;
static _transformCache: TransformCache;
constructor({
depGraphHelpers,
localPath,
file,
getTransformCacheKey,
globalTransformCache,
moduleCache,
options,
reporter,
transformCode,
}: ConstructorArgs) {
if (!isAbsolutePath(file)) {
@ -131,8 +125,6 @@ class Module {
this._getTransformCacheKey = getTransformCacheKey;
this._depGraphHelpers = depGraphHelpers;
this._options = options || {};
this._reporter = reporter;
this._globalCache = globalTransformCache;
this._readPromises = new Map();
this._readResultsByOptionsKey = new Map();
@ -280,15 +272,15 @@ class Module {
async _getTransformedCode(
cacheProps: ReadTransformProps,
): Promise<TransformedCode> {
const {_globalCache} = this;
if (_globalCache == null || !_globalCache.shouldFetch(cacheProps)) {
const globalCache = this._options.globalTransformCache;
if (globalCache == null || !globalCache.shouldFetch(cacheProps)) {
return await this._transformCodeFor(cacheProps);
}
const globalCachedResult = await _globalCache.fetch(cacheProps);
const globalCachedResult = await globalCache.fetch(cacheProps);
if (globalCachedResult != null) {
return globalCachedResult;
}
return await this._transformAndStoreCodeGlobally(cacheProps, _globalCache);
return await this._transformAndStoreCodeGlobally(cacheProps, globalCache);
}
async _getAndCacheTransformedCode(
@ -398,7 +390,7 @@ class Module {
transformOptionsKey,
cacheOptions: {
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');
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 DependencyGraphHelpers from './DependencyGraph/DependencyGraphHelpers';
import type {TransformCode, Options as ModuleOptions} from './Module';
import type {TransformCode, HasteImpl} from './Module';
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 {
_assetDependencies: Array<string>;
_depGraphHelpers: DependencyGraphHelpers;
@ -34,46 +51,32 @@ class ModuleCache {
_getTransformCacheKey: GetTransformCacheKey;
_globalTransformCache: ?GlobalTransformCache;
_moduleCache: {[filePath: string]: Module, __proto__: null};
_moduleOptions: ModuleOptions;
_packageCache: {[filePath: string]: Package, __proto__: null};
_packageModuleMap: WeakMap<Module, string>;
_platforms: Set<string>;
_transformCode: TransformCode;
_reporter: Reporter;
_roots: $ReadOnlyArray<string>;
_opts: Options;
constructor(
{
constructor(options: Options, platforms: Set<string>) {
const {
assetDependencies,
depGraphHelpers,
extractRequires,
getClosestPackage,
getTransformCacheKey,
globalTransformCache,
moduleOptions,
roots,
reporter,
transformCode,
}: {|
assetDependencies: Array<string>,
depGraphHelpers: DependencyGraphHelpers,
getClosestPackage: GetClosestPackageFn,
getTransformCacheKey: GetTransformCacheKey,
globalTransformCache: ?GlobalTransformCache,
moduleOptions: ModuleOptions,
roots: $ReadOnlyArray<string>,
reporter: Reporter,
transformCode: TransformCode,
|},
platforms: Set<string>,
) {
} = options;
this._opts = options;
this._assetDependencies = assetDependencies;
this._getClosestPackage = getClosestPackage;
this._getTransformCacheKey = getTransformCacheKey;
this._globalTransformCache = globalTransformCache;
this._depGraphHelpers = depGraphHelpers;
this._moduleCache = Object.create(null);
this._moduleOptions = moduleOptions;
this._packageCache = Object.create(null);
this._packageModuleMap = new WeakMap();
this._platforms = platforms;
@ -88,11 +91,9 @@ class ModuleCache {
depGraphHelpers: this._depGraphHelpers,
file: filePath,
getTransformCacheKey: this._getTransformCacheKey,
globalTransformCache: this._globalTransformCache,
localPath: toLocalPath(this._roots, filePath),
moduleCache: this,
options: this._moduleOptions,
reporter: this._reporter,
options: this._getModuleOptions(),
transformCode: this._transformCode,
});
}
@ -111,16 +112,17 @@ class ModuleCache {
*/
this._moduleCache[filePath] = new AssetModule(
{
depGraphHelpers: this._depGraphHelpers,
dependencies: this._assetDependencies,
depGraphHelpers: this._depGraphHelpers,
file: filePath,
getTransformCacheKey: this._getTransformCacheKey,
globalTransformCache: null,
localPath: toLocalPath(this._roots, filePath),
moduleCache: this,
options: this._moduleOptions,
reporter: this._reporter,
resetCache: this._opts.resetCache,
transformCode: this._transformCode,
options: this._getModuleOptions(),
},
this._platforms,
);
@ -164,7 +166,7 @@ class ModuleCache {
getTransformCacheKey: this._getTransformCacheKey,
localPath: toLocalPath(this._roots, file),
moduleCache: this,
options: this._moduleOptions,
options: this._getModuleOptions(),
transformCode: this._transformCode,
});
}
@ -179,6 +181,16 @@ class ModuleCache {
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;