This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Make the order of sub-dependencies deterministic. #57
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
*/ | ||
'use strict'; | ||
|
||
const AsyncTaskGroup = require('../lib/AsyncTaskGroup'); | ||
const MapWithDefaults = require('../lib/MapWithDefaults'); | ||
const debug = require('debug')('ReactNativePackager:DependencyGraph'); | ||
const util = require('util'); | ||
const path = require('../fastpath'); | ||
|
@@ -120,86 +122,127 @@ class ResolutionRequest { | |
return this._getAllMocks(mocksPattern).then(allMocks => { | ||
const entry = this._moduleCache.getModule(this._entryPath); | ||
const mocks = Object.create(null); | ||
const visited = Object.create(null); | ||
visited[entry.hash()] = true; | ||
|
||
response.pushDependency(entry); | ||
let totalModules = 1; | ||
let finishedModules = 0; | ||
|
||
const collect = (mod) => { | ||
return getDependencies(mod, transformOptions).then( | ||
depNames => Promise.all( | ||
depNames.map(name => this.resolveDependency(mod, name)) | ||
).then((dependencies) => [depNames, dependencies]) | ||
).then(([depNames, dependencies]) => { | ||
if (allMocks) { | ||
const list = [mod.getName()]; | ||
const pkg = mod.getPackage(); | ||
if (pkg) { | ||
list.push(pkg.getName()); | ||
} | ||
return Promise.all(list).then(names => { | ||
names.forEach(name => { | ||
if (allMocks[name] && !mocks[name]) { | ||
const mockModule = | ||
this._moduleCache.getModule(allMocks[name]); | ||
depNames.push(name); | ||
dependencies.push(mockModule); | ||
mocks[name] = allMocks[name]; | ||
} | ||
}); | ||
return [depNames, dependencies]; | ||
}); | ||
const resolveDependencies = module => | ||
getDependencies(module, transformOptions) | ||
.then(dependencyNames => | ||
Promise.all( | ||
dependencyNames.map(name => this.resolveDependency(module, name)) | ||
).then(dependencies => [dependencyNames, dependencies]) | ||
); | ||
|
||
const addMockDependencies = !allMocks | ||
? (module, result) => result | ||
: (module, [dependencyNames, dependencies]) => { | ||
const list = [module.getName()]; | ||
const pkg = module.getPackage(); | ||
if (pkg) { | ||
list.push(pkg.getName()); | ||
} | ||
return [depNames, dependencies]; | ||
}).then(([depNames, dependencies]) => { | ||
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 (allMocks && allMocks[name] && !mocks[name]) { | ||
return Promise.all(list).then(names => { | ||
names.forEach(name => { | ||
if (allMocks[name] && !mocks[name]) { | ||
const mockModule = this._moduleCache.getModule(allMocks[name]); | ||
dependencyNames.push(name); | ||
dependencies.push(mockModule); | ||
mocks[name] = allMocks[name]; | ||
return filteredPairs.push([name, mockModule]); | ||
} | ||
|
||
debug( | ||
'WARNING: Cannot find required module `%s` from module `%s`', | ||
name, | ||
mod.path | ||
); | ||
return false; | ||
} | ||
return filteredPairs.push([name, modDep]); | ||
}); | ||
return [dependencyNames, dependencies]; | ||
}); | ||
}; | ||
|
||
const collectedDependencies = new MapWithDefaults(module => collect(module)); | ||
const crawlDependencies = (mod, [depNames, dependencies]) => { | ||
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 (allMocks && allMocks[name] && !mocks[name]) { | ||
const mockModule = this._moduleCache.getModule(allMocks[name]); | ||
mocks[name] = allMocks[name]; | ||
return filteredPairs.push([name, mockModule]); | ||
} | ||
|
||
response.setResolvedDependencyPairs(mod, filteredPairs); | ||
|
||
const newDependencies = | ||
filteredPairs.filter(([, modDep]) => !visited[modDep.hash()]); | ||
|
||
if (onProgress) { | ||
finishedModules += 1; | ||
totalModules += newDependencies.length; | ||
onProgress(finishedModules, totalModules); | ||
debug( | ||
'WARNING: Cannot find required module `%s` from module `%s`', | ||
name, | ||
mod.path | ||
); | ||
return false; | ||
} | ||
return Promise.all( | ||
newDependencies.map(([depName, modDep]) => { | ||
visited[modDep.hash()] = true; | ||
return Promise.all([modDep, recursive ? collect(modDep) : []]); | ||
}) | ||
); | ||
return filteredPairs.push([name, modDep]); | ||
}); | ||
|
||
response.setResolvedDependencyPairs(mod, filteredPairs); | ||
|
||
const dependencyModules = filteredPairs.map(([, m]) => m); | ||
const newDependencies = | ||
dependencyModules.filter(m => !collectedDependencies.has(m)); | ||
|
||
if (onProgress) { | ||
finishedModules += 1; | ||
totalModules += newDependencies.length; | ||
onProgress(finishedModules, totalModules); | ||
} | ||
|
||
if (recursive) { | ||
// doesn't block the return of this function invocation, but defers | ||
// the resulution of collectionsInProgress.done.then(…) | ||
dependencyModules | ||
.forEach(dependency => collectedDependencies.get(dependency)); | ||
} | ||
return dependencyModules; | ||
}; | ||
|
||
return collect(entry).then(deps => { | ||
recursiveFlatten(deps).forEach(dep => response.pushDependency(dep)); | ||
const collectionsInProgress = new AsyncTaskGroup(); | ||
function collect(module) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: should this function be defined above with the other ones before it's invoked for clarity? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It calls the other ones, so there’s a circle |
||
collectionsInProgress.start(module); | ||
return resolveDependencies(module) | ||
.then(result => addMockDependencies(module, result)) | ||
.then(result => crawlDependencies(module, result)) | ||
.then(result => { | ||
collectionsInProgress.end(module); | ||
return result; | ||
}); | ||
} | ||
|
||
return Promise.all([ | ||
// kicks off recursive dependency discovery, but doesn't block until it's done | ||
collectedDependencies.get(entry), | ||
|
||
// resolves when there are no more modules resolving dependencies | ||
collectionsInProgress.done, | ||
]).then(([rootDependencies]) => { | ||
return Promise.all( | ||
Array.from(collectedDependencies, resolveKeyWithPromise) | ||
).then(moduleToDependenciesPairs => | ||
[rootDependencies, new MapWithDefaults(() => [], moduleToDependenciesPairs)] | ||
); | ||
}).then(([rootDependencies, moduleDependencies]) => { | ||
// serialize dependencies, and make sure that every single one is only | ||
// included once | ||
const seen = new Set([entry]); | ||
function traverse(dependencies) { | ||
dependencies.forEach(dependency => { | ||
if (seen.has(dependency)) { return; } | ||
|
||
seen.add(dependency); | ||
response.pushDependency(dependency); | ||
traverse(moduleDependencies.get(dependency)); | ||
}); | ||
} | ||
|
||
traverse(rootDependencies); | ||
response.setMocks(mocks); | ||
}); | ||
}); | ||
|
@@ -460,11 +503,8 @@ function normalizePath(modulePath) { | |
return modulePath.replace(/\/$/, ''); | ||
} | ||
|
||
function recursiveFlatten(array) { | ||
return Array.prototype.concat.apply( | ||
Array.prototype, | ||
array.map(item => Array.isArray(item) ? recursiveFlatten(item) : item) | ||
); | ||
function resolveKeyWithPromise([key, promise]) { | ||
return promise.then(value => [key, value]); | ||
} | ||
|
||
module.exports = ResolutionRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Copyright (c) 2016-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
|
||
module.exports = class AsyncTaskGroup { | ||
constructor() { | ||
this._runningTasks = new Set(); | ||
this._resolve = null; | ||
this.done = new Promise(resolve => this._resolve = resolve); | ||
} | ||
|
||
start(taskHandle) { | ||
this._runningTasks.add(taskHandle); | ||
} | ||
|
||
end(taskHandle) { | ||
const runningTasks = this._runningTasks; | ||
if (runningTasks.delete(taskHandle) && runningTasks.size === 0) { | ||
this._resolve(); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Copyright (c) 2016-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
|
||
module.exports = function MapWithDefaults(factory, iterable) { | ||
// This can't be `MapWithDefaults extends Map`, b/c the way babel transforms | ||
// super calls in constructors: Map.call(this, iterable) throws for native | ||
// Map objects in node 4+. | ||
// TODO(davidaurelio) switch to a transform that does not transform classes | ||
// and super calls, and change this into a class | ||
|
||
const map = iterable ? new Map(iterable) : new Map(); | ||
const {get} = map; | ||
map.get = key => { | ||
if (map.has(key)) { | ||
return get.call(map, key); | ||
} | ||
|
||
const value = factory(key); | ||
map.set(key, value); | ||
return value; | ||
}; | ||
return map; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fancy.