Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolving local/nested node_modules folder correctly #162

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const fs = require('node:fs');
const path = require('node:path');
const { debuglog } = require('node:util');
const cabinet = require('filing-cabinet');
const precinct = require('precinct');
Expand Down Expand Up @@ -161,6 +162,7 @@ function traverse(config = {}) {
for (const dependency of dependencies) {
const localConfig = config.clone();
localConfig.filename = dependency;
localConfig.directory = getDirectory(localConfig);

if (localConfig.isListForm) {
for (const item of traverse(localConfig)) {
Expand Down Expand Up @@ -192,3 +194,25 @@ function dedupeNonExistent(nonExistent) {
i++;
}
}

// If the file is in a node module, use the root directory of the module
function getDirectory(localConfig) {
function _getProjectPath(filename) {
try {
const nodeModuleParts = filename.split('node_modules');
const packageSubPathPath = nodeModuleParts.pop().split(path.sep).filter(Boolean);
const packageName = packageSubPathPath[0].startsWith('@') ? `${packageSubPathPath[0]}${path.sep}${packageSubPathPath[1]}` : packageSubPathPath[0];

return path.normalize([...nodeModuleParts, `${path.sep}${packageName}`].join('node_modules'));
} catch {
debug(`Could not determine the root directory of package file ${filename}. Using default`);
return null;
}
}

if (!localConfig.filename.includes('node_modules')) {
return localConfig.directory;
}

return _getProjectPath(path.dirname(localConfig.filename)) || localConfig.directory;
}
71 changes: 71 additions & 0 deletions test/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,77 @@ describe('dependencyTree', () => {
});
});

describe('It uses package specific node_module directory when resolving package dependencies', () => {
testTreesForFormat('commonjs');

it('It can find sub package in node module package', () => {
mockfs({
[path.join(__dirname, '/es6')]: {
'module.entry.js': 'import * as module from "parent_module_a"',
node_modules: {
parent_module_a: {
'index.main.js': 'import * as child_module from "child_node_module"; module.exports = child_module;',
'package.json': '{ "main": "index.main.js"}',
node_modules: {
child_node_module: {
'index.main.js': 'module.exports = "child_node_module_of_parent_a"',
'package.json': '{ "main": "index.main.js"}'
}
}
}
}
}
});

const directory = path.join(__dirname, '/es6');
const filename = path.normalize(`${directory}/module.entry.js`);

const treeList = dependencyTree({
filename,
directory,
isListForm: true
});

assert.ok(treeList.includes(path.normalize(`${directory}/node_modules/parent_module_a/node_modules/child_node_module/index.main.js`)));
});

it('it usues correct version of sub package in node module package', () => {
mockfs({
[path.join(__dirname, '/es6')]: {
'module.entry.js': 'import * as module from "parent_module_a"',
node_modules: {
child_node_module: {
'index.main.js': 'module.exports = "child_node_module"',
'package.json': '{ "main": "index.main.js", "version": "2.0.0"}'
},
parent_module_a: {
'index.main.js': 'import * as child_module from "child_node_module"; module.exports = child_module;',
'package.json': '{ "main": "index.main.js"}',
node_modules: {
child_node_module: {
'index.main.js': 'module.exports = "child_node_module_of_parent_a"',
'package.json': '{ "main": "index.main.js", "version": "1.0.0"}'
}
}
}
}
}
});

const directory = path.join(__dirname, '/es6');
const filename = path.normalize(`${directory}/module.entry.js`);

const treeList = dependencyTree({
filename,
directory,
isListForm: true
});

assert.ok(!treeList.includes(path.normalize(`${directory}/node_modules/child_node_module/index.main.js`)));
assert.ok(treeList.includes(path.normalize(`${directory}/node_modules/parent_module_a/node_modules/child_node_module/index.main.js`)));
});
});

describe('module formats', () => {
describe('amd', () => {
testTreesForFormat('amd');
Expand Down