From 1ddb0227ffa167a043fe233189534fa94125afeb Mon Sep 17 00:00:00 2001 From: Bhuwan Khattar Date: Tue, 30 Jan 2018 11:20:20 -0800 Subject: [PATCH] Pass hasteImplModulePath to JestHasteMap and everywhere else Reviewed By: jeanlauliac Differential Revision: D6641294 fbshipit-source-id: 36ebec95e69fe920c6cc10addd96406521cb4b82 --- packages/metro/src/Bundler/index.js | 5 ++--- packages/metro/src/Config.js | 5 ++--- .../src/ModuleGraph/worker/transform-module.js | 15 ++++++++++----- packages/metro/src/Server/index.js | 5 ++--- packages/metro/src/index.js | 2 +- packages/metro/src/legacy.js | 2 +- packages/metro/src/node-haste/DependencyGraph.js | 7 ++++--- packages/metro/src/node-haste/Module.js | 15 ++++++++------- packages/metro/src/node-haste/ModuleCache.js | 6 +++--- packages/metro/src/shared/types.flow.js | 3 +-- 10 files changed, 34 insertions(+), 31 deletions(-) diff --git a/packages/metro/src/Bundler/index.js b/packages/metro/src/Bundler/index.js index 11922ae1..2b6101db 100644 --- a/packages/metro/src/Bundler/index.js +++ b/packages/metro/src/Bundler/index.js @@ -32,7 +32,6 @@ import type {DynamicRequiresBehavior} from '../ModuleGraph/worker/collectDepende import type {GlobalTransformCache} from '../lib/GlobalTransformCache'; import type {TransformCache} from '../lib/TransformCaching'; import type {Reporter} from '../lib/reporting'; -import type {HasteImpl} from '../node-haste/Module'; import type {BabelSourceMap} from 'babel-core'; import type { MetroSourceMapSegmentTuple, @@ -90,7 +89,7 @@ export type Options = {| +getPolyfills: ({platform: ?string}) => $ReadOnlyArray, +getTransformOptions?: GetTransformOptions, +globalTransformCache: ?GlobalTransformCache, - +hasteImpl?: HasteImpl, + +hasteImplModulePath?: string, +maxWorkers: number, +platforms: Array, +polyfillModuleNames: Array, @@ -148,7 +147,7 @@ class Bundler { transformModulePath: opts.transformModulePath, }), globalTransformCache: opts.globalTransformCache, - hasteImpl: opts.hasteImpl, + hasteImplModulePath: opts.hasteImplModulePath, maxWorkers: opts.maxWorkers, platforms: new Set(opts.platforms), polyfillModuleNames: opts.polyfillModuleNames, diff --git a/packages/metro/src/Config.js b/packages/metro/src/Config.js index d33665a3..21cd3405 100644 --- a/packages/metro/src/Config.js +++ b/packages/metro/src/Config.js @@ -25,7 +25,6 @@ import type {PostProcessModules} from './DeltaBundler'; import type {PostProcessModules as PostProcessModulesForBuck} from './ModuleGraph/types.flow.js'; import type {TransformVariants} from './ModuleGraph/types.flow'; import type {DynamicRequiresBehavior} from './ModuleGraph/worker/collectDependencies'; -import type {HasteImpl} from './node-haste/Module'; import type {IncomingMessage, ServerResponse} from 'http'; type Middleware = (IncomingMessage, ServerResponse, ?() => mixed) => mixed; @@ -139,11 +138,11 @@ export type ConfigT = { postProcessModulesForBuck: PostProcessModulesForBuck, /** - * A module that exports: + * Path to a require-able module that exports: * - a `getHasteName(filePath)` method that returns `hasteName` for module at * `filePath`, or undefined if `filePath` is not a haste module. */ - hasteImpl?: HasteImpl, + hasteImplModulePath?: string, transformVariants: () => TransformVariants, diff --git a/packages/metro/src/ModuleGraph/worker/transform-module.js b/packages/metro/src/ModuleGraph/worker/transform-module.js index dd9b6414..eb4dd618 100644 --- a/packages/metro/src/ModuleGraph/worker/transform-module.js +++ b/packages/metro/src/ModuleGraph/worker/transform-module.js @@ -41,7 +41,7 @@ import type {Ast} from 'babel-core'; export type TransformOptions = {| +asyncRequireModulePath: string, filename: string, - hasteImpl?: HasteImpl, + hasteImplModulePath?: string, polyfill?: boolean, +sourceExts: Set, transformer: Transformer, @@ -102,11 +102,16 @@ function transformModule( let hasteID = null; if (filename.indexOf(NODE_MODULES) === -1 && !polyfill) { hasteID = docblock.parse(docblock.extract(sourceCode)).providesModule; - if (options.hasteImpl) { - if (options.hasteImpl.enforceHasteNameMatches) { - options.hasteImpl.enforceHasteNameMatches(filename, hasteID); + if (options.hasteImplModulePath != null) { + // eslint-disable-next-line no-useless-call + const HasteImpl = (require.call( + null, + options.hasteImplModulePath, + ): HasteImpl); + if (HasteImpl.enforceHasteNameMatches) { + HasteImpl.enforceHasteNameMatches(filename, hasteID); } - hasteID = options.hasteImpl.getHasteName(filename); + hasteID = HasteImpl.getHasteName(filename); } } diff --git a/packages/metro/src/Server/index.js b/packages/metro/src/Server/index.js index 99a500d6..808dcdb3 100644 --- a/packages/metro/src/Server/index.js +++ b/packages/metro/src/Server/index.js @@ -30,7 +30,6 @@ const url = require('url'); const {getAsset} = require('../Assets'); import type {CustomError} from '../lib/formatBundlingError'; -import type {HasteImpl} from '../node-haste/Module'; import type {IncomingMessage, ServerResponse} from 'http'; import type {Reporter} from '../lib/reporting'; import type {Options as DeltaBundlerOptions} from '../DeltaBundler/Serializers'; @@ -76,7 +75,7 @@ class Server { extraNodeModules: {}, getPolyfills: ({platform: ?string}) => $ReadOnlyArray, getTransformOptions?: GetTransformOptions, - hasteImpl?: HasteImpl, + hasteImplModulePath?: string, maxWorkers: number, moduleFormat: string, platforms: Array, @@ -132,7 +131,7 @@ class Server { getPolyfills: options.getPolyfills, getTransformOptions: options.getTransformOptions, globalTransformCache: options.globalTransformCache, - hasteImpl: options.hasteImpl, + hasteImplModulePath: options.hasteImplModulePath, maxWorkers, moduleFormat: options.moduleFormat != null ? options.moduleFormat : 'haste', diff --git a/packages/metro/src/index.js b/packages/metro/src/index.js index b6ea638b..f15aabdb 100644 --- a/packages/metro/src/index.js +++ b/packages/metro/src/index.js @@ -122,7 +122,7 @@ async function runMetro({ normalizedConfig.getModulesRunBeforeMainModule, getTransformOptions: normalizedConfig.getTransformOptions, globalTransformCache: null, - hasteImpl: normalizedConfig.hasteImpl, + hasteImplModulePath: normalizedConfig.hasteImplModulePath, maxWorkers, platforms: defaults.platforms.concat(platforms), postMinifyProcess: normalizedConfig.postMinifyProcess, diff --git a/packages/metro/src/legacy.js b/packages/metro/src/legacy.js index ac877762..a2649f10 100644 --- a/packages/metro/src/legacy.js +++ b/packages/metro/src/legacy.js @@ -179,7 +179,7 @@ function toServerOptions(options: Options): ServerOptions { getPolyfills: options.getPolyfills, getTransformOptions: options.getTransformOptions, globalTransformCache: options.globalTransformCache, - hasteImpl: options.hasteImpl, + hasteImplModulePath: options.hasteImplModulePath, maxWorkers: options.maxWorkers, moduleFormat: options.moduleFormat, platforms: options.platforms, diff --git a/packages/metro/src/node-haste/DependencyGraph.js b/packages/metro/src/node-haste/DependencyGraph.js index 52cb72c7..e9c4c7c6 100644 --- a/packages/metro/src/node-haste/DependencyGraph.js +++ b/packages/metro/src/node-haste/DependencyGraph.js @@ -40,7 +40,7 @@ import type { } from '../lib/TransformCaching'; import type {Reporter} from '../lib/reporting'; import type {ModuleMap} from './DependencyGraph/ModuleResolution'; -import type {TransformCode, HasteImpl} from './Module'; +import type {TransformCode} from './Module'; import type Package from './Package'; import type {HasteFS} from './types'; @@ -52,7 +52,7 @@ type Options = {| +getPolyfills: ({platform: ?string}) => $ReadOnlyArray, +getTransformCacheKey: GetTransformCacheKey, +globalTransformCache: ?GlobalTransformCache, - +hasteImpl?: ?HasteImpl, + +hasteImplModulePath?: string, +maxWorkers: number, +platforms: Set, +polyfillModuleNames?: Array, @@ -111,6 +111,7 @@ class DependencyGraph extends EventEmitter { return new JestHasteMap({ extensions: opts.sourceExts.concat(opts.assetExts), forceNodeFilesystemAPI: !useWatchman, + hasteImplModulePath: opts.hasteImplModulePath, ignorePattern: opts.blacklistRE || / ^/, maxWorkers: opts.maxWorkers, mocksPattern: '', @@ -205,7 +206,7 @@ class DependencyGraph extends EventEmitter { getClosestPackage: this._getClosestPackage.bind(this), getTransformCacheKey: _opts.getTransformCacheKey, globalTransformCache: _opts.globalTransformCache, - hasteImpl: _opts.hasteImpl, + hasteImplModulePath: _opts.hasteImplModulePath, resetCache: _opts.resetCache, transformCache: _opts.transformCache, reporter: _opts.reporter, diff --git a/packages/metro/src/node-haste/Module.js b/packages/metro/src/node-haste/Module.js index c276d712..fac8fd44 100644 --- a/packages/metro/src/node-haste/Module.js +++ b/packages/metro/src/node-haste/Module.js @@ -64,7 +64,7 @@ export type HasteImpl = { export type Options = { globalTransformCache: ?GlobalTransformCache, - hasteImpl: ?HasteImpl, + hasteImplModulePath?: string, reporter: Reporter, resetCache: boolean, transformCache: TransformCache, @@ -217,16 +217,17 @@ class Module { * the "@providesModule" name (ex. "foo"). */ _readHasteName(): ?string { - const hasteImpl = this._options.hasteImpl; - if (hasteImpl == null) { + const hasteImplModulePath = this._options.hasteImplModulePath; + if (hasteImplModulePath == null) { return this._readHasteNameFromDocBlock(); } - const {enforceHasteNameMatches} = hasteImpl; - if (enforceHasteNameMatches != null) { + // eslint-disable-next-line no-useless-call + const HasteImpl = (require.call(null, hasteImplModulePath): HasteImpl); + if (HasteImpl.enforceHasteNameMatches != null) { const name = this._readHasteNameFromDocBlock(); - enforceHasteNameMatches(this.path, name || undefined); + HasteImpl.enforceHasteNameMatches(this.path, name || undefined); } - return hasteImpl.getHasteName(this.path); + return HasteImpl.getHasteName(this.path); } /** diff --git a/packages/metro/src/node-haste/ModuleCache.js b/packages/metro/src/node-haste/ModuleCache.js index a05e29aa..55020358 100644 --- a/packages/metro/src/node-haste/ModuleCache.js +++ b/packages/metro/src/node-haste/ModuleCache.js @@ -26,14 +26,14 @@ import type { } from '../lib/TransformCaching'; import type {Reporter} from '../lib/reporting'; import type DependencyGraphHelpers from './DependencyGraph/DependencyGraphHelpers'; -import type {TransformCode, HasteImpl} from './Module'; +import type {TransformCode} from './Module'; type GetClosestPackageFn = (filePath: string) => ?string; type Options = {| assetDependencies: Array, depGraphHelpers: DependencyGraphHelpers, - hasteImpl: ?HasteImpl, + hasteImplModulePath?: string, getClosestPackage: GetClosestPackageFn, getTransformCacheKey: GetTransformCacheKey, globalTransformCache: ?GlobalTransformCache, @@ -183,7 +183,7 @@ class ModuleCache { transformCache: this._opts.transformCache, globalTransformCache: this._opts.globalTransformCache, resetCache: this._opts.resetCache, - hasteImpl: this._opts.hasteImpl, + hasteImplModulePath: this._opts.hasteImplModulePath, }; } } diff --git a/packages/metro/src/shared/types.flow.js b/packages/metro/src/shared/types.flow.js index 2a92462f..f292a95d 100644 --- a/packages/metro/src/shared/types.flow.js +++ b/packages/metro/src/shared/types.flow.js @@ -21,7 +21,6 @@ import type {DynamicRequiresBehavior} from '../ModuleGraph/worker/collectDepende import type {GlobalTransformCache} from '../lib/GlobalTransformCache'; import type {TransformCache} from '../lib/TransformCaching'; import type {Reporter} from '../lib/reporting'; -import type {HasteImpl} from '../node-haste/Module'; import type { MetroSourceMap, MetroSourceMapSegmentTuple, @@ -82,7 +81,7 @@ export type Options = {| getPolyfills: ({platform: ?string}) => $ReadOnlyArray, getTransformOptions?: GetTransformOptions, globalTransformCache: ?GlobalTransformCache, - hasteImpl?: HasteImpl, + hasteImplModulePath?: string, maxWorkers?: number, moduleFormat?: string, platforms?: Array,