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.