From 6bdc5e9f21772cddc60fe56fb0cbcb765ed4a9cf Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Mon, 3 Apr 2017 09:55:28 -0700 Subject: [PATCH] Adapt interface of node-haste duplicates Summary: Adapts mocked / duplicated functionality from `node-haste` to match the new synchronous return types in the original. Reviewed By: jeanlauliac Differential Revision: D4819137 fbshipit-source-id: 183316adc3fae161ad9999bf72bccb8218ef8941 --- .../src/ModuleGraph/node-haste/Module.js | 4 +- .../src/ModuleGraph/node-haste/ModuleCache.js | 20 +-- .../src/ModuleGraph/node-haste/Package.js | 118 +++++++++--------- .../ModuleGraph/node-haste/node-haste.flow.js | 7 +- .../src/ModuleGraph/node-haste/node-haste.js | 11 +- 5 files changed, 82 insertions(+), 78 deletions(-) diff --git a/packages/metro-bundler/src/ModuleGraph/node-haste/Module.js b/packages/metro-bundler/src/ModuleGraph/node-haste/Module.js index 13719de0..0636066a 100644 --- a/packages/metro-bundler/src/ModuleGraph/node-haste/Module.js +++ b/packages/metro-bundler/src/ModuleGraph/node-haste/Module.js @@ -24,9 +24,9 @@ module.exports = class Module { constructor( path: string, moduleCache: ModuleCache, - info: Promise, + info: TransformedFile, ) { - this.hasteID = info.then(({hasteID}) => hasteID); + this.hasteID = Promise.resolve(info.hasteID); this.moduleCache = moduleCache; this.name = this.hasteID.then(name => name || getName(path)); this.path = path; diff --git a/packages/metro-bundler/src/ModuleGraph/node-haste/ModuleCache.js b/packages/metro-bundler/src/ModuleGraph/node-haste/ModuleCache.js index bde9d0df..2a483af8 100644 --- a/packages/metro-bundler/src/ModuleGraph/node-haste/ModuleCache.js +++ b/packages/metro-bundler/src/ModuleGraph/node-haste/ModuleCache.js @@ -16,22 +16,20 @@ const Package = require('./Package'); import type {PackageData, TransformedFile} from '../types.flow'; -type GetFn = (path: string) => Promise; type GetClosestPackageFn = (filePath: string) => ?string; module.exports = class ModuleCache { _getClosestPackage: GetClosestPackageFn; - getPackageData: GetFn; - getTransformedFile: GetFn; + getTransformedFile: string => TransformedFile; modules: Map; packages: Map; - constructor(getClosestPackage: GetClosestPackageFn, getTransformedFile: GetFn) { + constructor( + getClosestPackage: GetClosestPackageFn, + getTransformedFile: string => TransformedFile, + ) { this._getClosestPackage = getClosestPackage; this.getTransformedFile = getTransformedFile; - this.getPackageData = path => getTransformedFile(path).then( - f => f.package || Promise.reject(new Error(`"${path}" does not exist`)) - ); this.modules = new Map(); this.packages = new Map(); } @@ -58,6 +56,14 @@ module.exports = class ModuleCache { return p; } + getPackageData(path: string): PackageData { + const pkg = this.getTransformedFile(path).package; + if (!pkg) { + throw new Error(`"${path}" does not exist`); + } + return pkg; + } + getPackageOf(filePath: string) { const candidate = this._getClosestPackage(filePath); return candidate != null ? this.getPackage(candidate) : null; diff --git a/packages/metro-bundler/src/ModuleGraph/node-haste/Package.js b/packages/metro-bundler/src/ModuleGraph/node-haste/Package.js index 9a3a6d7d..0f8ca641 100644 --- a/packages/metro-bundler/src/ModuleGraph/node-haste/Package.js +++ b/packages/metro-bundler/src/ModuleGraph/node-haste/Package.js @@ -17,12 +17,12 @@ const path = require('path'); import type {PackageData} from '../types.flow'; module.exports = class Package { - data: Promise; + data: PackageData; path: string; root: string; type: 'Package'; - constructor(packagePath: string, data: Promise) { + constructor(packagePath: string, data: PackageData) { this.data = data; this.path = packagePath; this.root = path.dirname(packagePath); @@ -31,80 +31,76 @@ module.exports = class Package { getMain() { // Copied from node-haste/Package.js - return this.data.then(data => { - const replacements = getReplacements(data); - if (typeof replacements === 'string') { - return path.join(this.root, replacements); - } + const replacements = getReplacements(this.data); + if (typeof replacements === 'string') { + return path.join(this.root, replacements); + } - let main = getMain(data); + let main = getMain(this.data); - if (replacements && typeof replacements === 'object') { - main = replacements[main] || - replacements[main + '.js'] || - replacements[main + '.json'] || - replacements[main.replace(/(\.js|\.json)$/, '')] || - main; - } + if (replacements && typeof replacements === 'object') { + main = replacements[main] || + replacements[main + '.js'] || + replacements[main + '.json'] || + replacements[main.replace(/(\.js|\.json)$/, '')] || + main; + } - return path.join(this.root, main); - }); + return path.join(this.root, main); } getName() { - return this.data.then(p => nullthrows(p.name)); + return Promise.resolve(nullthrows(this.data.name)); } isHaste() { - return this.data.then(p => !!p.name); + return Promise.resolve(!!this.data.name); } redirectRequire(name: string) { // Copied from node-haste/Package.js - return this.data.then(data => { - const replacements = getReplacements(data); - - if (!replacements || typeof replacements !== 'object') { - return name; - } - - if (!path.isAbsolute(name)) { - const replacement = replacements[name]; - // support exclude with "someDependency": false - return replacement === false - ? false - : replacement || name; - } - - let relPath = './' + path.relative(this.root, name); - if (path.sep !== '/') { - relPath = relPath.replace(new RegExp('\\' + path.sep, 'g'), '/'); - } - - let redirect = replacements[relPath]; - - // false is a valid value - if (redirect == null) { - redirect = replacements[relPath + '.js']; - if (redirect == null) { - redirect = replacements[relPath + '.json']; - } - } - - // support exclude with "./someFile": false - if (redirect === false) { - return false; - } - - if (redirect) { - return path.join( - this.root, - redirect - ); - } + const replacements = getReplacements(this.data); + if (!replacements || typeof replacements !== 'object') { return name; - }); + } + + if (!path.isAbsolute(name)) { + const replacement = replacements[name]; + // support exclude with "someDependency": false + return replacement === false + ? false + : replacement || name; + } + + let relPath = './' + path.relative(this.root, name); + if (path.sep !== '/') { + relPath = relPath.replace(new RegExp('\\' + path.sep, 'g'), '/'); + } + + let redirect = replacements[relPath]; + + // false is a valid value + if (redirect == null) { + redirect = replacements[relPath + '.js']; + if (redirect == null) { + redirect = replacements[relPath + '.json']; + } + } + + // support exclude with "./someFile": false + if (redirect === false) { + return false; + } + + if (redirect) { + return path.join( + this.root, + redirect + ); + } + + return name; } }; diff --git a/packages/metro-bundler/src/ModuleGraph/node-haste/node-haste.flow.js b/packages/metro-bundler/src/ModuleGraph/node-haste/node-haste.flow.js index a420bf6b..ff45d101 100644 --- a/packages/metro-bundler/src/ModuleGraph/node-haste/node-haste.flow.js +++ b/packages/metro-bundler/src/ModuleGraph/node-haste/node-haste.flow.js @@ -33,14 +33,13 @@ export type Package = { path: Path, root: Path, type: 'Package', - getMain(): Promise, + getMain(): Path, getName(): Promise, isHaste(): Promise, - redirectRequire(id: ModuleID): Promise, + redirectRequire(id: ModuleID): Path | false, }; -// when changing this to `type`, the code does not typecheck any more -export interface ModuleCache { +export type ModuleCache = { getAssetModule(path: Path): Module, getModule(path: Path): Module, getPackage(path: Path): Package, diff --git a/packages/metro-bundler/src/ModuleGraph/node-haste/node-haste.js b/packages/metro-bundler/src/ModuleGraph/node-haste/node-haste.js index 042e8c43..e3cbd224 100644 --- a/packages/metro-bundler/src/ModuleGraph/node-haste/node-haste.js +++ b/packages/metro-bundler/src/ModuleGraph/node-haste/node-haste.js @@ -63,10 +63,13 @@ exports.createResolveFn = function(options: ResolveOptions): ResolveFn { transformedFiles, } = options; const files = Object.keys(transformedFiles); - const getTransformedFile = - path => Promise.resolve( - transformedFiles[path] || Promise.reject(new Error(`"${path} does not exist`)) - ); + function getTransformedFile(path) { + const result = transformedFiles[path]; + if (!result) { + throw new Error(`"${path} does not exist`); + } + return result; + } const helpers = new DependencyGraphHelpers({ assetExts,