From 41a6317fd2c4c712e498b09b203c230247eac049 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Tue, 11 Apr 2017 07:49:12 -0700 Subject: [PATCH] Push handling of missing parent module to `resolve` Reviewed By: jeanlauliac Differential Revision: D4863235 fbshipit-source-id: 857814cb25ee661db5e2cbdeac7175d9276044db --- packages/metro-bundler/src/ModuleGraph/Graph.js | 9 ++++----- .../src/ModuleGraph/__tests__/Graph-test.js | 10 +++++----- .../src/ModuleGraph/node-haste/node-haste.js | 2 +- packages/metro-bundler/src/ModuleGraph/types.flow.js | 3 +-- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/metro-bundler/src/ModuleGraph/Graph.js b/packages/metro-bundler/src/ModuleGraph/Graph.js index 27288c31..e8079139 100644 --- a/packages/metro-bundler/src/ModuleGraph/Graph.js +++ b/packages/metro-bundler/src/ModuleGraph/Graph.js @@ -46,7 +46,7 @@ type Async$Queue = { }; type LoadQueue = - Async$Queue<{id: string, parent: string}, Callback>>; + Async$Queue<{id: string, parent: ?string}, Callback>>; const createParentModule = () => ({file: {code: '', type: 'script', path: ''}, dependencies: []}); @@ -57,7 +57,6 @@ const NO_OPTIONS = {}; exports.create = function create(resolve: ResolveFn, load: LoadFn): GraphFn { function Graph(entryPoints, platform, options, callback = noop) { const { - cwd = '', log = (console: any), optimize = false, skip, @@ -74,7 +73,7 @@ exports.create = function create(resolve: ResolveFn, load: LoadFn): GraphFn { memoize((file, cb) => load(file, {log, optimize}, cb)), ), Number.MAX_SAFE_INTEGER); - const {collect, loadModule} = createGraphHelpers(loadQueue, cwd, skip); + const {collect, loadModule} = createGraphHelpers(loadQueue, skip); loadQueue.drain = () => { loadQueue.kill(); @@ -101,7 +100,7 @@ exports.create = function create(resolve: ResolveFn, load: LoadFn): GraphFn { return Graph; }; -function createGraphHelpers(loadQueue, cwd, skip) { +function createGraphHelpers(loadQueue, skip) { const modules = new Map([[null, createParentModule()]]); function collect( @@ -132,7 +131,7 @@ function createGraphHelpers(loadQueue, cwd, skip) { function loadModule(id, parent, parentDepIndex) { loadQueue.push( - {id, parent: parent != null ? parent : cwd}, + {id, parent}, (error, file, dependencyIDs) => onFileLoaded(error, file, dependencyIDs, id, parent, parentDepIndex), ); diff --git a/packages/metro-bundler/src/ModuleGraph/__tests__/Graph-test.js b/packages/metro-bundler/src/ModuleGraph/__tests__/Graph-test.js index 621f489d..032bd3fd 100644 --- a/packages/metro-bundler/src/ModuleGraph/__tests__/Graph-test.js +++ b/packages/metro-bundler/src/ModuleGraph/__tests__/Graph-test.js @@ -46,7 +46,7 @@ describe('Graph:', () => { const entryPoint = '/arbitrary/path'; graph([entryPoint], anyPlatform, noOpts, () => { expect(resolve).toBeCalledWith( - entryPoint, '', any(String), any(Object), any(Function)); + entryPoint, null, any(String), any(Object), any(Function)); done(); }); }); @@ -55,9 +55,9 @@ describe('Graph:', () => { const entryPoints = ['Arbitrary', '../entry.js']; graph(entryPoints, anyPlatform, noOpts, () => { expect(resolve).toBeCalledWith( - entryPoints[0], '', any(String), any(Object), any(Function)); + entryPoints[0], null, any(String), any(Object), any(Function)); expect(resolve).toBeCalledWith( - entryPoints[1], '', any(String), any(Object), any(Function)); + entryPoints[1], null, any(String), any(Object), any(Function)); done(); }); @@ -74,7 +74,7 @@ describe('Graph:', () => { const platform = 'any'; graph(anyEntry, platform, noOpts, () => { expect(resolve).toBeCalledWith( - any(String), '', platform, any(Object), any(Function)); + any(String), null, platform, any(Object), any(Function)); done(); }); }); @@ -83,7 +83,7 @@ describe('Graph:', () => { const log = new Console(); graph(anyEntry, anyPlatform, {log}, () => { expect(resolve).toBeCalledWith( - any(String), '', any(String), objectContaining({log}), any(Function)); + any(String), null, any(String), objectContaining({log}), any(Function)); done(); }); }); 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 3481185b..daf739a7 100644 --- a/packages/metro-bundler/src/ModuleGraph/node-haste/node-haste.js +++ b/packages/metro-bundler/src/ModuleGraph/node-haste/node-haste.js @@ -102,7 +102,7 @@ exports.createResolveFn = function(options: ResolveOptions): ResolveFn { const hasteMapBuilt = hasteMap.build(); const resolutionRequests = {}; const filesByDirNameIndex = new FilesByDirNameIndex(hasteMap.getAllFiles()); - return (id, source: ?string, platform, _, callback) => { + return (id, source, platform, _, callback) => { let resolutionRequest = resolutionRequests[platform]; if (!resolutionRequest) { resolutionRequest = resolutionRequests[platform] = new ResolutionRequest({ diff --git a/packages/metro-bundler/src/ModuleGraph/types.flow.js b/packages/metro-bundler/src/ModuleGraph/types.flow.js index 2c0c038c..4e8643b8 100644 --- a/packages/metro-bundler/src/ModuleGraph/types.flow.js +++ b/packages/metro-bundler/src/ModuleGraph/types.flow.js @@ -40,7 +40,6 @@ export type GraphFn = ( ) => void; type GraphOptions = {| - cwd?: string, log?: Console, optimize?: boolean, skip?: Set, @@ -90,7 +89,7 @@ export type PackageData = {| export type ResolveFn = ( id: string, - source: string, + source: ?string, platform: string, options?: ResolveOptions, callback: Callback,