Skip to content

Commit

Permalink
Only include shallow NPM modules
Browse files Browse the repository at this point in the history
  • Loading branch information
pahen committed Feb 5, 2017
1 parent eac8591 commit 4697bc8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
**Madge** is a developer tool for generating a visual graph of your module dependencies, finding circular dependencies, and give you other useful info. Joel Kemp's awesome [dependency-tree](https://github.com/mrjoelkemp/node-dependency-tree) is used for extracting the dependency tree.

* Works for JavaScript (AMD, CommonJS, ES6 modules) and CSS preprocessors (Sass, Stylus)
* NPM installed dependencies are excluded by default (can be enabled in config)
* NPM installed dependencies are excluded by default (can be enabled)
* All core Node.js modules (assert, path, fs, etc) are excluded
* Recurse into child dependencies to get a complete dependency tree of a file
* Will traverse child dependencies automatically

Read the [changelog](CHANGELOG.md) for latest changes.

Expand Down Expand Up @@ -152,7 +152,7 @@ madge('path/to/app.js')
Property | Type | Default | Description
--- | --- | --- | ---
`baseDir` | String | null | Base directory to use instead of the default
`includeNpm` | Boolean | false | If node_modules should be included
`includeNpm` | Boolean | false | If shallow NPM modules should be included
`fileExtensions` | Array | ['js'] | Valid file extensions used to find files in directories
`showFileExtension` | Boolean | false | If file extension should be included in module name
`excludeRegExp` | Array | false | An array of RegExp for excluding modules
Expand Down
5 changes: 5 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ program
.option('--show-extension', 'include file extension in module name', false)
.option('--require-config <file>', 'path to RequireJS config')
.option('--webpack-config <file>', 'path to webpack config')
.option('--include-npm', 'include shallow NPM modules', false)
.option('--no-color', 'disable color in output and image', false)
.option('--stdin', 'read predefined tree from STDIN', false)
.option('--warning', 'show warnings about skipped files', false)
Expand Down Expand Up @@ -79,6 +80,10 @@ if (program.webpackConfig) {
config.webpackConfig = program.webpackConfig;
}

if (program.includeNpm) {
config.includeNpm = program.includeNpm;
}

if (!program.color) {
config.backgroundColor = '#ffffff';
config.nodeColor = '#00000';
Expand Down
16 changes: 15 additions & 1 deletion lib/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class Tree {
const depTree = {};
const visited = {};
const nonExistent = [];
const npmPaths = {};
const pathCache = {};

files.forEach((file) => {
if (visited[file]) {
Expand All @@ -125,14 +127,26 @@ class Tree {
dependencyFilterRes = this.config.dependencyFilter(dependencyFilePath, traversedFilePath, this.baseDir);
}

if (this.config.includeNpm && isNpmPath) {
(npmPaths[traversedFilePath] = npmPaths[traversedFilePath] || []).push(dependencyFilePath);
}

return !isNpmPath && (dependencyFilterRes || dependencyFilterRes === undefined);
},
detective: this.config.detectiveOptions,
nonExistent: nonExistent
}));
});

let tree = this.convertTree(depTree, {}, {});
let tree = this.convertTree(depTree, {}, pathCache, npmPaths);

for (const npmKey in npmPaths) {
const id = this.processPath(npmKey, pathCache);

npmPaths[npmKey].forEach((npmPath) => {
tree[id].push(this.processPath(npmPath, pathCache));
});
}

if (this.config.excludeRegExp) {
tree = this.exclude(tree, this.config.excludeRegExp);
Expand Down
3 changes: 1 addition & 2 deletions test/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ describe('CommonJS', () => {
}).catch(done);
});

it('can include NPM modules', (done) => {
it('can include shallow NPM modules', (done) => {
madge(dir + '/npm.js', {
includeNpm: true
}).then((res) => {
res.obj().should.eql({
'node_modules/a': [],
'normal/d': [],
'npm': ['node_modules/a', 'normal/d']
});
Expand Down

0 comments on commit 4697bc8

Please sign in to comment.