diff --git a/lib/madge.js b/lib/madge.js index 5b54f282..0e401f79 100644 --- a/lib/madge.js +++ b/lib/madge.js @@ -4,93 +4,12 @@ * Module dependencies. */ var util = require('util'), - requirejs = require('./requirejs'), cyclic = require('./cyclic'), CJS = require('./parse/cjs'), AMD = require('./parse/amd'), ES6 = require('./parse/es6'), graph = require('./graph'); -/** - * Merge the two given trees. - * @param {Object} a - * @param {Object} b - */ -function mergeTrees(a, b) { - Object.keys(b).forEach(function (id) { - if (!a[id]) { - a[id] = []; - } - - b[id].forEach(function (dep) { - if (a[id].indexOf(dep) < 0) { - a[id].push(dep); - } - }); - }); -} - -/** - * Helper for re-mapping path-refs to id-refs that are specified in RequireJS' path config. - * @param {Object} deps (dependency-list) - * @param {Object} pathDefs (path-definitions from requirejs-config) - * @param {String} baseDir (base directory of source files) - */ -function convertPathsToIds(deps, pathDefs, baseDir) { - var path, pathDeps, i1, len1, i2, len2, isContained; - - if (baseDir){ - baseDir += '/'; - } else { - baseDir = ''; - } - - Object.keys(pathDefs).forEach(function (id) { - path = pathDefs[id]; - - //if path does not start with / or a protocol: prepend with baseDir - if (!/^[^\/]+:\/\/|^\//m.test(path) ){ - path = baseDir + path; - } - - if (path !== id && deps[path]) { - if (deps[id] && deps[id].length > 0){ - pathDeps = deps[path].slice(0, deps[path].length-1); - - //remove entries from , if already contained in - for (i1=0, len1 = pathDeps.length; i1 < len1; ++i1){ - for (i2=0, len2 = deps[id].length; i2 < len2; ++i2){ - if (pathDeps[i1] === deps[id][i2]){ - pathDeps.splice(i1--, 1); - break; - } - } - } - deps[id] = deps[id].concat(pathDeps); - } else { - deps[id] = deps[path]; - } - - delete deps[path]; - } else if (!deps[id]) { - deps[id] = []; - } - - //normalize entries within deps-arrays (i.e. replace path-refs with id-refs) - Object.keys(pathDefs).forEach(function (id) { - path = baseDir + pathDefs[id]; - if (deps[id]){ - for (i1=0, len1 = deps[id].length; i1 < len1; ++i1){ - //replace path-ref with id-ref (if necessary) - if (deps[id][i1] === path){ - deps[id][i1] = id; - } - } - } - }); - }); -} - /** * Expose factory function. * @api public @@ -128,13 +47,6 @@ function Madge(src, opts) { tree = this.parse(src); } - if (this.opts.requireConfig) { - var baseDir = src.length ? src[0].replace(/\\/g, '/') : ''; - baseDir = requirejs.getBaseUrlFromConfig(this.opts.requireConfig, baseDir); - convertPathsToIds(tree, requirejs.getPathsFromConfig(this.opts.requireConfig, this.opts.exclude), baseDir); - mergeTrees(tree, requirejs.getShimDepsFromConfig(this.opts.requireConfig, this.opts.exclude)); - } - this.tree = tree; } diff --git a/lib/parse/amd.js b/lib/parse/amd.js index 99b7b570..1997a81c 100644 --- a/lib/parse/amd.js +++ b/lib/parse/amd.js @@ -8,15 +8,166 @@ var fs = require('fs'), util = require('util'), amdetective = require('amdetective'), colors = require('colors'), + parse = require('./parse'), Base = require('./base'); +/** + * Merge the two given trees. + * @param {Object} a + * @param {Object} b + */ +function mergeTrees(a, b) { + Object.keys(b).forEach(function (id) { + if (!a[id]) { + a[id] = []; + } + + b[id].forEach(function (dep) { + if (a[id].indexOf(dep) < 0) { + a[id].push(dep); + } + }); + }); +} + +/** + * Helper for re-mapping path-refs to id-refs that are specified in RequireJS' path config. + * @param {Object} deps (dependency-list) + * @param {Object} pathDefs (path-definitions from requirejs-config) + * @param {String} baseDir (base directory of source files) + */ +function convertPathsToIds(deps, pathDefs, baseDir) { + var path, pathDeps, i1, len1, i2, len2, isContained; + + if (baseDir){ + baseDir += '/'; + } else { + baseDir = ''; + } + + Object.keys(pathDefs).forEach(function (id) { + path = pathDefs[id]; + + //if path does not start with / or a protocol: prepend with baseDir + if (!/^[^\/]+:\/\/|^\//m.test(path) ){ + path = baseDir + path; + } + + if (path !== id && deps[path]) { + if (deps[id] && deps[id].length > 0){ + pathDeps = deps[path].slice(0, deps[path].length-1); + + //remove entries from , if already contained in + for (i1=0, len1 = pathDeps.length; i1 < len1; ++i1){ + for (i2=0, len2 = deps[id].length; i2 < len2; ++i2){ + if (pathDeps[i1] === deps[id][i2]){ + pathDeps.splice(i1--, 1); + break; + } + } + } + deps[id] = deps[id].concat(pathDeps); + } else { + deps[id] = deps[path]; + } + + delete deps[path]; + } else if (!deps[id]) { + deps[id] = []; + } + + //normalize entries within deps-arrays (i.e. replace path-refs with id-refs) + Object.keys(pathDefs).forEach(function (id) { + path = baseDir + pathDefs[id]; + if (deps[id]){ + for (i1=0, len1 = deps[id].length; i1 < len1; ++i1){ + //replace path-ref with id-ref (if necessary) + if (deps[id][i1] === path){ + deps[id][i1] = id; + } + } + } + }); + }); +} + +/** + * Read shim dependencies from RequireJS config. + * @param {String} filename + * @param {String} [exclude] + * @return {Object} + */ +function getShimDepsFromConfig(filename, exclude) { + var deps = {}, + 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).filter(isIncluded).forEach(function (key) { + if (config.shim[key].deps) { + deps[key] = config.shim[key].deps.filter(isIncluded); + } else { + deps[key] = []; + } + }); + } + + return deps; +} + +/** +* Read path definitions from RequireJS config. +* @param {String} filename +* @param {String} [exclude] +* @return {Object} +*/ +function getPathsFromConfig(filename, exclude) { + var paths = {}, + config = parse.findConfig(filename, fs.readFileSync(filename, 'utf8')), + excludeRegex = exclude ? new RegExp(exclude) : false, + isIncluded = function (key) { + return !(excludeRegex && key.match(excludeRegex)); + }; + + if (config.paths) { + Object.keys(config.paths).filter(isIncluded).forEach(function (key) { + paths[key] = config.paths[key]; + }); + } + + return paths; +} + +/** + * Read baseUrl from RequireJS config. + * @param {String} filename + * @param {String} srcBaseDir + */ +function getBaseUrlFromConfig(filename, srcBaseDir) { + var config = parse.findConfig(filename, fs.readFileSync(filename, 'utf8')); + return config.baseUrl ? path.relative(srcBaseDir, config.baseUrl) : ''; +} + /** * This class will parse the AMD module format. * @see https://github.com/amdjs/amdjs-api/wiki/AMD * @constructor + * @param {Array} src + * @param {Object} opts + * @param {Object} parent */ -var AMD = module.exports = function () { +var AMD = module.exports = function (src, opts, parent) { Base.apply(this, arguments); + + if (opts.requireConfig) { + var baseDir = src.length ? src[0].replace(/\\/g, '/') : ''; + baseDir = getBaseUrlFromConfig(opts.requireConfig, baseDir); + convertPathsToIds(this.tree, getPathsFromConfig(opts.requireConfig, opts.exclude), baseDir); + mergeTrees(this.tree, getShimDepsFromConfig(opts.requireConfig, opts.exclude)); + } }; /** @@ -44,7 +195,7 @@ AMD.prototype.parseFile = function (filename) { src = this.getFileSource(filename), fileData = {filename: filename, src: src}; - this.emit('parseFile', fileData); + this.emit('parseFile', fileData); if (/define|require\s*\(/m.test(fileData.src)) { amdetective(fileData.src, {findNestedDependencies: this.opts.findNestedDependencies}).map(function (obj) { diff --git a/lib/requirejs.js b/lib/requirejs.js deleted file mode 100644 index f55496b2..00000000 --- a/lib/requirejs.js +++ /dev/null @@ -1,68 +0,0 @@ -'use strict'; - -/** - * Module dependencies. - */ -var fs = require('fs'), - parse = require('./parse/parse'), - path = require('path'); - -/** - * Read shim dependencies from RequireJS config. - * @param {String} filename - * @param {String} [exclude] - * @return {Object} - */ -module.exports.getShimDepsFromConfig = function (filename, exclude) { - var deps = {}, - 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).filter(isIncluded).forEach(function (key) { - if (config.shim[key].deps) { - deps[key] = config.shim[key].deps.filter(isIncluded); - } else { - deps[key] = []; - } - }); - } - - return deps; -}; - -/** -* Read path definitions from RequireJS config. -* @param {String} filename -* @param {String} [exclude] -* @return {Object} -*/ -module.exports.getPathsFromConfig = function (filename, exclude) { - var paths = {}, - config = parse.findConfig(filename, fs.readFileSync(filename, 'utf8')), - excludeRegex = exclude ? new RegExp(exclude) : false, - isIncluded = function (key) { - return !(excludeRegex && key.match(excludeRegex)); - }; - - if (config.paths) { - Object.keys(config.paths).filter(isIncluded).forEach(function (key) { - paths[key] = config.paths[key]; - }); - } - - return paths; -}; - -/** - * Read baseUrl from RequireJS config. - * @param {String} filename - * @param {String} srcBaseDir - */ -module.exports.getBaseUrlFromConfig = function (filename, srcBaseDir) { - var config = parse.findConfig(filename, fs.readFileSync(filename, 'utf8')); - return config.baseUrl ? path.relative(srcBaseDir, config.baseUrl) : ''; -}; \ No newline at end of file diff --git a/package.json b/package.json index dfa5086e..42e05efc 100644 --- a/package.json +++ b/package.json @@ -2,11 +2,13 @@ "name": "madge", "version": "0.5.4", "author": "Patrik Henningsson ", - "repository": "git://github.com/pahen/node-madge", - "homepage": "https://github.com/pahen/node-madge", + "repository": "git://github.com/pahen/madge", + "homepage": "https://github.com/pahen/madge", "license": "MIT", - "description": "Create graphs from your CommonJS or AMD module dependencies.", + "description": "Create graphs from your CommonJS, AMD or ES6 module dependencies.", "keywords": [ + "ES6", + "ES7", "AMD", "RequireJS", "require",