Skip to content

Commit

Permalink
Add support for JSX (React) and additional module paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
blowery committed Dec 19, 2014
1 parent ea4b85b commit 858cd72
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 15 deletions.
4 changes: 3 additions & 1 deletion bin/madge
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ program
.option('-O, --optimized', 'if given file is optimized with r.js', 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)', '')
.parse(process.argv);

if (!program.args.length && !program.read && !program.requireConfig) {
Expand Down Expand Up @@ -75,7 +76,8 @@ function run() {
exclude: program.exclude,
optimized: program.optimized,
requireConfig: program.requireConfig,
mainRequireModule: program.mainRequireModule
mainRequireModule: program.mainRequireModule,
paths: program.paths ? program.paths.split(',') : undefined
});

// Ouput summary
Expand Down
8 changes: 6 additions & 2 deletions lib/parse/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ var fs = require('fs'),
EventEmitter = require('events').EventEmitter,
commondir = require('commondir'),
finder = require('walkdir'),
coffee = require('coffee-script');
coffee = require('coffee-script'),
jsx = require('react-tools');

/**
* Traversing `src` and fetches all dependencies.
Expand All @@ -29,8 +30,9 @@ var Base = module.exports = function(src, opts, parent) {

this.opts = opts;
this.tree = {};
this.extRegEx = /\.(js|coffee)$/;
this.extRegEx = /\.(js|coffee|jsx)$/;
this.coffeeExtRegEx = /\.coffee$/;
this.jsxExtRegEx = /\.jsx$/;
src = this.resolveTargets(src);
this.excludeRegex = opts.exclude ? new RegExp(opts.exclude) : false;
this.baseDir = this.getBaseDir(src);
Expand Down Expand Up @@ -127,6 +129,8 @@ Base.prototype.getFileSource = function (filename) {
header: false,
bare: true
});
} else if (filename.match(this.jsxExtRegEx)) {
src = jsx.transform( src );
}

return src;
Expand Down
3 changes: 2 additions & 1 deletion lib/parse/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ util.inherits(CJS, Base);
Base.prototype.resolve = function (dir, id) {
try {
return resolve.sync(id, {
basedir: dir
basedir: dir,
paths: this.opts.paths
});
} catch (e) {
if (this.opts.breakOnError) {
Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@
"test": "grunt"
},
"dependencies": {
"amdetective": "0.0.2",
"coffee-script": "1.3.3",
"colors": "0.6.0-1",
"commander": "1.0.0",
"detective": "0.1.1",
"walkdir": "0.0.5",
"resolve": "0.2.3",
"commondir": "0.0.1",
"detective": "0.1.1",
"graphviz": "0.0.7",
"react-tools": "0.12.1",
"resolve": "0.2.3",
"uglify-js": "1.2.6",
"colors": "0.6.0-1",
"coffee-script": "1.3.3",
"amdetective": "0.0.2"
"walkdir": "0.0.5"
},
"devDependencies": {
"grunt": "0.4.4",
Expand Down
6 changes: 3 additions & 3 deletions test/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('module format (CommonJS)', function () {

it('should behave as expected on ok files', function () {
madge([__dirname + '/files/cjs/normal'])
.obj().should.eql({ 'a': [ 'sub/b' ], 'd': [], 'sub/b': [ 'sub/c' ], 'sub/c': [ 'd' ] });
.obj().should.eql({ 'a': [ 'sub/b' ], 'fancy-main/not-index': [], 'd': [], 'sub/b': [ 'sub/c' ], 'sub/c': [ 'd' ] });
});

it('should handle expressions in require call', function () {
Expand Down Expand Up @@ -39,11 +39,11 @@ describe('module format (CommonJS)', function () {
it('should be able to exclude modules', function () {
madge([__dirname + '/files/cjs/normal'], {
exclude: '^sub'
}).obj().should.eql({ 'a': [], 'd': [] });
}).obj().should.eql({ 'a': [], 'd': [], 'fancy-main/not-index': [] });

madge([__dirname + '/files/cjs/normal'], {
exclude: '.*\/c$'
}).obj().should.eql({ 'a': [ 'sub/b' ], 'd': [], 'sub/b': [] });
}).obj().should.eql({ 'a': [ 'sub/b' ], 'd': [], 'sub/b': [], 'fancy-main/not-index': [], });
});

it('should find circular dependencies', function () {
Expand Down
1 change: 1 addition & 0 deletions test/files/cjs/normal/fancy-main/not-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = new Date();
3 changes: 3 additions & 0 deletions test/files/cjs/normal/fancy-main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": "not-index.js"
}
4 changes: 2 additions & 2 deletions test/pluggable.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Madge', function () {
idAdd += obj.id;
};
madge([__dirname + '/files/cjs/normal'], opts);
(fileAdd + idAdd).should.eql( "a.jsd.jsb.jsc.js" + "adsub/bsub/c" );
(fileAdd + idAdd).should.eql( "a.jsd.jsnot-index.jsb.jsc.js" + "adfancy-main/not-indexsub/bsub/c" );
});
});

Expand Down Expand Up @@ -49,7 +49,7 @@ describe('Madge', function () {
}
};
var madger = madge([__dirname + '/files/cjs/normal'], opts);
madger.idAdd.should.eql( "adsub/bsub/c" );
madger.idAdd.should.eql( "adfancy-main/not-indexsub/bsub/c" );
});
});

Expand Down

0 comments on commit 858cd72

Please sign in to comment.