Skip to content

Commit

Permalink
Merge pull request #772 from stealjs/circular
Browse files Browse the repository at this point in the history
Fail the build if circular dependencies are found
  • Loading branch information
Manuel Mujica committed Jun 30, 2017
2 parents ff29ba1 + f28f742 commit a5a7b4f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
40 changes: 40 additions & 0 deletions lib/slim/checks/circular_dependencies.js
@@ -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;
}
2 changes: 2 additions & 0 deletions lib/stream/check_slim_support.js
Expand Up @@ -4,6 +4,7 @@ var through = require("through2");
var checkStealAndLoader = require("../slim/checks/steal_and_loader");
var checkStealConditional = require("../slim/checks/steal_conditional");
var checkProductionEnvConfig = require("../slim/checks/production_env_config");
var checkCircularDependencies = require("../slim/checks/circular_dependencies");

module.exports = function() {
return through.obj(function(data, enc, done) {
Expand All @@ -21,6 +22,7 @@ function checkSupport(data) {
checkStealAndLoader(configMain, data.graph);
checkStealConditional(data.graph[configMain]);
checkProductionEnvConfig(data.steal);
checkCircularDependencies(data.graph);

return data;
}
29 changes: 25 additions & 4 deletions test/slim_build_test.js
Expand Up @@ -267,10 +267,14 @@ describe("slim builds", function() {
return Promise.all([args.close, find(args.browser, "_props")]);
})
.then(function(data) {
assert.deepEqual(data[1], {
foo: "foo",
bar: "bar"
}, "module cache works");
assert.deepEqual(
data[1],
{
foo: "foo",
bar: "bar"
},
"module cache works"
);
data[0]();
});
});
Expand Down Expand Up @@ -336,4 +340,21 @@ describe("slim builds", function() {
data[0]();
});
});

it("errors out with circular dependencies", function(done) {
var base = path.join(__dirname, "circular");
var config = { config: path.join(base, "package.json!npm") };

rmdir(path.join(base, "dist"))
.then(function() {
return optimize(config, { minify: false, quiet: true });
})
.then(function() {
done(new Error("should not build the app"));
})
.catch(function(err) {
assert(/Cannot create slim build/.test(err.message));
done();
});
});
});

0 comments on commit a5a7b4f

Please sign in to comment.