Make order of bundle transports deterministic

Summary:After starting to minify off the main process, the order of module transport objects in `Bundle` instances became less deterministic.
These changes guarantee that module transports appear in addition order, not in order of minification completion.

Reviewed By: bestander

Differential Revision: D3029588

fb-gh-sync-id: 80e83c05d7f78ed7e69583d7e3aa2831bd5ae4d0
shipit-source-id: 80e83c05d7f78ed7e69583d7e3aa2831bd5ae4d0
This commit is contained in:
David Aurelio 2016-03-09 08:29:19 -08:00 committed by Facebook Github Bot 7
parent 4a5cbbbec8
commit c17a8e48cf
3 changed files with 53 additions and 4 deletions

View File

@ -31,6 +31,7 @@ class Bundle extends BundleBase {
}
addModule(resolver, resolutionResponse, module, moduleTransport) {
const index = super.addModule(moduleTransport);
return resolver.wrapModule({
resolutionResponse,
module,
@ -46,7 +47,8 @@ class Bundle extends BundleBase {
this._shouldCombineSourceMaps = true;
}
super.addModule(new ModuleTransport({...moduleTransport, code, map}));
this.replaceModuleAt(
index, new ModuleTransport({...moduleTransport, code, map}));
});
}

View File

@ -31,11 +31,19 @@ class BundleBase {
}
addModule(module) {
if (!module instanceof ModuleTransport) {
if (!(module instanceof ModuleTransport)) {
throw new Error('Expeceted a ModuleTransport object');
}
this._modules.push(module);
return this._modules.push(module) - 1;
}
replaceModuleAt(index, module) {
if (!(module instanceof ModuleTransport)) {
throw new Error('Expeceted a ModuleTransport object');
}
this._modules[index] = module;
}
getModules() {

View File

@ -11,6 +11,7 @@
jest.autoMockOff();
const Bundle = require('../Bundle');
const ModuleTransport = require('../../lib/ModuleTransport');
const Promise = require('Promise');
const SourceMapGenerator = require('source-map').SourceMapGenerator;
const crypto = require('crypto');
@ -106,6 +107,35 @@ describe('Bundle', () => {
].join('\n'));
});
});
fpit('should insert modules in a deterministic order, independent from timing of the wrapping process', () => {
const moduleTransports = [
createModuleTransport({name: 'module1'}),
createModuleTransport({name: 'module2'}),
createModuleTransport({name: 'module3'}),
];
const resolves = {};
const resolver = {
wrapModule({name}) {
return new Promise(resolve => resolves[name] = resolve);
}
};
console.log(bundle.addModule+'')
const promise = Promise.all(
moduleTransports.map(m => bundle.addModule(resolver, null, null, m)))
.then(() => {
expect(bundle.getModules())
.toEqual(moduleTransports);
});
resolves.module2({code: ''});
resolves.module3({code: ''});
resolves.module1({code: ''});
return promise;
});
});
describe('sourcemap bundle', () => {
@ -351,6 +381,15 @@ function addModule({bundle, code, sourceCode, sourcePath, map, virtual}) {
resolverFor(code, map),
null,
null,
{code, sourceCode, sourcePath, map, virtual}
createModuleTransport({code, sourceCode, sourcePath, map, virtual})
);
}
function createModuleTransport(data) {
return new ModuleTransport({
code: '',
sourceCode: '',
sourcePath: '',
...data,
});
}