From 0ff379bb76170a2dab80bc73fa0902689f290b06 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Wed, 8 Mar 2017 10:37:11 -0800 Subject: [PATCH] packager: node-haste: cleanup the load() function Reviewed By: davidaurelio Differential Revision: D4659391 fbshipit-source-id: a83e218ba89ad459fed07a4dfb61c422a6b4db5d --- packager/src/Bundler/index.js | 4 +- .../src/Resolver/__tests__/Resolver-test.js | 22 +- packager/src/Resolver/index.js | 25 ++- packager/src/Server/__tests__/Server-test.js | 4 +- packager/src/Server/index.js | 5 +- .../__tests__/DependencyGraph-test.js | 195 +++++++++-------- packager/src/node-haste/index.js | 202 +++++++++--------- 7 files changed, 240 insertions(+), 217 deletions(-) diff --git a/packager/src/Bundler/index.js b/packager/src/Bundler/index.js index 15b06b0cc4..1ec116492c 100644 --- a/packager/src/Bundler/index.js +++ b/packager/src/Bundler/index.js @@ -204,7 +204,9 @@ class Bundler { this._transformer.kill(); return Promise.all([ this._cache.end(), - this.getResolver().getDependencyGraph().getWatcher().end(), + this.getResolver().getDependencyGraph().then(dependencyGraph => { + dependencyGraph.getWatcher().end(); + }), ]); } diff --git a/packager/src/Resolver/__tests__/Resolver-test.js b/packager/src/Resolver/__tests__/Resolver-test.js index b5daf28f7d..0bfa175120 100644 --- a/packager/src/Resolver/__tests__/Resolver-test.js +++ b/packager/src/Resolver/__tests__/Resolver-test.js @@ -8,6 +8,8 @@ */ 'use strict'; +jest.useRealTimers(); + jest.unmock('../'); jest.unmock('../../../defaults'); jest.mock('path'); @@ -37,6 +39,7 @@ describe('Resolver', function() { return polyfill; }); + DependencyGraph.load = jest.fn().mockImplementation(opts => Promise.resolve(new DependencyGraph(opts))); DependencyGraph.replacePatterns = require.requireActual('../../node-haste/lib/replacePatterns'); DependencyGraph.prototype.createPolyfill = jest.fn(); DependencyGraph.prototype.getDependencies = jest.fn(); @@ -91,20 +94,23 @@ describe('Resolver', function() { describe('getDependencies', function() { it('forwards transform options to the dependency graph', function() { + expect.assertions(1); const transformOptions = {arbitrary: 'options'}; const platform = 'ios'; const entry = '/root/index.js'; DependencyGraph.prototype.getDependencies.mockImplementation( () => Promise.reject()); - new Resolver({projectRoot: '/root'}) - .getDependencies(entry, {platform}, transformOptions); - expect(DependencyGraph.prototype.getDependencies).toBeCalledWith({ - entryPath: entry, - platform, - transformOptions, - recursive: true, - }); + return new Resolver({projectRoot: '/root'}) + .getDependencies(entry, {platform}, transformOptions) + .catch(() => { + expect(DependencyGraph.prototype.getDependencies).toBeCalledWith({ + entryPath: entry, + platform, + transformOptions, + recursive: true, + }); + }); }); it('passes custom platforms to the dependency graph', function() { diff --git a/packager/src/Resolver/index.js b/packager/src/Resolver/index.js index 63245edd17..181b121b1d 100644 --- a/packager/src/Resolver/index.js +++ b/packager/src/Resolver/index.js @@ -50,12 +50,13 @@ type Options = { class Resolver { + _depGraphPromise: Promise; _depGraph: DependencyGraph; _minifyCode: MinifyCode; _polyfillModuleNames: Array; constructor(opts: Options) { - this._depGraph = new DependencyGraph({ + this._depGraphPromise = DependencyGraph.load({ assetDependencies: ['react-native/Libraries/Image/AssetRegistry'], assetExts: opts.assetExts, cache: opts.cache, @@ -89,10 +90,15 @@ class Resolver { this._minifyCode = opts.minifyCode; this._polyfillModuleNames = opts.polyfillModuleNames || []; - this._depGraph.load().catch(err => { - console.error(err.message + '\n' + err.stack); - process.exit(1); - }); + this._depGraphPromise.then( + depGraph => { this._depGraph = depGraph; }, + err => { + console.error(err.message + '\n' + err.stack); + // FIXME(jeanlauliac): we shall never exit the process by ourselves, + // packager may be used in a server application or the like. + process.exit(1); + }, + ); } getShallowDependencies( @@ -114,17 +120,18 @@ class Resolver { getModuleId: mixed, ): Promise { const {platform, recursive = true} = options; - return this._depGraph.getDependencies({ + return this._depGraphPromise.then(depGraph => depGraph.getDependencies({ entryPath, platform, transformOptions, recursive, onProgress, - }).then(resolutionResponse => { + })).then(resolutionResponse => { this._getPolyfillDependencies().reverse().forEach( polyfill => resolutionResponse.prependDependency(polyfill) ); + /* $FlowFixMe: monkey patching */ resolutionResponse.getModuleId = getModuleId; return resolutionResponse.finalize(); }); @@ -245,8 +252,8 @@ class Resolver { return this._minifyCode(path, code, map); } - getDependencyGraph(): DependencyGraph { - return this._depGraph; + getDependencyGraph(): Promise { + return this._depGraphPromise; } } diff --git a/packager/src/Server/__tests__/Server-test.js b/packager/src/Server/__tests__/Server-test.js index 75d8e5a679..2e6ddd8a05 100644 --- a/packager/src/Server/__tests__/Server-test.js +++ b/packager/src/Server/__tests__/Server-test.js @@ -80,10 +80,10 @@ describe('processRequest', () => { Bundler.prototype.invalidateFile = invalidatorFunc; Bundler.prototype.getResolver = jest.fn().mockReturnValue({ - getDependencyGraph: jest.fn().mockReturnValue({ + getDependencyGraph: jest.fn().mockReturnValue(Promise.resolve({ getHasteMap: jest.fn().mockReturnValue({on: jest.fn()}), load: jest.fn(() => Promise.resolve()), - }), + })), }); server = new Server(options); diff --git a/packager/src/Server/index.js b/packager/src/Server/index.js index 7fc1f12fb7..46929210ca 100644 --- a/packager/src/Server/index.js +++ b/packager/src/Server/index.js @@ -251,8 +251,7 @@ class Server { this._bundler = new Bundler(bundlerOpts); // changes to the haste map can affect resolution of files in the bundle - const dependencyGraph = this._bundler.getResolver().getDependencyGraph(); - dependencyGraph.load().then(() => { + this._bundler.getResolver().getDependencyGraph().then(dependencyGraph => { dependencyGraph.getWatcher().on( 'change', ({eventsQueue}) => eventsQueue.forEach(processFileChange), @@ -307,7 +306,7 @@ class Server { entryFile: string, platform?: string, }): Promise { - return this._bundler.getResolver().getDependencyGraph().load().then(() => { + return this._bundler.getResolver().getDependencyGraph().then(() => { if (!options.platform) { options.platform = getPlatformExtension(options.entryFile); } diff --git a/packager/src/node-haste/__tests__/DependencyGraph-test.js b/packager/src/node-haste/__tests__/DependencyGraph-test.js index 5316d0ea7d..3c5aca6fbc 100644 --- a/packager/src/node-haste/__tests__/DependencyGraph-test.js +++ b/packager/src/node-haste/__tests__/DependencyGraph-test.js @@ -37,8 +37,9 @@ describe('DependencyGraph', function() { let ResolutionRequest; let defaults; - function getOrderedDependenciesAsJSON(dgraph, entryPath, platform, recursive = true) { - return dgraph.getDependencies({entryPath, platform, recursive}) + function getOrderedDependenciesAsJSON(dgraphPromise, entryPath, platform, recursive = true) { + return dgraphPromise + .then(dgraph => dgraph.getDependencies({entryPath, platform, recursive})) .then(response => response.finalize()) .then(({dependencies}) => Promise.all(dependencies.map(dep => Promise.all([ dep.getName(), @@ -167,7 +168,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -220,7 +221,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -264,7 +265,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -316,7 +317,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -364,7 +365,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -418,7 +419,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -466,7 +467,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -519,7 +520,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -594,7 +595,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -665,7 +666,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -714,7 +715,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -763,7 +764,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -820,7 +821,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -872,7 +873,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -919,7 +920,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -965,7 +966,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -1008,7 +1009,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -1055,7 +1056,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -1100,7 +1101,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -1138,12 +1139,12 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); - return dgraph.load().catch(err => { + return dgraph.catch(err => { expect(err.message).toEqual( `Failed to build DependencyGraph: @providesModule naming collision:\n` + ` Duplicate module name: index\n` + @@ -1169,7 +1170,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -1203,7 +1204,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -1258,7 +1259,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -1344,7 +1345,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -1397,7 +1398,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -1450,7 +1451,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], assetExts: ['png', 'jpg'], @@ -1504,7 +1505,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], assetExts: ['png', 'jpg'], @@ -1573,7 +1574,7 @@ describe('DependencyGraph', function() { }, }); - const dgraph = new DependencyGraph({ + const dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -1668,7 +1669,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -1735,7 +1736,7 @@ describe('DependencyGraph', function() { }, }); - const dgraph = new DependencyGraph({ + const dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -1812,7 +1813,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -1877,7 +1878,7 @@ describe('DependencyGraph', function() { }, }); - const dgraph = new DependencyGraph({ + const dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -1939,7 +1940,7 @@ describe('DependencyGraph', function() { }, }); - const dgraph = new DependencyGraph({ + const dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -2023,7 +2024,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -2075,7 +2076,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -2186,7 +2187,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -2254,7 +2255,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], extraNodeModules: { @@ -2308,7 +2309,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], extraNodeModules: { @@ -2354,7 +2355,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], extraNodeModules: { @@ -2428,7 +2429,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -2478,7 +2479,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -2531,7 +2532,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -2634,7 +2635,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -2710,7 +2711,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -2788,7 +2789,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -2879,7 +2880,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -2959,7 +2960,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -3094,7 +3095,7 @@ describe('DependencyGraph', function() { }; setMockFileSystem(filesystem); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root, otherRoot], }); @@ -3200,7 +3201,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -3251,7 +3252,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -3308,7 +3309,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -3365,7 +3366,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, platforms: new Set(['ios', 'android', 'web']), roots: [root], @@ -3411,7 +3412,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -3469,7 +3470,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -3531,7 +3532,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -3609,7 +3610,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -3685,7 +3686,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -3763,7 +3764,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -3854,7 +3855,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -3934,7 +3935,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -4068,7 +4069,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root, otherRoot], }); @@ -4166,7 +4167,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -4218,7 +4219,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -4260,7 +4261,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -4317,7 +4318,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -4374,7 +4375,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -4419,7 +4420,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -4477,7 +4478,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -4566,7 +4567,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -4628,7 +4629,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -4691,7 +4692,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -4731,7 +4732,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -4808,7 +4809,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], assetExts: ['png'], @@ -4820,7 +4821,6 @@ describe('DependencyGraph', function() { } ).then(() => { filesystem.root['foo.png'] = ''; - dgraph._hasteFS._files[root + '/foo.png'] = ['', 8648460, 1, []]; return triggerAndProcessWatchEvent(dgraph, 'change', root + '/foo.png'); }).then( () => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'), @@ -4873,7 +4873,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -4933,7 +4933,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -4948,11 +4948,11 @@ describe('DependencyGraph', function() { name: 'bPackage', main: 'main.js', }); - return new Promise(resolve => { - dgraph.once('change', () => resolve()); + return dgraph.then(dg => new Promise(resolve => { + dg.once('change', () => resolve()); triggerWatchEvent('change', root + '/index.js'); triggerWatchEvent('change', root + '/aPackage/package.json'); - }); + })); }).then( () => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'), ).then(function(deps) { @@ -5012,7 +5012,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -5105,7 +5105,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -5168,7 +5168,7 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], }); @@ -5214,13 +5214,14 @@ describe('DependencyGraph', function() { }, }); - var dgraph = new DependencyGraph({ + var dgraph = DependencyGraph.load({ ...defaults, roots: [root], extensions: ['jsx', 'coffee'], }); - return dgraph.matchFilesByPattern('.*') + return dgraph + .then(dg => dg.matchFilesByPattern('.*')) .then(files => { expect(files).toEqual([ '/root/index.jsx', '/root/a.coffee', @@ -5285,10 +5286,10 @@ describe('DependencyGraph', function() { }, }); const DependencyGraph = require('../'); - dependencyGraph = new DependencyGraph({ + return DependencyGraph.load({ ...defaults, roots: ['/root'], - }); + }).then(dg => { dependencyGraph = dg; }); }); it('calls back for each finished module', () => { @@ -5330,15 +5331,13 @@ describe('DependencyGraph', function() { }, }); - const dependencyGraph = new DependencyGraph({ + DependencyGraph.load({ ...defaults, assetDependencies, roots: ['/root'], - }); - - return dependencyGraph.getDependencies({ + }).then(dependencyGraph => dependencyGraph.getDependencies({ entryPath: '/root/index.js', - }).then(({dependencies}) => { + })).then(({dependencies}) => { const [, assetModule] = dependencies; return assetModule.getDependencies() .then(deps => expect(deps).toBe(assetDependencies)); @@ -5374,7 +5373,7 @@ describe('DependencyGraph', function() { 'f.js': 'require("./c");', // circular dependency }, }); - dependencyGraph = new DependencyGraph({ + dependencyGraph = DependencyGraph.load({ ...defaults, roots: ['/root'], }); @@ -5462,11 +5461,11 @@ describe('DependencyGraph', function() { return require('graceful-fs').__setMockFilesystem(object); } - function triggerAndProcessWatchEvent(dgraph, eventType, filename) { - return new Promise(resolve => { + function triggerAndProcessWatchEvent(dgraphPromise, eventType, filename) { + return dgraphPromise.then(dgraph => new Promise(resolve => { dgraph.once('change', () => resolve()); triggerWatchEvent(eventType, filename); - }); + })); } function triggerWatchEvent(eventType, filename) { diff --git a/packager/src/node-haste/index.js b/packager/src/node-haste/index.js index cdcf798522..e4b502ec54 100644 --- a/packager/src/node-haste/index.js +++ b/packager/src/node-haste/index.js @@ -72,90 +72,102 @@ type Options = { }; class DependencyGraph extends EventEmitter { + _opts: Options; _haste: JestHasteMap; - _hasteFS: HasteFS; _helpers: DependencyGraphHelpers; _moduleCache: ModuleCache; + _hasteFS: HasteFS; _moduleMap: ModuleMap; - _loading: Promise; - - constructor(opts: Options) { + constructor(config: { + opts: Options, + haste: JestHasteMap, + initialHasteFS: HasteFS, + initialModuleMap: ModuleMap, + }) { super(); - this._opts = {...opts}; + this._opts = {...config.opts}; + this._haste = config.haste; + this._hasteFS = config.initialHasteFS; + this._moduleMap = config.initialModuleMap; this._helpers = new DependencyGraphHelpers(this._opts); - this.load(); + this._haste.on('change', this._onHasteChange.bind(this)); + this._moduleCache = this._createModuleCache(); } - load(): Promise { - if (this._loading) { - return this._loading; - } - - const mw = this._opts.maxWorkers; - this._haste = new JestHasteMap({ - extensions: this._opts.extensions.concat(this._opts.assetExts), - forceNodeFilesystemAPI: this._opts.forceNodeFilesystemAPI, - ignorePattern: {test: this._opts.ignoreFilePath}, + static _createHaste(opts: Options): JestHasteMap { + const mw = opts.maxWorkers; + return new JestHasteMap({ + extensions: opts.extensions.concat(opts.assetExts), + forceNodeFilesystemAPI: opts.forceNodeFilesystemAPI, + ignorePattern: {test: opts.ignoreFilePath}, maxWorkers: typeof mw === 'number' && mw >= 1 ? mw : getMaxWorkers(), mocksPattern: '', name: 'react-native-packager', - platforms: Array.from(this._opts.platforms), - providesModuleNodeModules: this._opts.providesModuleNodeModules, - resetCache: this._opts.resetCache, + platforms: Array.from(opts.platforms), + providesModuleNodeModules: opts.providesModuleNodeModules, + resetCache: opts.resetCache, retainAllFiles: true, - roots: this._opts.roots, - useWatchman: this._opts.useWatchman, - watch: this._opts.watch, + roots: opts.roots, + useWatchman: opts.useWatchman, + watch: opts.watch, }); + } + static load(opts: Options): Promise { const initializingPackagerLogEntry = log(createActionStartEntry('Initializing Packager')); - this._opts.reporter.update({type: 'dep_graph_loading'}); - this._loading = this._haste.build().then(({hasteFS, moduleMap}) => { - this._hasteFS = hasteFS; - this._moduleMap = moduleMap; - - this._moduleCache = new ModuleCache({ - cache: this._opts.cache, - getTransformCacheKey: this._opts.getTransformCacheKey, - globalTransformCache: this._opts.globalTransformCache, - transformCode: this._opts.transformCode, - depGraphHelpers: this._helpers, - assetDependencies: this._opts.assetDependencies, - moduleOptions: this._opts.moduleOptions, - reporter: this._opts.reporter, - getClosestPackage: filePath => { - const parsedPath = path.parse(filePath); - const root = parsedPath.root; - let dir = parsedPath.dir; - do { - const candidate = path.join(dir, 'package.json'); - if (this._hasteFS.exists(candidate)) { - return candidate; - } - dir = path.dirname(dir); - } while (dir !== '.' && dir !== root); - return null; - }, - }, this._opts.platforms); - - this._haste.on('change', event => { - this._hasteFS = event.hasteFS; - this._moduleMap = event.moduleMap; - event.eventsQueue.forEach(({type, filePath, stat}) => - this._moduleCache.processFileChange(type, filePath, stat) - ); - this.emit('change'); - }); - + opts.reporter.update({type: 'dep_graph_loading'}); + const haste = DependencyGraph._createHaste(opts); + return haste.build().then(({hasteFS, moduleMap}) => { log(createActionEndEntry(initializingPackagerLogEntry)); - this._opts.reporter.update({type: 'dep_graph_loaded'}); - + opts.reporter.update({type: 'dep_graph_loaded'}); + return new DependencyGraph({ + opts, + haste, + initialHasteFS: hasteFS, + initialModuleMap: moduleMap, + }); }); + } - return this._loading; + _getClosestPackage(filePath: string): ?string { + const parsedPath = path.parse(filePath); + const root = parsedPath.root; + let dir = parsedPath.dir; + do { + const candidate = path.join(dir, 'package.json'); + if (this._hasteFS.exists(candidate)) { + return candidate; + } + dir = path.dirname(dir); + } while (dir !== '.' && dir !== root); + return null; + } + + _onHasteChange({eventsQueue, hasteFS, moduleMap}) { + this._hasteFS = hasteFS; + this._moduleMap = moduleMap; + eventsQueue.forEach(({type, filePath, stat}) => + this._moduleCache.processFileChange(type, filePath, stat) + ); + this.emit('change'); + } + + _createModuleCache() { + const {_opts} = this; + return new ModuleCache({ + cache: _opts.cache, + getTransformCacheKey: _opts.getTransformCacheKey, + globalTransformCache: _opts.globalTransformCache, + transformCode: _opts.transformCode, + depGraphHelpers: this._helpers, + assetDependencies: _opts.assetDependencies, + moduleOptions: _opts.moduleOptions, + reporter: _opts.reporter, + getClosestPackage: this._getClosestPackage.bind(this), + }, _opts.platforms); } /** @@ -180,7 +192,7 @@ class DependencyGraph extends EventEmitter { } getAllModules() { - return this.load().then(() => this._moduleCache.getAllModules()); + return Promise.resolve(this._moduleCache.getAllModules()); } getDependencies({ @@ -195,42 +207,40 @@ class DependencyGraph extends EventEmitter { transformOptions: TransformOptions, onProgress?: ?(finishedModules: number, totalModules: number) => mixed, recursive: boolean, - }) { - return this.load().then(() => { - platform = this._getRequestPlatform(entryPath, platform); - const absPath = this._getAbsolutePath(entryPath); - const dirExists = filePath => { - try { - return fs.lstatSync(filePath).isDirectory(); - } catch (e) {} - return false; - }; - const req = new ResolutionRequest({ - dirExists, - entryPath: absPath, - extraNodeModules: this._opts.extraNodeModules, - hasteFS: this._hasteFS, - helpers: this._helpers, - moduleCache: this._moduleCache, - moduleMap: this._moduleMap, - platform, - platforms: this._opts.platforms, - preferNativePlatform: this._opts.preferNativePlatform, - }); - - const response = new ResolutionResponse({transformOptions}); - - return req.getOrderedDependencies({ - response, - transformOptions, - onProgress, - recursive, - }).then(() => response); + }): Promise { + platform = this._getRequestPlatform(entryPath, platform); + const absPath = this._getAbsolutePath(entryPath); + const dirExists = filePath => { + try { + return fs.lstatSync(filePath).isDirectory(); + } catch (e) {} + return false; + }; + const req = new ResolutionRequest({ + dirExists, + entryPath: absPath, + extraNodeModules: this._opts.extraNodeModules, + hasteFS: this._hasteFS, + helpers: this._helpers, + moduleCache: this._moduleCache, + moduleMap: this._moduleMap, + platform, + platforms: this._opts.platforms, + preferNativePlatform: this._opts.preferNativePlatform, }); + + const response = new ResolutionResponse({transformOptions}); + + return req.getOrderedDependencies({ + response, + transformOptions, + onProgress, + recursive, + }).then(() => response); } matchFilesByPattern(pattern: RegExp) { - return this.load().then(() => this._hasteFS.matchFiles(pattern)); + return Promise.resolve(this._hasteFS.matchFiles(pattern)); } _getRequestPlatform(entryPath: string, platform: string) {