Skip to content

Tdermendzhiev/add recursive module resolution #3503

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

Merged
merged 4 commits into from
Apr 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions lib/tools/node-modules/node-modules-dependencies-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as path from "path";
import { NODE_MODULES_FOLDER_NAME, PACKAGE_JSON_FILE_NAME } from "../../constants";

interface IDependencyDescription {
parent: IDependencyDescription;
parentDir: string;
name: string;
depth: number;
Expand All @@ -20,20 +21,22 @@ export class NodeModulesDependenciesBuilder implements INodeModulesDependenciesB

const queue: IDependencyDescription[] = _.keys(dependencies)
.map(dependencyName => ({
parent: null,
parentDir: projectPath,
name: dependencyName,
depth: 0
}));

while (queue.length) {
const currentModule = queue.shift();
const resolvedDependency = this.findModule(rootNodeModulesPath, currentModule.parentDir, currentModule.name, currentModule.depth, resolvedDependencies);
const resolvedDependency = this.findModule(rootNodeModulesPath, currentModule, resolvedDependencies);

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

const shouldAdd = !_.some(queue, element =>
element.parent === dependency.parent &&
element.name === dependency.name &&
element.parentDir === dependency.parentDir &&
element.depth === dependency.depth);
Expand All @@ -50,26 +53,40 @@ export class NodeModulesDependenciesBuilder implements INodeModulesDependenciesB
return resolvedDependencies;
}

private findModule(rootNodeModulesPath: string, parentModulePath: string, name: string, depth: number, resolvedDependencies: IDependencyData[]): IDependencyData {
let modulePath = path.join(parentModulePath, NODE_MODULES_FOLDER_NAME, name); // node_modules/parent/node_modules/<package>
const rootModulesPath = path.join(rootNodeModulesPath, name);
let depthInNodeModules = depth;
private findModule(rootNodeModulesPath: string, depDescription: IDependencyDescription, resolvedDependencies: IDependencyData[]): IDependencyData {
let modulePath = path.join(depDescription.parentDir, NODE_MODULES_FOLDER_NAME, depDescription.name); // node_modules/parent/node_modules/<package>
const rootModulesPath = path.join(rootNodeModulesPath, depDescription.name);
let depthInNodeModules = depDescription.depth;

if (!this.moduleExists(modulePath)) {
modulePath = rootModulesPath; // /node_modules/<package>
if (!this.moduleExists(modulePath)) {
return null;

let moduleExists = false;
let parent = depDescription.parent;

while (parent && !moduleExists) {
modulePath = path.join(depDescription.parent.parentDir, NODE_MODULES_FOLDER_NAME, depDescription.name);
moduleExists = this.moduleExists(modulePath);
if (!moduleExists) {
parent = parent.parent;
}
}

if (!moduleExists) {
modulePath = rootModulesPath; // /node_modules/<package>
if (!this.moduleExists(modulePath)) {
return null;
}
}

depthInNodeModules = 0;
}

if (_.some(resolvedDependencies, r => r.name === name && r.directory === modulePath)) {
if (_.some(resolvedDependencies, r => r.name === depDescription.name && r.directory === modulePath)) {
return null;

}

return this.getDependencyData(name, modulePath, depthInNodeModules);
return this.getDependencyData(depDescription.name, modulePath, depthInNodeModules);
}

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