mirror of https://github.com/status-im/metro.git
Fix shallow dependency resolution
Summary: The feature added in D2862850 only adds the module that requests shallow dependency resolution to the dependencies of a module. The reason why this is happens is because `collect` calls `response.addDependency` but if `recursive` is set to `false`, dependencies don't call `collect` and therefore don't get added to the resolution response. This fixes it by adding dependencies outside of the `collect` call. public Reviewed By: davidaurelio Differential Revision: D2885152 fb-gh-sync-id: 8ab3b6c6b7cd45d59615a99ac87984a41b5d7025
This commit is contained in:
parent
1a547cc2b1
commit
c620ddd71d
|
@ -110,8 +110,8 @@ class ResolutionRequest {
|
||||||
const visited = Object.create(null);
|
const visited = Object.create(null);
|
||||||
visited[entry.hash()] = true;
|
visited[entry.hash()] = true;
|
||||||
|
|
||||||
|
response.pushDependency(entry);
|
||||||
const collect = (mod) => {
|
const collect = (mod) => {
|
||||||
response.pushDependency(mod);
|
|
||||||
return mod.getDependencies().then(
|
return mod.getDependencies().then(
|
||||||
depNames => Promise.all(
|
depNames => Promise.all(
|
||||||
depNames.map(name => this.resolveDependency(mod, name))
|
depNames.map(name => this.resolveDependency(mod, name))
|
||||||
|
@ -163,6 +163,7 @@ class ResolutionRequest {
|
||||||
p = p.then(() => {
|
p = p.then(() => {
|
||||||
if (!visited[modDep.hash()]) {
|
if (!visited[modDep.hash()]) {
|
||||||
visited[modDep.hash()] = true;
|
visited[modDep.hash()] = true;
|
||||||
|
response.pushDependency(modDep);
|
||||||
if (recursive) {
|
if (recursive) {
|
||||||
return collect(modDep);
|
return collect(modDep);
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,8 +21,8 @@ const mocksPattern = /(?:[\\/]|^)__mocks__[\\/]([^\/]+)\.js$/;
|
||||||
describe('DependencyGraph', function() {
|
describe('DependencyGraph', function() {
|
||||||
let defaults;
|
let defaults;
|
||||||
|
|
||||||
function getOrderedDependenciesAsJSON(dgraph, entry, platform) {
|
function getOrderedDependenciesAsJSON(dgraph, entry, platform, recursive = true) {
|
||||||
return dgraph.getDependencies(entry, platform)
|
return dgraph.getDependencies(entry, platform, recursive)
|
||||||
.then(response => response.finalize())
|
.then(response => response.finalize())
|
||||||
.then(({ dependencies }) => Promise.all(dependencies.map(dep => Promise.all([
|
.then(({ dependencies }) => Promise.all(dependencies.map(dep => Promise.all([
|
||||||
dep.getName(),
|
dep.getName(),
|
||||||
|
@ -89,6 +89,12 @@ describe('DependencyGraph', function() {
|
||||||
'/**',
|
'/**',
|
||||||
' * @providesModule a',
|
' * @providesModule a',
|
||||||
' */',
|
' */',
|
||||||
|
'require("b")',
|
||||||
|
].join('\n'),
|
||||||
|
'b.js': [
|
||||||
|
'/**',
|
||||||
|
' * @providesModule b',
|
||||||
|
' */',
|
||||||
].join('\n'),
|
].join('\n'),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -114,6 +120,17 @@ describe('DependencyGraph', function() {
|
||||||
{
|
{
|
||||||
id: 'a',
|
id: 'a',
|
||||||
path: '/root/a.js',
|
path: '/root/a.js',
|
||||||
|
dependencies: ['b'],
|
||||||
|
isAsset: false,
|
||||||
|
isAsset_DEPRECATED: false,
|
||||||
|
isJSON: false,
|
||||||
|
isPolyfill: false,
|
||||||
|
resolution: undefined,
|
||||||
|
resolveDependency: undefined,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'b',
|
||||||
|
path: '/root/b.js',
|
||||||
dependencies: [],
|
dependencies: [],
|
||||||
isAsset: false,
|
isAsset: false,
|
||||||
isAsset_DEPRECATED: false,
|
isAsset_DEPRECATED: false,
|
||||||
|
@ -126,6 +143,61 @@ describe('DependencyGraph', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
pit('should get shallow dependencies', function() {
|
||||||
|
var root = '/root';
|
||||||
|
fs.__setMockFilesystem({
|
||||||
|
'root': {
|
||||||
|
'index.js': [
|
||||||
|
'/**',
|
||||||
|
' * @providesModule index',
|
||||||
|
' */',
|
||||||
|
'require("a")',
|
||||||
|
].join('\n'),
|
||||||
|
'a.js': [
|
||||||
|
'/**',
|
||||||
|
' * @providesModule a',
|
||||||
|
' */',
|
||||||
|
'require("b")',
|
||||||
|
].join('\n'),
|
||||||
|
'b.js': [
|
||||||
|
'/**',
|
||||||
|
' * @providesModule b',
|
||||||
|
' */',
|
||||||
|
].join('\n'),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
var dgraph = new DependencyGraph({
|
||||||
|
...defaults,
|
||||||
|
roots: [root],
|
||||||
|
});
|
||||||
|
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js', null, false).then(function(deps) {
|
||||||
|
expect(deps)
|
||||||
|
.toEqual([
|
||||||
|
{
|
||||||
|
id: 'index',
|
||||||
|
path: '/root/index.js',
|
||||||
|
dependencies: ['a'],
|
||||||
|
isAsset: false,
|
||||||
|
isAsset_DEPRECATED: false,
|
||||||
|
isJSON: false,
|
||||||
|
isPolyfill: false,
|
||||||
|
resolution: undefined,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'a',
|
||||||
|
path: '/root/a.js',
|
||||||
|
dependencies: ['b'],
|
||||||
|
isAsset: false,
|
||||||
|
isAsset_DEPRECATED: false,
|
||||||
|
isJSON: false,
|
||||||
|
isPolyfill: false,
|
||||||
|
resolution: undefined,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
pit('should get dependencies with the correct extensions', function() {
|
pit('should get dependencies with the correct extensions', function() {
|
||||||
var root = '/root';
|
var root = '/root';
|
||||||
fs.__setMockFilesystem({
|
fs.__setMockFilesystem({
|
||||||
|
|
Loading…
Reference in New Issue