From 02f3d28f26af12d0b0ade3c56df42451161b3882 Mon Sep 17 00:00:00 2001 From: Michael White Date: Sun, 1 Jun 2014 00:35:26 -0400 Subject: [PATCH] Apply exclude to RequireJS shim dependencies --- lib/madge.js | 2 +- lib/requirejs.js | 12 ++++++++---- test/amd.js | 8 +++++++- test/files/amd/requirejs/config.js | 8 ++++++-- test/files/amd/requirejs/vendor/baz.js | 0 test/files/amd/requirejs/vendor/quux.js | 0 6 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 test/files/amd/requirejs/vendor/baz.js create mode 100644 test/files/amd/requirejs/vendor/quux.js diff --git a/lib/madge.js b/lib/madge.js index 298a581b..692fb404 100644 --- a/lib/madge.js +++ b/lib/madge.js @@ -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; diff --git a/lib/requirejs.js b/lib/requirejs.js index 28c1f181..371aded3 100644 --- a/lib/requirejs.js +++ b/lib/requirejs.js @@ -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] = []; } diff --git a/test/amd.js b/test/amd.js index b751619f..91e5cf1b 100644 --- a/test/amd.js +++ b/test/amd.js @@ -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 () { @@ -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 () { diff --git a/test/files/amd/requirejs/config.js b/test/files/amd/requirejs/config.js index 2009a8c6..8f1cfcc5 100644 --- a/test/files/amd/requirejs/config.js +++ b/test/files/amd/requirejs/config.js @@ -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' } } }); \ No newline at end of file diff --git a/test/files/amd/requirejs/vendor/baz.js b/test/files/amd/requirejs/vendor/baz.js new file mode 100644 index 00000000..e69de29b diff --git a/test/files/amd/requirejs/vendor/quux.js b/test/files/amd/requirejs/vendor/quux.js new file mode 100644 index 00000000..e69de29b