From 86aba173291e35cee94825f912cce43544a28d3c Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Wed, 29 Nov 2017 08:35:58 -0800 Subject: [PATCH] metro: clean up Module option, remove some from state Reviewed By: davidaurelio Differential Revision: D6435957 fbshipit-source-id: eaed8f5c1d4d18d6ff0bde34892f059a01fe1a65 --- .../traverseDependencies-integration-test.js | 2 +- packages/metro/src/Resolver/index.js | 9 +-- .../metro/src/node-haste/DependencyGraph.js | 15 +++-- packages/metro/src/node-haste/Module.js | 26 +++----- packages/metro/src/node-haste/ModuleCache.js | 66 +++++++++++-------- 5 files changed, 63 insertions(+), 55 deletions(-) diff --git a/packages/metro/src/DeltaBundler/__tests__/traverseDependencies-integration-test.js b/packages/metro/src/DeltaBundler/__tests__/traverseDependencies-integration-test.js index 438347a3..3ae7d0d1 100644 --- a/packages/metro/src/DeltaBundler/__tests__/traverseDependencies-integration-test.js +++ b/packages/metro/src/DeltaBundler/__tests__/traverseDependencies-integration-test.js @@ -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: []}; diff --git a/packages/metro/src/Resolver/index.js b/packages/metro/src/Resolver/index.js index 86fecfd9..85b53a16 100644 --- a/packages/metro/src/Resolver/index.js +++ b/packages/metro/src/Resolver/index.js @@ -43,7 +43,7 @@ type Options = {| +getPolyfills: ({platform: ?string}) => $ReadOnlyArray, +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, diff --git a/packages/metro/src/node-haste/DependencyGraph.js b/packages/metro/src/node-haste/DependencyGraph.js index b1e36bc2..d3d5b746 100644 --- a/packages/metro/src/node-haste/DependencyGraph.js +++ b/packages/metro/src/node-haste/DependencyGraph.js @@ -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, +preferNativePlatform: boolean, +providesModuleNodeModules: Array, +reporter: Reporter, + +resetCache: ?boolean, +resetCache: boolean, +roots: $ReadOnlyArray, +sourceExts: Array, + +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, diff --git a/packages/metro/src/node-haste/Module.js b/packages/metro/src/node-haste/Module.js index 4eb9c929..7fd587de 100644 --- a/packages/metro/src/node-haste/Module.js +++ b/packages/metro/src/node-haste/Module.js @@ -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; - 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 { - 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, }, }; } diff --git a/packages/metro/src/node-haste/ModuleCache.js b/packages/metro/src/node-haste/ModuleCache.js index 903e2573..23600de6 100644 --- a/packages/metro/src/node-haste/ModuleCache.js +++ b/packages/metro/src/node-haste/ModuleCache.js @@ -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, + depGraphHelpers: DependencyGraphHelpers, + hasteImpl: ?HasteImpl, + getClosestPackage: GetClosestPackageFn, + getTransformCacheKey: GetTransformCacheKey, + globalTransformCache: ?GlobalTransformCache, + roots: $ReadOnlyArray, + reporter: Reporter, + resetCache: boolean, + transformCache: TransformCache, + transformCode: TransformCode, +|}; + class ModuleCache { _assetDependencies: Array; _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; _platforms: Set; _transformCode: TransformCode; _reporter: Reporter; _roots: $ReadOnlyArray; + _opts: Options; - constructor( - { + constructor(options: Options, platforms: Set) { + const { assetDependencies, depGraphHelpers, - extractRequires, getClosestPackage, getTransformCacheKey, globalTransformCache, - moduleOptions, roots, reporter, transformCode, - }: {| - assetDependencies: Array, - depGraphHelpers: DependencyGraphHelpers, - getClosestPackage: GetClosestPackageFn, - getTransformCacheKey: GetTransformCacheKey, - globalTransformCache: ?GlobalTransformCache, - moduleOptions: ModuleOptions, - roots: $ReadOnlyArray, - reporter: Reporter, - transformCode: TransformCode, - |}, - platforms: Set, - ) { + } = 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;