refactor(ngcc): pass dependency info to collectDependencies()
(#34494)
Rather than return a new object of dependency info from calls to `collectDependencies()` we now pass in an object that will be updated with the dependency info. This is in preparation of a change where we will collect dependency information from more than one `DependencyHost`. Also to better fit with this approach the name is changed from `findDependencies()` to `collectDependencies()`. PR Close #34494
This commit is contained in:

committed by
Alex Rickabaugh

parent
0be9456b0a
commit
8573677528
@ -27,7 +27,7 @@ export class CommonJsDependencyHost extends DependencyHostBase {
|
||||
* @param alreadySeen A set that is used to track internal dependencies to prevent getting stuck
|
||||
* in a circular dependency loop.
|
||||
*/
|
||||
protected recursivelyFindDependencies(
|
||||
protected recursivelyCollectDependencies(
|
||||
file: AbsoluteFsPath, dependencies: Set<AbsoluteFsPath>, missing: Set<string>,
|
||||
deepImports: Set<AbsoluteFsPath>, alreadySeen: Set<AbsoluteFsPath>): void {
|
||||
const fromContents = this.fs.readFile(file);
|
||||
@ -53,7 +53,7 @@ export class CommonJsDependencyHost extends DependencyHostBase {
|
||||
const internalDependency = resolvedModule.modulePath;
|
||||
if (!alreadySeen.has(internalDependency)) {
|
||||
alreadySeen.add(internalDependency);
|
||||
this.recursivelyFindDependencies(
|
||||
this.recursivelyCollectDependencies(
|
||||
internalDependency, dependencies, missing, deepImports, alreadySeen);
|
||||
}
|
||||
} else {
|
||||
|
@ -11,7 +11,8 @@ import {resolveFileWithPostfixes} from '../utils';
|
||||
import {ModuleResolver} from './module_resolver';
|
||||
|
||||
export interface DependencyHost {
|
||||
findDependencies(entryPointPath: AbsoluteFsPath): DependencyInfo;
|
||||
collectDependencies(
|
||||
entryPointPath: AbsoluteFsPath, {dependencies, missing, deepImports}: DependencyInfo): void;
|
||||
}
|
||||
|
||||
export interface DependencyInfo {
|
||||
@ -20,6 +21,10 @@ export interface DependencyInfo {
|
||||
deepImports: Set<AbsoluteFsPath>;
|
||||
}
|
||||
|
||||
export function createDependencyInfo(): DependencyInfo {
|
||||
return {dependencies: new Set(), missing: new Set(), deepImports: new Set()};
|
||||
}
|
||||
|
||||
export abstract class DependencyHostBase implements DependencyHost {
|
||||
constructor(protected fs: FileSystem, protected moduleResolver: ModuleResolver) {}
|
||||
|
||||
@ -27,23 +32,19 @@ export abstract class DependencyHostBase implements DependencyHost {
|
||||
* Find all the dependencies for the entry-point at the given path.
|
||||
*
|
||||
* @param entryPointPath The absolute path to the JavaScript file that represents an entry-point.
|
||||
* @returns Information about the dependencies of the entry-point, including those that were
|
||||
* missing or deep imports into other entry-points.
|
||||
* @param dependencyInfo An object containing information about the dependencies of the
|
||||
* entry-point, including those that were missing or deep imports into other entry-points. The
|
||||
* sets in this object will be updated with new information about the entry-point's dependencies.
|
||||
*/
|
||||
findDependencies(entryPointPath: AbsoluteFsPath): DependencyInfo {
|
||||
const dependencies = new Set<AbsoluteFsPath>();
|
||||
const missing = new Set<AbsoluteFsPath|PathSegment>();
|
||||
const deepImports = new Set<AbsoluteFsPath>();
|
||||
|
||||
collectDependencies(
|
||||
entryPointPath: AbsoluteFsPath, {dependencies, missing, deepImports}: DependencyInfo): void {
|
||||
const resolvedFile =
|
||||
resolveFileWithPostfixes(this.fs, entryPointPath, ['', '.js', '/index.js']);
|
||||
if (resolvedFile !== null) {
|
||||
const alreadySeen = new Set<AbsoluteFsPath>();
|
||||
this.recursivelyFindDependencies(
|
||||
this.recursivelyCollectDependencies(
|
||||
resolvedFile, dependencies, missing, deepImports, alreadySeen);
|
||||
}
|
||||
|
||||
return {dependencies, missing, deepImports};
|
||||
}
|
||||
|
||||
/**
|
||||
@ -58,7 +59,7 @@ export abstract class DependencyHostBase implements DependencyHost {
|
||||
* @param alreadySeen A set that is used to track internal dependencies to prevent getting stuck
|
||||
* in a circular dependency loop.
|
||||
*/
|
||||
protected abstract recursivelyFindDependencies(
|
||||
protected abstract recursivelyCollectDependencies(
|
||||
file: AbsoluteFsPath, dependencies: Set<AbsoluteFsPath>, missing: Set<string>,
|
||||
deepImports: Set<AbsoluteFsPath>, alreadySeen: Set<AbsoluteFsPath>): void;
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import {AbsoluteFsPath, FileSystem, resolve} from '../../../src/ngtsc/file_syste
|
||||
import {Logger} from '../logging/logger';
|
||||
import {EntryPoint, EntryPointFormat, SUPPORTED_FORMAT_PROPERTIES, getEntryPointFormat} from '../packages/entry_point';
|
||||
import {PartiallyOrderedList} from '../utils';
|
||||
import {DependencyHost, DependencyInfo} from './dependency_host';
|
||||
import {DependencyHost, DependencyInfo, createDependencyInfo} from './dependency_host';
|
||||
|
||||
const builtinNodeJsModules = new Set<string>(require('module').builtinModules);
|
||||
|
||||
@ -123,7 +123,9 @@ export class DependencyResolver {
|
||||
throw new Error(
|
||||
`Could not find a suitable format for computing dependencies of entry-point: '${entryPoint.path}'.`);
|
||||
}
|
||||
return host.findDependencies(formatInfo.path);
|
||||
const depInfo = createDependencyInfo();
|
||||
host.collectDependencies(formatInfo.path, depInfo);
|
||||
return depInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -26,7 +26,7 @@ export class EsmDependencyHost extends DependencyHostBase {
|
||||
* @param alreadySeen A set that is used to track internal dependencies to prevent getting stuck
|
||||
* in a circular dependency loop.
|
||||
*/
|
||||
protected recursivelyFindDependencies(
|
||||
protected recursivelyCollectDependencies(
|
||||
file: AbsoluteFsPath, dependencies: Set<AbsoluteFsPath>, missing: Set<string>,
|
||||
deepImports: Set<string>, alreadySeen: Set<AbsoluteFsPath>): void {
|
||||
const fromContents = this.fs.readFile(file);
|
||||
@ -52,7 +52,7 @@ export class EsmDependencyHost extends DependencyHostBase {
|
||||
const internalDependency = resolvedModule.modulePath;
|
||||
if (!alreadySeen.has(internalDependency)) {
|
||||
alreadySeen.add(internalDependency);
|
||||
this.recursivelyFindDependencies(
|
||||
this.recursivelyCollectDependencies(
|
||||
internalDependency, dependencies, missing, deepImports, alreadySeen);
|
||||
}
|
||||
} else {
|
||||
|
@ -27,7 +27,7 @@ export class UmdDependencyHost extends DependencyHostBase {
|
||||
* @param alreadySeen A set that is used to track internal dependencies to prevent getting stuck
|
||||
* in a circular dependency loop.
|
||||
*/
|
||||
protected recursivelyFindDependencies(
|
||||
protected recursivelyCollectDependencies(
|
||||
file: AbsoluteFsPath, dependencies: Set<AbsoluteFsPath>, missing: Set<string>,
|
||||
deepImports: Set<string>, alreadySeen: Set<AbsoluteFsPath>): void {
|
||||
const fromContents = this.fs.readFile(file);
|
||||
@ -58,7 +58,7 @@ export class UmdDependencyHost extends DependencyHostBase {
|
||||
const internalDependency = resolvedModule.modulePath;
|
||||
if (!alreadySeen.has(internalDependency)) {
|
||||
alreadySeen.add(internalDependency);
|
||||
this.recursivelyFindDependencies(
|
||||
this.recursivelyCollectDependencies(
|
||||
internalDependency, dependencies, missing, deepImports, alreadySeen);
|
||||
}
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user