diff --git a/README.md b/README.md index 7969ab7..4ed1e78 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,9 @@ const tree = dependencyTree({ }, // optional filter: path => path.indexOf('node_modules') === -1, // optional nonExistent: [], // optional - noTypeDefinitions: false // optional + noTypeDefinitions: false, // optional + includeCore: false, // optional, if true include node.js core modules (for example: "fs"), they will be prefixed with ':!EXISTS: ' + includeNonExisting: false // optional, if true include unresolved dependencies, they will be prefixed with ':!EXISTS: ' }); // Returns a post-order traversal (list form) of the tree with duplicate sub-trees pruned. diff --git a/index.js b/index.js index 7b3ca6e..c126fd5 100644 --- a/index.js +++ b/index.js @@ -83,7 +83,7 @@ module.exports.toList = function(options = {}) { */ module.exports._getDependencies = function(config = {}) { const precinctOptions = config.detectiveConfig; - precinctOptions.includeCore = false; + let dependencies; try { @@ -113,7 +113,12 @@ module.exports._getDependencies = function(config = {}) { if (!result) { debug(`skipping an empty filepath resolution for partial: ${dependency}`); + config.nonExistent.push(dependency); + if (config.includeNonExisting) { + resolvedDependencies.push(`:!EXISTS: ${dependency}`); + } + continue; } @@ -122,6 +127,11 @@ module.exports._getDependencies = function(config = {}) { if (!exists) { config.nonExistent.push(dependency); debug(`skipping non-empty but non-existent resolution: ${result} for partial: ${dependency}`); + + if (config.includeNonExisting) { + resolvedDependencies.push(`:!EXISTS: ${dependency}`); + } + continue; } diff --git a/lib/config.js b/lib/config.js index b0ba90a..c4a89b2 100644 --- a/lib/config.js +++ b/lib/config.js @@ -16,11 +16,15 @@ module.exports = class Config { this.requireConfig = options.config || options.requireConfig; this.webpackConfig = options.webpackConfig; this.nodeModulesConfig = options.nodeModulesConfig; - this.detectiveConfig = options.detective || options.detectiveConfig || {}; + this.includeCore = options.includeCore || false; + this.includeNonExisting = options.includeNonExisting || options.includeCore || false; this.tsConfig = options.tsConfig; this.tsConfigPath = options.tsConfigPath; this.noTypeDefinitions = options.noTypeDefinitions; this.filter = options.filter; + this.detectiveConfig = options.detective || options.detectiveConfig || {}; + this.detectiveConfig.includeCore = this.includeCore; + this.detectiveConfig.includeNonExisting = this.includeNonExisting; if (!this.filename) throw new Error('filename not given'); if (!this.directory) throw new Error('directory not given'); diff --git a/test/test.mjs b/test/test.mjs index 8a97d8d..36ccef9 100644 --- a/test/test.mjs +++ b/test/test.mjs @@ -221,6 +221,26 @@ describe('dependencyTree', () => { assert.ok(!list.includes(path.join(directory, 'required.js'))); }); + it('test includeNonExisting=true', () => { + const directory = path.join(__dirname, '/fixtures/onlyRealDeps'); + const filename = path.normalize(`${directory}/a.js`); + + const tree = dependencyTree({ filename, directory, includeNonExisting: true }); + const subTree = tree[filename]; + + assert.ok(Object.keys(subTree).includes(':!EXISTS: not-real')); + }); + + it('test includeCore=true', () => { + const directory = path.join(__dirname, '/fixtures/onlyRealDeps'); + const filename = path.normalize(`${directory}/a.js`); + + const tree = dependencyTree({ filename, directory, includeCore: true }); + const subTree = tree[filename]; + + assert.ok(Object.keys(subTree).includes(':!EXISTS: path')); + }); + describe('when given a detective configuration', () => { it('passes it through to precinct', () => { const spy = sinon.spy(precinct, 'paperwork');