Skip to content

Commit

Permalink
Apply exclude to RequireJS shim dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
mwhite committed Jun 1, 2014
1 parent 41a15f5 commit 02f3d28
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/madge.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function Madge(src, opts) {
}

if (this.opts.requireConfig) {
mergeTrees(tree, requirejs.getShimDepsFromConfig(this.opts.requireConfig));
mergeTrees(tree, requirejs.getShimDepsFromConfig(this.opts.requireConfig, this.opts.exclude));
}

this.tree = tree;
Expand Down
12 changes: 8 additions & 4 deletions lib/requirejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ var fs = require('fs'),
* Read shim dependencies from RequireJS config.
* @param {String} filename
*/
module.exports.getShimDepsFromConfig = function (filename) {
module.exports.getShimDepsFromConfig = function (filename, exclude) {
var deps = {},
config = parse.findConfig(filename, fs.readFileSync(filename, 'utf8'));
config = parse.findConfig(filename, fs.readFileSync(filename, 'utf8')),
excludeRegex = exclude ? new RegExp(exclude) : false,
isIncluded = function (key) {
return !(excludeRegex && key.match(excludeRegex));
};

if (config.shim) {
Object.keys(config.shim).forEach(function (key) {
Object.keys(config.shim).filter(isIncluded).forEach(function (key) {
if (config.shim[key].deps) {
deps[key] = config.shim[key].deps;
deps[key] = config.shim[key].deps.filter(isIncluded);
} else {
deps[key] = [];
}
Expand Down
8 changes: 7 additions & 1 deletion test/amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('module format (AMD)', function () {
madge([__dirname + '/files/amd/requirejs/a.js'], {
format: 'amd',
requireConfig: __dirname + '/files/amd/requirejs/config.js'
}).obj().should.eql({ a: [ 'jquery' ], 'jquery': [], 'jquery.foo': [ 'jquery' ], 'jquery.bar': [ 'jquery' ] });
}).obj().should.eql({ a: [ 'jquery' ], 'jquery': [], 'jquery.foo': [ 'jquery' ], 'jquery.bar': [ 'jquery' ], 'baz': [ 'quux' ], 'quux': [] });
});

it('should be able to exclude modules', function () {
Expand All @@ -33,6 +33,12 @@ describe('module format (AMD)', function () {
format: 'amd',
exclude: '.*\/c$'
}).obj().should.eql({ 'a': [ 'sub/b' ], 'd': [], 'e': [], 'sub/b': [] });

madge([__dirname + '/files/amd/requirejs/a.js'], {
format: 'amd',
requireConfig: __dirname + '/files/amd/requirejs/config.js',
exclude: '^jquery.foo|quux$'
}).obj().should.eql({ a: [ 'jquery' ], 'jquery': [], 'jquery.bar': [ 'jquery' ] , 'baz': []});
});

it('should tackle errors in files', function () {
Expand Down
8 changes: 6 additions & 2 deletions test/files/amd/requirejs/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ require.config({
paths: {
'jquery': 'vendor/jquery-2.0.3',
'jquery.foo': 'vendor/jquery.foo-1.0',
'jquery.bar': 'vendor/jquery.bar-1.0'
'jquery.bar': 'vendor/jquery.bar-1.0',
'baz': 'vendor/baz',
'quux': 'vendor/quux'
},
shim: {
'jquery': { exports: '$' },
'jquery.foo': { deps: ['jquery'] },
'jquery.bar': { deps: ['jquery'] }
'jquery.bar': { deps: ['jquery'] },
'baz': { exports: 'baz', deps: ['quux'] },
'quux': { exports: 'quux' }
}
});
Empty file.
Empty file.

0 comments on commit 02f3d28

Please sign in to comment.