diff --git a/README.md b/README.md index da0d72f5..402a9565 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ Only required if you want to generate the visual graphs using [Graphviz](http:// - {String} **exclude**. String from which a regex will be constructed for excluding files from the scan. - {Boolean} **breakOnError**. True if the parser should stop on parse errors and when modules are missing, false otherwise. Defaults to false. - {Boolean} **optimized**. True if the parser should read modules from a optimized file (r.js). Defaults to false. +- {Boolean} **findNestedDependencies**. True if nested dependencies should be found in AMD modules. Defaults to false. - {String} **mainRequireModule**. Name of the module if parsing an optimized file (r.js), where the main file used `require()` instead of `define`. Defaults to `''`. - {String} **requireConfig**. Path to RequireJS config used to find shim dependencies and path aliases. Not used by default. - {Function} **onParseFile**. Function to be called when parsing a file (argument will be an object with "filename" and "src" property set). @@ -231,6 +232,7 @@ minimize a global energy function, which is equivalent to statistical multi-dime ## v0.4.0 (December 19, 2014) Add support for JSX (React) and additional module paths (Thanks to Ben Lowery). Fix for detecting presence of AMD or CommonJS modules (Thanks to Aaron Russ). +Added support for option findNestedDependencies to find nested dependencies in AMD modules. ## v0.3.5 (Septemper 22, 2014) Fix issue with number of graph node lines increased with each render (Thanks to Colin H. Fredericks). diff --git a/bin/madge b/bin/madge index 07a87052..39d02c47 100755 --- a/bin/madge +++ b/bin/madge @@ -28,6 +28,7 @@ program .option('-C, --config ', 'provide a config file') .option('-R, --require-config ', 'include shim dependencies and paths found in RequireJS config file') .option('-O, --optimized', 'if given file is optimized with r.js', false) + .option('-N, --find-nested-dependencies', 'find nested dependencies in AMD modules', false) .option('-M, --main-require-module', 'name of the primary RequireJS module, if it\'s included with `require()`', '') .option('-j --json', 'output dependency tree in json') .option('-p --paths ', 'additional paths to search for dependencies (CJS only)', '') @@ -77,7 +78,8 @@ function run() { optimized: program.optimized, requireConfig: program.requireConfig, mainRequireModule: program.mainRequireModule, - paths: program.paths ? program.paths.split(',') : undefined + paths: program.paths ? program.paths.split(',') : undefined, + findNestedDependencies: program.findNestedDependencies }); // Ouput summary diff --git a/lib/parse/amd.js b/lib/parse/amd.js index 821580f5..9e73fc04 100644 --- a/lib/parse/amd.js +++ b/lib/parse/amd.js @@ -46,7 +46,7 @@ AMD.prototype.parseFile = function (filename) { this.emit('parseFile', {filename: filename, src: src}); if (/define|require\s*\(/m.test(src)) { - amdetective(src).map(function (obj) { + amdetective(src, {findNestedDependencies: this.opts.findNestedDependencies}).map(function (obj) { return typeof(obj) === 'string' ? [obj] : obj.deps; }).filter(function (deps) { deps.filter(function (id) { diff --git a/test/amd.js b/test/amd.js index 56568394..84bfb5a1 100644 --- a/test/amd.js +++ b/test/amd.js @@ -117,4 +117,11 @@ describe('module format (AMD)', function () { breakOnError: true }).obj().should.eql({ plugin: [ 'ok/a' ] }); }); + + it('should find nested dependencies', function () { + madge([__dirname + '/files/amd/nested/main.js'], { + format: 'amd', + findNestedDependencies: true + }).obj().should.eql({ 'main': ['a', 'b'] }); + }); }); diff --git a/test/files/amd/nested/a.js b/test/files/amd/nested/a.js new file mode 100644 index 00000000..edaaf903 --- /dev/null +++ b/test/files/amd/nested/a.js @@ -0,0 +1,3 @@ +define('a', function () { + return 'A'; +}); diff --git a/test/files/amd/nested/b.js b/test/files/amd/nested/b.js new file mode 100644 index 00000000..a910a384 --- /dev/null +++ b/test/files/amd/nested/b.js @@ -0,0 +1,3 @@ +define('b', function () { + return 'b'; +}); \ No newline at end of file diff --git a/test/files/amd/nested/main.js b/test/files/amd/nested/main.js new file mode 100644 index 00000000..e694b215 --- /dev/null +++ b/test/files/amd/nested/main.js @@ -0,0 +1,5 @@ +define(['require', 'a'], function(require, a) { + require(['b'], function (b) { + + }); +}); \ No newline at end of file