From a4d7f9c891b57cfd47abe67b4a604027d60611fc Mon Sep 17 00:00:00 2001 From: Yuri Yaryshev Date: Fri, 11 Aug 2023 11:08:41 +0300 Subject: [PATCH 1/5] - 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. --- README.md | 4 +++- index.js | 8 +++++++- lib/config.js | 2 ++ test/test.mjs | 20 ++++++++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 933dc3f..c461836 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 9199268..83f3aa8 100644 --- a/index.js +++ b/index.js @@ -82,7 +82,7 @@ module.exports.toList = function(options = {}) { */ module.exports._getDependencies = function(config = {}) { const precinctOptions = config.detectiveConfig; - precinctOptions.includeCore = false; + let dependencies; try { @@ -112,6 +112,9 @@ 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; } @@ -120,6 +123,9 @@ 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 7c8b6c3..f55206c 100644 --- a/lib/config.js +++ b/lib/config.js @@ -17,6 +17,8 @@ module.exports = class Config { this.webpackConfig = options.webpackConfig; this.nodeModulesConfig = options.nodeModulesConfig; this.detectiveConfig = options.detective || options.detectiveConfig || {}; + this.detectiveConfig.includeCore = this.includeCore = options.includeCore || false; + this.detectiveConfig.includeNonExisting = this.includeNonExisting = options.includeNonExisting || options.includeCore || false; this.tsConfig = options.tsConfig; this.noTypeDefinitions = options.noTypeDefinitions; diff --git a/test/test.mjs b/test/test.mjs index d38cf33..4d78bf6 100644 --- a/test/test.mjs +++ b/test/test.mjs @@ -139,6 +139,26 @@ describe('dependencyTree', () => { assert.ok(!Object.keys(subTree).includes('notReal')); }); + 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')); + }); + it('does not choke on cyclic dependencies', () => { mockfs({ [path.join(__dirname, '/cyclic')]: { From 03fc14683e95021943bb15b02abcabc0a9316a52 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Sun, 9 Feb 2025 07:16:16 +0200 Subject: [PATCH 2/5] Update index.js --- index.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index f771ff6..c126fd5 100644 --- a/index.js +++ b/index.js @@ -113,10 +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); - } + if (config.includeNonExisting) { + resolvedDependencies.push(`:!EXISTS: ${dependency}`); + } + continue; } @@ -125,9 +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); - } + + if (config.includeNonExisting) { + resolvedDependencies.push(`:!EXISTS: ${dependency}`); + } + continue; } From 1a61f98bfd5272dec1842a3d563ac44a82cd2478 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Sun, 9 Feb 2025 07:19:57 +0200 Subject: [PATCH 3/5] Update config.js --- lib/config.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/config.js b/lib/config.js index b8a42ff..c4a89b2 100644 --- a/lib/config.js +++ b/lib/config.js @@ -16,13 +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.detectiveConfig.includeCore = this.includeCore = options.includeCore || false; - this.detectiveConfig.includeNonExisting = this.includeNonExisting = options.includeNonExisting || options.includeCore || false; + 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'); From 2eaefa155cf16be82fa5be232d5d845f6465ae2d Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Sun, 9 Feb 2025 07:21:15 +0200 Subject: [PATCH 4/5] Update test.mjs --- test/test.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test.mjs b/test/test.mjs index f729a6a..d37e3c9 100644 --- a/test/test.mjs +++ b/test/test.mjs @@ -146,7 +146,7 @@ describe('dependencyTree', () => { const tree = dependencyTree({ filename, directory, includeNonExisting: true }); const subTree = tree[filename]; - assert.ok(Object.keys(subTree).includes( ':!EXISTS: not-real')); + assert.ok(Object.keys(subTree).includes(':!EXISTS: not-real')); }); it('test includeCore=true', () => { @@ -156,7 +156,7 @@ describe('dependencyTree', () => { const tree = dependencyTree({ filename, directory, includeCore: true }); const subTree = tree[filename]; - assert.ok(Object.keys(subTree).includes( ':!EXISTS: path')); + assert.ok(Object.keys(subTree).includes(':!EXISTS: path')); }); it('does not choke on cyclic dependencies', () => { From b289a89d92976c11a1e35a38be81723565870285 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Sun, 9 Feb 2025 07:23:14 +0200 Subject: [PATCH 5/5] Update test.mjs --- test/test.mjs | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/test/test.mjs b/test/test.mjs index d37e3c9..36ccef9 100644 --- a/test/test.mjs +++ b/test/test.mjs @@ -139,26 +139,6 @@ describe('dependencyTree', () => { assert.ok(!Object.keys(subTree).includes('notReal')); }); - 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')); - }); - it('does not choke on cyclic dependencies', () => { mockfs({ [path.join(__dirname, '/cyclic')]: { @@ -241,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');