[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.
This commit is contained in:
Martín Bigio 2015-08-14 12:10:13 -07:00
parent 76e37c4423
commit 0623371814
2 changed files with 46 additions and 2 deletions

View File

@ -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);
});
});
}); });
}); });

View File

@ -26,6 +26,8 @@ class BundlesLayout {
constructor(options) { constructor(options) {
const opts = validateOpts(options); const opts = validateOpts(options);
this._resolver = opts.dependencyResolver; this._resolver = opts.dependencyResolver;
this._moduleToBundle = Object.create(null);
} }
generateLayout(entryPaths, isDev) { generateLayout(entryPaths, isDev) {
@ -44,9 +46,10 @@ class BundlesLayout {
.then(modulesDeps => { .then(modulesDeps => {
let syncDependencies = Object.create(null); let syncDependencies = Object.create(null);
modulesDeps.forEach(moduleDeps => { modulesDeps.forEach(moduleDeps => {
moduleDeps.dependencies.forEach(dep => moduleDeps.dependencies.forEach(dep => {
syncDependencies[dep.path] = dep syncDependencies[dep.path] = dep
); this._moduleToBundle[dep.path] = bundles.length;
});
pending = pending.concat(moduleDeps.asyncDependencies); 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. // Runs the body Promise meanwhile the condition callback is satisfied.