Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #772 from stealjs/circular
Fail the build if circular dependencies are found
- Loading branch information
Showing
3 changed files
with
67 additions
and
4 deletions.
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
var some = require("lodash/some"); | ||
var values = require("lodash/values"); | ||
var includes = require("lodash/includes"); | ||
|
||
/** | ||
* Checks whether there are circular dependencies in the graph | ||
* @param {Object} steal - The dependency graph object | ||
* @throws if circular dependencies are found | ||
*/ | ||
module.exports = function(graph) { | ||
var nodes = values(graph); | ||
|
||
if (some(nodes, hasCircularDependencies)) { | ||
throw new Error( | ||
`Cannot create slim build. Circular dependencies are not supported` | ||
); | ||
} | ||
}; | ||
|
||
/** | ||
* Whether the node has a circular dependency | ||
* @param {Object} - A node from the dependency graph | ||
* @param {number} - The index of the node in the (array) graph | ||
* @param {Array.[Object]} - A list of nodes | ||
* @return `true` if a circular dependency is found | ||
*/ | ||
function hasCircularDependencies(node, index, nodes) { | ||
for (var i = index + 1; i < nodes.length; i += 1) { | ||
var nextNode = nodes[i]; | ||
|
||
if ( | ||
includes(node.dependencies, nextNode.load.name) && | ||
includes(nextNode.dependencies, node.load.name) | ||
) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} |
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