Skip to content

Commit

Permalink
Move RequireJS specific code into amd.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pahen committed Jul 3, 2016
1 parent eba42cc commit 374d14c
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 161 deletions.
88 changes: 0 additions & 88 deletions lib/madge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <path-ref>, if already contained in <id-ref>
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
Expand Down Expand Up @@ -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;
}

Expand Down
155 changes: 153 additions & 2 deletions lib/parse/amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <path-ref>, if already contained in <id-ref>
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));
}
};

/**
Expand Down Expand Up @@ -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) {
Expand Down
68 changes: 0 additions & 68 deletions lib/requirejs.js

This file was deleted.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"name": "madge",
"version": "0.5.4",
"author": "Patrik Henningsson <[email protected]>",
"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",
Expand Down

0 comments on commit 374d14c

Please sign in to comment.