diff --git a/react-packager/src/Bundler/Bundle.js b/react-packager/src/Bundler/Bundle.js index 4f54e3be..f02127f3 100644 --- a/react-packager/src/Bundler/Bundle.js +++ b/react-packager/src/Bundler/Bundle.js @@ -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, }; diff --git a/react-packager/src/Bundler/BundleBase.js b/react-packager/src/Bundler/BundleBase.js index 635f87e7..b9e1cbe3 100644 --- a/react-packager/src/Bundler/BundleBase.js +++ b/react-packager/src/Bundler/BundleBase.js @@ -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); diff --git a/react-packager/src/Bundler/__tests__/Bundle-test.js b/react-packager/src/Bundler/__tests__/Bundle-test.js index b82f4a6c..5e5fca22 100644 --- a/react-packager/src/Bundler/__tests__/Bundle-test.js +++ b/react-packager/src/Bundler/__tests__/Bundle-test.js @@ -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); + }); + }); });