Skip to content

Commit

Permalink
Support for option findNestedDependencies to find nested dependencies…
Browse files Browse the repository at this point in the history
… in AMD modules.
  • Loading branch information
pahen committed Dec 19, 2014
1 parent 65cd69a commit 9bf15c9
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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).
Expand Down
4 changes: 3 additions & 1 deletion bin/madge
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ program
.option('-C, --config <filename>', 'provide a config file')
.option('-R, --require-config <filename>', '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 <directory>', 'additional paths to search for dependencies (CJS only)', '')
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/parse/amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions test/amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'] });
});
});
3 changes: 3 additions & 0 deletions test/files/amd/nested/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
define('a', function () {
return 'A';
});
3 changes: 3 additions & 0 deletions test/files/amd/nested/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
define('b', function () {
return 'b';
});
5 changes: 5 additions & 0 deletions test/files/amd/nested/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
define(['require', 'a'], function(require, a) {
require(['b'], function (b) {

});
});

0 comments on commit 9bf15c9

Please sign in to comment.