Support "free" mocks.

Summary:
It's possible that a mock doesn't have an associated real module that it maps to. This is actually very common in www, where we have JS mocks for dynamic PHP JS modules. The implementation I chose seems like the smartest one for now: if a module cannot be resolved, we look up whether we have a mock with the same id. If we do, we just resolve it. That's it! And it also only does the minimum amount of resolution necessary.

public

Reviewed By: martinbigio

Differential Revision: D2822277

fb-gh-sync-id: 7c9fbb6f69a0c0c85157c0650f5719d94a02410e
This commit is contained in:
Christoph Pojer
2016-01-12 15:33:00 -08:00
committed by facebook-github-bot-1
parent e2641a237a
commit 801b83da2d
2 changed files with 88 additions and 10 deletions

View File

@@ -135,15 +135,25 @@ class ResolutionRequest {
const filteredPairs = [];
dependencies.forEach((modDep, i) => {
const name = depNames[i];
if (modDep == null) {
// It is possible to require mocks that don't have a real
// module backing them. If a dependency cannot be found but there
// exists a mock with the desired ID, resolve it and add it as
// a dependency.
if (mocks && mocks[name]) {
const mockModule = this._moduleCache.getModule(mocks[name]);
return filteredPairs.push([name, mockModule]);
}
debug(
'WARNING: Cannot find required module `%s` from module `%s`',
depNames[i],
name,
mod.path
);
return false;
}
return filteredPairs.push([depNames[i], modDep]);
return filteredPairs.push([name, modDep]);
});
response.setResolvedDependencyPairs(mod, filteredPairs);