diff --git a/index.js b/index.js index ec0f634..245499b 100644 --- a/index.js +++ b/index.js @@ -4,12 +4,14 @@ var fs = require('fs'), path = require('path'), gutil = require('gulp-util'), _ = require('lodash'), - Stream = require('stream'); + Stream = require('stream'), + DAG = require('dag'); var PLUGIN_NAME = 'gulp-resolve-dependencies'; function resolveDependencies(config) { var stream, + dag = new DAG(), fileCache = [], filesReturned = [], getFiles = function(targetFile) { @@ -34,9 +36,10 @@ function resolveDependencies(config) { while (match = pattern.exec(content)) { filePath = path.join(path.dirname(targetFile.path), match[1]); - // Skip if already added to dependencies - if (_.indexOf(fileCache, filePath) !== -1) { - continue; + try { + dag.addEdge(targetFile.path, filePath); + } catch (ex) { + stream.emit('error', new Error('circular file 1: ' + targetFile.path + ' file 2: ' + filePath)); } // Check existence diff --git a/package.json b/package.json index 27389a7..7cc1628 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gulp-resolve-dependencies", - "version": "1.0.3", + "version": "1.1.0", "description": "Resolve dependency directives in assets (e.g. \"@requires\" or \"//= require\" in JavaScript)", "license": "MIT", "repository": "backflip/gulp-resolve-dependencies", @@ -9,7 +9,8 @@ "node": ">=0.10.0" }, "scripts": { - "test": "mocha" + "pretest": "npm i", + "test": "./node_modules/mocha/bin/mocha" }, "main": "./index.js", "keywords": [ @@ -25,12 +26,14 @@ "sprockets" ], "dependencies": { + "dag": "0.0.1", "gulp-util": "~3.0.1", "lodash": "~2.4.1" }, "devDependencies": { - "mocha": "~2.0.1", "event-stream": "~3.1.7", - "gulp": "~3.8.10" + "gulp": "~3.8.10", + "gulp-concat": "^2.4.3", + "mocha": "~2.0.1" } } diff --git a/test/circular/a.js b/test/circular/a.js new file mode 100644 index 0000000..620cd70 --- /dev/null +++ b/test/circular/a.js @@ -0,0 +1,5 @@ +/** + * @requires ../circular/b.js + * @requires ../circular/c.js + */ +module.exports = 'A'; diff --git a/test/circular/b.js b/test/circular/b.js new file mode 100644 index 0000000..33f979b --- /dev/null +++ b/test/circular/b.js @@ -0,0 +1,4 @@ +/** + * @requires ../circular/c.js + */ +module.exports = 'B'; diff --git a/test/circular/c.js b/test/circular/c.js new file mode 100644 index 0000000..83e09cd --- /dev/null +++ b/test/circular/c.js @@ -0,0 +1,4 @@ +/** + * @requires ../circular/b.js + */ +module.exports = 'C'; diff --git a/test/main.js b/test/main.js index 7ff797b..0812dc0 100755 --- a/test/main.js +++ b/test/main.js @@ -23,4 +23,12 @@ describe('gulp-resolve-dependencies', function() { done(); })); }); + + it('should throw error if hit circular dependency', function(done) { + gulp.src(__dirname + '/circular/a.js') + .pipe(resolveDependencies()) + .on('error', function () { + done(); + }); + }); });