mirror of https://github.com/status-im/metro.git
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:
parent
4a5cbbbec8
commit
c17a8e48cf
|
@ -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}));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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,
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue