From 06233718145ac7cc009102dcc43903a6a967965f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Bigio?= Date: Fri, 14 Aug 2015 12:10:13 -0700 Subject: [PATCH] [react-packager] Cache in which bundle is each module Summary: Not that at the moment a module can be present in multiple bundles, so the new API will return only one of them. In the near future we'll impose the invariant that a module can only be present in a single bundle so this API will return the exact bundle in which it is. --- .../__tests__/BundlesLayout-test.js | 37 +++++++++++++++++++ .../react-packager/src/BundlesLayout/index.js | 11 +++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/packager/react-packager/src/BundlesLayout/__tests__/BundlesLayout-test.js b/packager/react-packager/src/BundlesLayout/__tests__/BundlesLayout-test.js index 154a29c35..fce23226a 100644 --- a/packager/react-packager/src/BundlesLayout/__tests__/BundlesLayout-test.js +++ b/packager/react-packager/src/BundlesLayout/__tests__/BundlesLayout-test.js @@ -146,5 +146,42 @@ describe('BundlesLayout', () => { ]) ); }); + + pit('separate cache in which bundle is each dependency', () => { + DependencyResolver.prototype.getDependencies.mockImpl((path) => { + switch (path) { + case '/root/index.js': + return Promise.resolve({ + dependencies: [dep('/root/index.js'), dep('/root/a.js')], + asyncDependencies: [['/root/b.js']], + }); + case '/root/a.js': + return Promise.resolve({ + dependencies: [dep('/root/a.js')], + asyncDependencies: [], + }); + case '/root/b.js': + return Promise.resolve({ + dependencies: [dep('/root/b.js')], + asyncDependencies: [['/root/c.js']], + }); + case '/root/c.js': + return Promise.resolve({ + dependencies: [dep('/root/c.js')], + asyncDependencies: [], + }); + default: + throw 'Undefined path: ' + path; + } + }); + + var layout = newBundlesLayout(); + return layout.generateLayout(['/root/index.js']).then(() => { + expect(layout.getBundleIDForModule('/root/index.js')).toBe(0); + expect(layout.getBundleIDForModule('/root/a.js')).toBe(0); + expect(layout.getBundleIDForModule('/root/b.js')).toBe(1); + expect(layout.getBundleIDForModule('/root/c.js')).toBe(2); + }); + }); }); }); diff --git a/packager/react-packager/src/BundlesLayout/index.js b/packager/react-packager/src/BundlesLayout/index.js index 88e616c00..165a33b34 100644 --- a/packager/react-packager/src/BundlesLayout/index.js +++ b/packager/react-packager/src/BundlesLayout/index.js @@ -26,6 +26,8 @@ class BundlesLayout { constructor(options) { const opts = validateOpts(options); this._resolver = opts.dependencyResolver; + + this._moduleToBundle = Object.create(null); } generateLayout(entryPaths, isDev) { @@ -44,9 +46,10 @@ class BundlesLayout { .then(modulesDeps => { let syncDependencies = Object.create(null); modulesDeps.forEach(moduleDeps => { - moduleDeps.dependencies.forEach(dep => + moduleDeps.dependencies.forEach(dep => { syncDependencies[dep.path] = dep - ); + this._moduleToBundle[dep.path] = bundles.length; + }); pending = pending.concat(moduleDeps.asyncDependencies); }); @@ -60,6 +63,10 @@ class BundlesLayout { }, ); } + + getBundleIDForModule(path) { + return this._moduleToBundle[path]; + } } // Runs the body Promise meanwhile the condition callback is satisfied.