Distinguish between module ID and name

Reviewed By: martinbigio

Differential Revision: D2854975

fb-gh-sync-id: 8c75037d5d7f6155369d4ec581158ebabf445bac
This commit is contained in:
David Aurelio 2016-01-22 10:39:13 -08:00 committed by facebook-github-bot-4
parent 153989bcd3
commit 741cb48fa2
3 changed files with 41 additions and 7 deletions

View File

@ -341,14 +341,11 @@ class Bundle extends BundleBase {
}
toJSON() {
if (!this._finalized) {
throw new Error('Cannot serialize bundle unless finalized');
}
this.assertFinalized('Cannot serialize bundle unless finalized');
return {
...super.toJSON(),
sourceMapUrl: this._sourceMapUrl,
mainModuleId: super.getMainModuleId(),
numPrependedModules: this._numPrependedModules,
numRequireCalls: this._numRequireCalls,
};

View File

@ -16,6 +16,7 @@ class BundleBase {
this._finalized = false;
this._modules = [];
this._assets = [];
this._mainModuleId = this._mainModuleName = undefined;
}
getMainModuleId() {
@ -26,6 +27,14 @@ class BundleBase {
this._mainModuleId = moduleId;
}
getMainModuleName() {
return this._mainModuleName;
}
setMainModuleName(moduleName) {
this._mainModuleName = moduleName;
}
addModule(module) {
if (!module instanceof ModuleTransport) {
throw new Error('Expeceted a ModuleTransport object');
@ -65,9 +74,9 @@ class BundleBase {
return this._source;
}
assertFinalized() {
assertFinalized(message) {
if (!this._finalized) {
throw new Error('Bundle needs to be finalized before getting any source');
throw new Error(message || 'Bundle needs to be finalized before getting any source');
}
}
@ -75,13 +84,16 @@ class BundleBase {
return {
modules: this._modules,
assets: this._assets,
mainModuleId: this.getMainModuleId(),
mainModuleName: this.getMainModuleName(),
};
}
static fromJSON(bundle, json) {
bundle._assets = json.assets;
bundle._modules = json.modules;
bundle._mainModuleId = json.mainModuleId;
bundle.setMainModuleId(json.mainModuleId);
bundle.setMainModuleName(json.mainModuleName);
Object.freeze(bundle._modules);
Object.seal(bundle._modules);

View File

@ -311,6 +311,31 @@ describe('Bundle', () => {
expect(bundle.getEtag()).toEqual(eTag);
});
});
describe('main module name and id:', function() {
it('keeps distinct module names and IDs', function() {
const id = 'arbitrary module ID';
const name = 'arbitrary module name';
bundle.setMainModuleId(id);
bundle.setMainModuleName(name);
expect(bundle.getMainModuleId()).toEqual(id);
expect(bundle.getMainModuleName()).toEqual(name);
});
it('can serialize and deserialize module ID and name', function() {
const id = 'arbitrary module ID';
const name = 'arbitrary module name';
bundle.setMainModuleId(id);
bundle.setMainModuleName(name);
bundle.finalize({});
const deserialized = Bundle.fromJSON(bundle.toJSON());
expect(deserialized.getMainModuleId()).toEqual(id);
expect(deserialized.getMainModuleName()).toEqual(name);
});
});
});