Skip to content

Commit 3238836

Browse files
authored
Merge pull request #3503 from NativeScript/tdermendzhiev/add-recursive-module-resolution
Tdermendzhiev/add recursive module resolution
2 parents 159ca19 + e31790a commit 3238836

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

lib/tools/node-modules/node-modules-dependencies-builder.ts

+28-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as path from "path";
22
import { NODE_MODULES_FOLDER_NAME, PACKAGE_JSON_FILE_NAME } from "../../constants";
33

44
interface IDependencyDescription {
5+
parent: IDependencyDescription;
56
parentDir: string;
67
name: string;
78
depth: number;
@@ -20,20 +21,22 @@ export class NodeModulesDependenciesBuilder implements INodeModulesDependenciesB
2021

2122
const queue: IDependencyDescription[] = _.keys(dependencies)
2223
.map(dependencyName => ({
24+
parent: null,
2325
parentDir: projectPath,
2426
name: dependencyName,
2527
depth: 0
2628
}));
2729

2830
while (queue.length) {
2931
const currentModule = queue.shift();
30-
const resolvedDependency = this.findModule(rootNodeModulesPath, currentModule.parentDir, currentModule.name, currentModule.depth, resolvedDependencies);
32+
const resolvedDependency = this.findModule(rootNodeModulesPath, currentModule, resolvedDependencies);
3133

3234
if (resolvedDependency && !_.some(resolvedDependencies, r => r.directory === resolvedDependency.directory)) {
3335
_.each(resolvedDependency.dependencies, d => {
34-
const dependency: IDependencyDescription = { name: d, parentDir: resolvedDependency.directory, depth: resolvedDependency.depth + 1 };
36+
const dependency: IDependencyDescription = { parent: currentModule, name: d, parentDir: resolvedDependency.directory, depth: resolvedDependency.depth + 1 };
3537

3638
const shouldAdd = !_.some(queue, element =>
39+
element.parent === dependency.parent &&
3740
element.name === dependency.name &&
3841
element.parentDir === dependency.parentDir &&
3942
element.depth === dependency.depth);
@@ -50,26 +53,40 @@ export class NodeModulesDependenciesBuilder implements INodeModulesDependenciesB
5053
return resolvedDependencies;
5154
}
5255

53-
private findModule(rootNodeModulesPath: string, parentModulePath: string, name: string, depth: number, resolvedDependencies: IDependencyData[]): IDependencyData {
54-
let modulePath = path.join(parentModulePath, NODE_MODULES_FOLDER_NAME, name); // node_modules/parent/node_modules/<package>
55-
const rootModulesPath = path.join(rootNodeModulesPath, name);
56-
let depthInNodeModules = depth;
56+
private findModule(rootNodeModulesPath: string, depDescription: IDependencyDescription, resolvedDependencies: IDependencyData[]): IDependencyData {
57+
let modulePath = path.join(depDescription.parentDir, NODE_MODULES_FOLDER_NAME, depDescription.name); // node_modules/parent/node_modules/<package>
58+
const rootModulesPath = path.join(rootNodeModulesPath, depDescription.name);
59+
let depthInNodeModules = depDescription.depth;
5760

5861
if (!this.moduleExists(modulePath)) {
59-
modulePath = rootModulesPath; // /node_modules/<package>
60-
if (!this.moduleExists(modulePath)) {
61-
return null;
62+
63+
let moduleExists = false;
64+
let parent = depDescription.parent;
65+
66+
while (parent && !moduleExists) {
67+
modulePath = path.join(depDescription.parent.parentDir, NODE_MODULES_FOLDER_NAME, depDescription.name);
68+
moduleExists = this.moduleExists(modulePath);
69+
if (!moduleExists) {
70+
parent = parent.parent;
71+
}
72+
}
73+
74+
if (!moduleExists) {
75+
modulePath = rootModulesPath; // /node_modules/<package>
76+
if (!this.moduleExists(modulePath)) {
77+
return null;
78+
}
6279
}
6380

6481
depthInNodeModules = 0;
6582
}
6683

67-
if (_.some(resolvedDependencies, r => r.name === name && r.directory === modulePath)) {
84+
if (_.some(resolvedDependencies, r => r.name === depDescription.name && r.directory === modulePath)) {
6885
return null;
6986

7087
}
7188

72-
return this.getDependencyData(name, modulePath, depthInNodeModules);
89+
return this.getDependencyData(depDescription.name, modulePath, depthInNodeModules);
7390
}
7491

7592
private getDependencyData(name: string, directory: string, depth: number): IDependencyData {

0 commit comments

Comments
 (0)