Skip to content

Commit a4d7f9c

Browse files
committed
- Added includeNonExisting if true include unresolved dependencies, they will be prefixed with ':!EXISTS: '
- Added includeCore to include core modules (for example: "fs"), they will also be prefixed with ':!EXISTS: ' (because they don't really exist in filesystem) The WHY: - Because I want to know which of dependend packages use Node.js core modules and thus require polyfilling or can't be used inside browser.
1 parent dc6fc69 commit a4d7f9c

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ const tree = dependencyTree({
3333
}, // optional
3434
filter: path => path.indexOf('node_modules') === -1, // optional
3535
nonExistent: [], // optional
36-
noTypeDefinitions: false // optional
36+
noTypeDefinitions: false, // optional
37+
includeCore: false, // optional, if true include node.js core modules (for example: "fs"), they will be prefixed with ':!EXISTS: '
38+
includeNonExisting: false // optional, if true include unresolved dependencies, they will be prefixed with ':!EXISTS: '
3739
});
3840

3941
// Returns a post-order traversal (list form) of the tree with duplicate sub-trees pruned.

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ module.exports.toList = function(options = {}) {
8282
*/
8383
module.exports._getDependencies = function(config = {}) {
8484
const precinctOptions = config.detectiveConfig;
85-
precinctOptions.includeCore = false;
85+
8686
let dependencies;
8787

8888
try {
@@ -112,6 +112,9 @@ module.exports._getDependencies = function(config = {}) {
112112
if (!result) {
113113
debug(`skipping an empty filepath resolution for partial: ${dependency}`);
114114
config.nonExistent.push(dependency);
115+
if(config.includeNonExisting) {
116+
resolvedDependencies.push(":!EXISTS: " + dependency);
117+
}
115118
continue;
116119
}
117120

@@ -120,6 +123,9 @@ module.exports._getDependencies = function(config = {}) {
120123
if (!exists) {
121124
config.nonExistent.push(dependency);
122125
debug(`skipping non-empty but non-existent resolution: ${result} for partial: ${dependency}`);
126+
if(config.includeNonExisting) {
127+
resolvedDependencies.push(":!EXISTS: " + dependency);
128+
}
123129
continue;
124130
}
125131

lib/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ module.exports = class Config {
1717
this.webpackConfig = options.webpackConfig;
1818
this.nodeModulesConfig = options.nodeModulesConfig;
1919
this.detectiveConfig = options.detective || options.detectiveConfig || {};
20+
this.detectiveConfig.includeCore = this.includeCore = options.includeCore || false;
21+
this.detectiveConfig.includeNonExisting = this.includeNonExisting = options.includeNonExisting || options.includeCore || false;
2022
this.tsConfig = options.tsConfig;
2123
this.noTypeDefinitions = options.noTypeDefinitions;
2224

test/test.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,26 @@ describe('dependencyTree', () => {
139139
assert.ok(!Object.keys(subTree).includes('notReal'));
140140
});
141141

142+
it('test includeNonExisting=true', () => {
143+
const directory = path.join(__dirname, '/fixtures/onlyRealDeps');
144+
const filename = path.normalize(`${directory}/a.js`);
145+
146+
const tree = dependencyTree({ filename, directory, includeNonExisting: true });
147+
const subTree = tree[filename];
148+
149+
assert.ok(Object.keys(subTree).includes( ':!EXISTS: not-real'));
150+
});
151+
152+
it('test includeCore=true', () => {
153+
const directory = path.join(__dirname, '/fixtures/onlyRealDeps');
154+
const filename = path.normalize(`${directory}/a.js`);
155+
156+
const tree = dependencyTree({ filename, directory, includeCore: true });
157+
const subTree = tree[filename];
158+
159+
assert.ok(Object.keys(subTree).includes( ':!EXISTS: path'));
160+
});
161+
142162
it('does not choke on cyclic dependencies', () => {
143163
mockfs({
144164
[path.join(__dirname, '/cyclic')]: {

0 commit comments

Comments
 (0)