Make sure that modules not present in the dependency graph are initially required

Reviewed By: davidaurelio

Differential Revision: D6071529

fbshipit-source-id: 916768e85657207d1f7a2bf41d0aa11abd158ee7
This commit is contained in:
Rafael Oleza 2017-10-18 12:17:11 -07:00
parent a548ba18de
commit 17d07ff9ea
3 changed files with 54 additions and 2 deletions

View File

@ -99,6 +99,9 @@ describe('Bundler', function() {
return {
getDependencies,
getModuleSystemDependencies,
getModuleForPath(path) {
return {path};
},
};
});
Resolver.load = jest
@ -152,7 +155,7 @@ describe('Bundler', function() {
mainModuleId: 'foo',
dependencies: modules,
options: transformOptions,
getModuleId: () => 123,
getModuleId: ({path}) => path,
getResolvedDependencyPairs: () => [],
}),
);
@ -290,6 +293,44 @@ describe('Bundler', function() {
});
});
it('passes runBeforeMainModule correctly to finalize', function() {
return bundler
.bundle({
entryFile: '/root/foo.js',
runBeforeMainModule: ['/root/bar.js'],
runModule: true,
sourceMapUrl: 'source_map_url',
})
.then(bundle => {
expect(bundle.finalize.mock.calls[0]).toEqual([
{
runModule: true,
runBeforeMainModule: ['/root/bar.js'],
allowUpdates: false,
},
]);
});
});
it('ignores runBeforeMainModule when it is not part of the bundle', function() {
return bundler
.bundle({
entryFile: '/root/foo.js',
runBeforeMainModule: ['/root/not-valid.js'],
runModule: true,
sourceMapUrl: 'source_map_url',
})
.then(bundle => {
expect(bundle.finalize.mock.calls[0]).toEqual([
{
runModule: true,
runBeforeMainModule: [],
allowUpdates: false,
},
]);
});
});
it('loads and runs asset plugins', function() {
jest.mock(
'mockPlugin1',
@ -381,7 +422,7 @@ describe('Bundler', function() {
modules.map(x =>
objectContaining({
name: any(String),
id: any(Number),
id: any(String), // Our mock of getModuleId returns a string
code: any(String),
sourceCode: any(String),
sourcePath: x.path,

View File

@ -412,10 +412,12 @@ class Bundler {
bundle: finalBundle,
transformedModules,
response,
modulesByPath,
}: {
bundle: Bundle,
transformedModules: Array<{module: Module, transformed: ModuleTransport}>,
response: ResolutionResponse<Module, BundlingOptions>,
modulesByPath: {[path: string]: Module},
}) =>
this._resolverPromise
.then(resolver =>
@ -433,6 +435,10 @@ class Bundler {
);
})
.then(runBeforeMainModules => {
runBeforeMainModules = runBeforeMainModules
.map(module => modulesByPath[module.path])
.filter(Boolean);
finalBundle.finalize({
runModule,
runBeforeMainModule: runBeforeMainModules.map(module =>
@ -488,6 +494,8 @@ class Bundler {
}),
);
const modulesByPath = Object.create(null);
if (!resolutionResponse) {
resolutionResponse = this.getDependencies({
entryFile,
@ -545,6 +553,7 @@ class Bundler {
dependencyPairs: response.getResolvedDependencyPairs(module),
}).then(transformed => {
modulesByTransport.set(transformed, module);
modulesByPath[module.path] = module;
onModuleTransformed({
module,
response,
@ -570,6 +579,7 @@ class Bundler {
bundle,
transformedModules,
response,
modulesByPath,
});
})
.then(() => bundle);

View File

@ -266,6 +266,7 @@ class DeltaTransformer extends EventEmitter {
this._bundleOptions.runBeforeMainModule
.map(path => this._resolver.getModuleForPath(path))
.concat(entryPointModule)
.filter(module => dependencyEdges.has(module.path))
.map(this._getModuleId)
.map(moduleId => {
const code = `;require(${JSON.stringify(moduleId)})`;