refactor(ivy): ngcc - move the dependency resolving stuff around (#29643)

For UMD/RequireJS support we will need to have multiple
`DependencyHost` implementations. This commit  prepares the
ground for that.

PR Close #29643
This commit is contained in:
Pete Bacon Darwin 2019-04-28 20:47:57 +01:00 committed by Andrew Kushnir
parent 5ced8fbbd5
commit 1fd2cc6340
10 changed files with 89 additions and 52 deletions

View File

@ -0,0 +1,19 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AbsoluteFsPath} from '../../../src/ngtsc/path';
export interface DependencyHost {
findDependencies(entryPointPath: AbsoluteFsPath): DependencyInfo;
}
export interface DependencyInfo {
dependencies: Set<AbsoluteFsPath>;
missing: Set<string>;
deepImports: Set<string>;
}

View File

@ -11,9 +11,10 @@ import {resolve} from 'path';
import {AbsoluteFsPath} from '../../../src/ngtsc/path';
import {Logger} from '../logging/logger';
import {EntryPoint, EntryPointJsonProperty, getEntryPointFormat} from '../packages/entry_point';
import {DependencyHost} from './dependency_host';
import {EntryPoint, EntryPointJsonProperty, getEntryPointFormat} from './entry_point';
/**
@ -119,7 +120,7 @@ export class DependencyResolver {
// Now add the dependencies between them
angularEntryPoints.forEach(entryPoint => {
const entryPointPath = getEntryPointPath(entryPoint);
const {dependencies, missing, deepImports} = this.host.computeDependencies(entryPointPath);
const {dependencies, missing, deepImports} = this.host.findDependencies(entryPointPath);
if (missing.size > 0) {
// This entry point has dependencies that are missing

View File

@ -11,18 +11,37 @@ import * as ts from 'typescript';
import {AbsoluteFsPath} from '../../../src/ngtsc/path';
import {DependencyHost, DependencyInfo} from './dependency_host';
import {ModuleResolver, ResolvedDeepImport, ResolvedRelativeModule} from './module_resolver';
/**
* Helper functions for computing dependencies.
*/
export class DependencyHost {
export class EsmDependencyHost implements DependencyHost {
constructor(private moduleResolver: ModuleResolver) {}
/**
* Get a list of the resolved paths to all the dependencies of this entry point.
* @param from An absolute path to the file whose dependencies we want to get.
* 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.
*/
findDependencies(entryPointPath: AbsoluteFsPath): DependencyInfo {
const dependencies = new Set<AbsoluteFsPath>();
const missing = new Set<string>();
const deepImports = new Set<string>();
const alreadySeen = new Set<AbsoluteFsPath>();
this.recursivelyFindDependencies(
entryPointPath, dependencies, missing, deepImports, alreadySeen);
return {dependencies, missing, deepImports};
}
/**
* Compute the dependencies of the given file.
*
* @param file An absolute path to the file whose dependencies we want to get.
* @param dependencies A set that will have the absolute paths of resolved entry points added to
* it.
* @param missing A set that will have the dependencies that could not be found added to it.
@ -32,19 +51,17 @@ export class DependencyHost {
* in a
* circular dependency loop.
*/
computeDependencies(
from: AbsoluteFsPath, dependencies: Set<AbsoluteFsPath> = new Set(),
missing: Set<string> = new Set(), deepImports: Set<string> = new Set(),
alreadySeen: Set<AbsoluteFsPath> = new Set()):
{dependencies: Set<AbsoluteFsPath>, missing: Set<string>, deepImports: Set<string>} {
const fromContents = fs.readFileSync(from, 'utf8');
private recursivelyFindDependencies(
file: AbsoluteFsPath, dependencies: Set<AbsoluteFsPath>, missing: Set<string>,
deepImports: Set<string>, alreadySeen: Set<AbsoluteFsPath>): void {
const fromContents = fs.readFileSync(file, 'utf8');
if (!this.hasImportOrReexportStatements(fromContents)) {
return {dependencies, missing, deepImports};
return;
}
// Parse the source into a TypeScript AST and then walk it looking for imports and re-exports.
const sf =
ts.createSourceFile(from, fromContents, ts.ScriptTarget.ES2015, false, ts.ScriptKind.JS);
ts.createSourceFile(file, fromContents, ts.ScriptTarget.ES2015, false, ts.ScriptKind.JS);
sf.statements
// filter out statements that are not imports or reexports
.filter(this.isStringImportOrReexport)
@ -52,13 +69,13 @@ export class DependencyHost {
.map(stmt => stmt.moduleSpecifier.text)
// Resolve this module id into an absolute path
.forEach(importPath => {
const resolvedModule = this.moduleResolver.resolveModuleImport(importPath, from);
const resolvedModule = this.moduleResolver.resolveModuleImport(importPath, file);
if (resolvedModule) {
if (resolvedModule instanceof ResolvedRelativeModule) {
const internalDependency = resolvedModule.modulePath;
if (!alreadySeen.has(internalDependency)) {
alreadySeen.add(internalDependency);
this.computeDependencies(
this.recursivelyFindDependencies(
internalDependency, dependencies, missing, deepImports, alreadySeen);
}
} else {
@ -72,7 +89,6 @@ export class DependencyHost {
missing.add(importPath);
}
});
return {dependencies, missing, deepImports};
}
/**

View File

@ -11,15 +11,15 @@ import {readFileSync} from 'fs';
import {AbsoluteFsPath} from '../../src/ngtsc/path';
import {DependencyResolver} from './dependencies/dependency_resolver';
import {EsmDependencyHost} from './dependencies/esm_dependency_host';
import {ModuleResolver} from './dependencies/module_resolver';
import {ConsoleLogger, LogLevel} from './logging/console_logger';
import {Logger} from './logging/logger';
import {hasBeenProcessed, markAsProcessed} from './packages/build_marker';
import {DependencyHost} from './packages/dependency_host';
import {DependencyResolver} from './packages/dependency_resolver';
import {EntryPointFormat, EntryPointJsonProperty, SUPPORTED_FORMAT_PROPERTIES, getEntryPointFormat} from './packages/entry_point';
import {makeEntryPointBundle} from './packages/entry_point_bundle';
import {EntryPointFinder} from './packages/entry_point_finder';
import {ModuleResolver} from './packages/module_resolver';
import {Transformer} from './packages/transformer';
import {PathMappings} from './utils';
import {FileWriter} from './writing/file_writer';
@ -27,6 +27,7 @@ import {InPlaceFileWriter} from './writing/in_place_file_writer';
import {NewEntryPointFileWriter} from './writing/new_entry_point_file_writer';
/**
* The options to configure the ngcc compiler.
*/
@ -81,7 +82,7 @@ export function mainNgcc(
logger = new ConsoleLogger(LogLevel.info), pathMappings}: NgccOptions): void {
const transformer = new Transformer(logger);
const moduleResolver = new ModuleResolver(pathMappings);
const host = new DependencyHost(moduleResolver);
const host = new EsmDependencyHost(moduleResolver);
const resolver = new DependencyResolver(logger, host);
const finder = new EntryPointFinder(logger, resolver);
const fileWriter = getFileWriter(createNewEntryPointFormats);

View File

@ -10,10 +10,10 @@ import * as fs from 'fs';
import {join, resolve} from 'path';
import {AbsoluteFsPath} from '../../../src/ngtsc/path';
import {DependencyResolver, SortedEntryPointsInfo} from '../dependencies/dependency_resolver';
import {Logger} from '../logging/logger';
import {PathMappings} from '../utils';
import {DependencyResolver, SortedEntryPointsInfo} from './dependency_resolver';
import {EntryPoint, getEntryPointInfo} from './entry_point';

View File

@ -6,19 +6,19 @@
* found in the LICENSE file at https://angular.io/license
*/
import {AbsoluteFsPath} from '../../../src/ngtsc/path';
import {DependencyHost} from '../../src/packages/dependency_host';
import {DependencyResolver, SortedEntryPointsInfo} from '../../src/packages/dependency_resolver';
import {DependencyResolver, SortedEntryPointsInfo} from '../../src/dependencies/dependency_resolver';
import {EsmDependencyHost} from '../../src/dependencies/esm_dependency_host';
import {ModuleResolver} from '../../src/dependencies/module_resolver';
import {EntryPoint} from '../../src/packages/entry_point';
import {ModuleResolver} from '../../src/packages/module_resolver';
import {MockLogger} from '../helpers/mock_logger';
const _ = AbsoluteFsPath.from;
describe('DependencyResolver', () => {
let host: DependencyHost;
let host: EsmDependencyHost;
let resolver: DependencyResolver;
beforeEach(() => {
host = new DependencyHost(new ModuleResolver());
host = new EsmDependencyHost(new ModuleResolver());
resolver = new DependencyResolver(new MockLogger(), host);
});
describe('sortEntryPointsByDependency()', () => {
@ -57,13 +57,13 @@ describe('DependencyResolver', () => {
};
it('should order the entry points by their dependency on each other', () => {
spyOn(host, 'computeDependencies').and.callFake(createFakeComputeDependencies(dependencies));
spyOn(host, 'findDependencies').and.callFake(createFakeComputeDependencies(dependencies));
const result = resolver.sortEntryPointsByDependency([fifth, first, fourth, second, third]);
expect(result.entryPoints).toEqual([fifth, fourth, third, second, first]);
});
it('should remove entry-points that have missing direct dependencies', () => {
spyOn(host, 'computeDependencies').and.callFake(createFakeComputeDependencies({
spyOn(host, 'findDependencies').and.callFake(createFakeComputeDependencies({
[_('/first/index.js')]: {resolved: [], missing: ['/missing']},
[_('/second/sub/index.js')]: {resolved: [], missing: []},
}));
@ -75,7 +75,7 @@ describe('DependencyResolver', () => {
});
it('should remove entry points that depended upon an invalid entry-point', () => {
spyOn(host, 'computeDependencies').and.callFake(createFakeComputeDependencies({
spyOn(host, 'findDependencies').and.callFake(createFakeComputeDependencies({
[_('/first/index.js')]: {resolved: [second.path], missing: []},
[_('/second/sub/index.js')]: {resolved: [], missing: ['/missing']},
[_('/third/index.js')]: {resolved: [], missing: []},
@ -90,7 +90,7 @@ describe('DependencyResolver', () => {
});
it('should remove entry points that will depend upon an invalid entry-point', () => {
spyOn(host, 'computeDependencies').and.callFake(createFakeComputeDependencies({
spyOn(host, 'findDependencies').and.callFake(createFakeComputeDependencies({
[_('/first/index.js')]: {resolved: [second.path], missing: []},
[_('/second/sub/index.js')]: {resolved: [], missing: ['/missing']},
[_('/third/index.js')]: {resolved: [], missing: []},
@ -111,7 +111,7 @@ describe('DependencyResolver', () => {
});
it('should capture any dependencies that were ignored', () => {
spyOn(host, 'computeDependencies').and.callFake(createFakeComputeDependencies(dependencies));
spyOn(host, 'findDependencies').and.callFake(createFakeComputeDependencies(dependencies));
const result = resolver.sortEntryPointsByDependency([fifth, first, fourth, second, third]);
expect(result.ignoredDependencies).toEqual([
{entryPoint: first, dependencyPath: '/ignored-1'},
@ -120,7 +120,7 @@ describe('DependencyResolver', () => {
});
it('should only return dependencies of the target, if provided', () => {
spyOn(host, 'computeDependencies').and.callFake(createFakeComputeDependencies(dependencies));
spyOn(host, 'findDependencies').and.callFake(createFakeComputeDependencies(dependencies));
const entryPoints = [fifth, first, fourth, second, third];
let sorted: SortedEntryPointsInfo;

View File

@ -9,14 +9,14 @@ import * as mockFs from 'mock-fs';
import * as ts from 'typescript';
import {AbsoluteFsPath} from '../../../src/ngtsc/path';
import {DependencyHost} from '../../src/packages/dependency_host';
import {ModuleResolver} from '../../src/packages/module_resolver';
import {EsmDependencyHost} from '../../src/dependencies/esm_dependency_host';
import {ModuleResolver} from '../../src/dependencies/module_resolver';
const _ = AbsoluteFsPath.from;
describe('DependencyHost', () => {
let host: DependencyHost;
beforeEach(() => host = new DependencyHost(new ModuleResolver()));
let host: EsmDependencyHost;
beforeEach(() => host = new EsmDependencyHost(new ModuleResolver()));
describe('getDependencies()', () => {
beforeEach(createMockFileSystem);
@ -25,13 +25,13 @@ describe('DependencyHost', () => {
it('should not generate a TS AST if the source does not contain any imports or re-exports',
() => {
spyOn(ts, 'createSourceFile');
host.computeDependencies(_('/no/imports/or/re-exports/index.js'));
host.findDependencies(_('/no/imports/or/re-exports/index.js'));
expect(ts.createSourceFile).not.toHaveBeenCalled();
});
it('should resolve all the external imports of the source file', () => {
const {dependencies, missing, deepImports} =
host.computeDependencies(_('/external/imports/index.js'));
host.findDependencies(_('/external/imports/index.js'));
expect(dependencies.size).toBe(2);
expect(missing.size).toBe(0);
expect(deepImports.size).toBe(0);
@ -41,7 +41,7 @@ describe('DependencyHost', () => {
it('should resolve all the external re-exports of the source file', () => {
const {dependencies, missing, deepImports} =
host.computeDependencies(_('/external/re-exports/index.js'));
host.findDependencies(_('/external/re-exports/index.js'));
expect(dependencies.size).toBe(2);
expect(missing.size).toBe(0);
expect(deepImports.size).toBe(0);
@ -51,7 +51,7 @@ describe('DependencyHost', () => {
it('should capture missing external imports', () => {
const {dependencies, missing, deepImports} =
host.computeDependencies(_('/external/imports-missing/index.js'));
host.findDependencies(_('/external/imports-missing/index.js'));
expect(dependencies.size).toBe(1);
expect(dependencies.has(_('/node_modules/lib-1'))).toBe(true);
@ -65,7 +65,7 @@ describe('DependencyHost', () => {
// is found that does not map to an entry-point but still exists on disk, i.e. a deep import.
// Such deep imports are captured for diagnostics purposes.
const {dependencies, missing, deepImports} =
host.computeDependencies(_('/external/deep-import/index.js'));
host.findDependencies(_('/external/deep-import/index.js'));
expect(dependencies.size).toBe(0);
expect(missing.size).toBe(0);
@ -75,7 +75,7 @@ describe('DependencyHost', () => {
it('should recurse into internal dependencies', () => {
const {dependencies, missing, deepImports} =
host.computeDependencies(_('/internal/outer/index.js'));
host.findDependencies(_('/internal/outer/index.js'));
expect(dependencies.size).toBe(1);
expect(dependencies.has(_('/node_modules/lib-1/sub-1'))).toBe(true);
@ -85,7 +85,7 @@ describe('DependencyHost', () => {
it('should handle circular internal dependencies', () => {
const {dependencies, missing, deepImports} =
host.computeDependencies(_('/internal/circular-a/index.js'));
host.findDependencies(_('/internal/circular-a/index.js'));
expect(dependencies.size).toBe(2);
expect(dependencies.has(_('/node_modules/lib-1'))).toBe(true);
expect(dependencies.has(_('/node_modules/lib-1/sub-1'))).toBe(true);
@ -94,15 +94,14 @@ describe('DependencyHost', () => {
});
it('should support `paths` alias mappings when resolving modules', () => {
host = new DependencyHost(new ModuleResolver({
host = new EsmDependencyHost(new ModuleResolver({
baseUrl: '/dist',
paths: {
'@app/*': ['*'],
'@lib/*/test': ['lib/*/test'],
}
}));
const {dependencies, missing, deepImports} =
host.computeDependencies(_('/path-alias/index.js'));
const {dependencies, missing, deepImports} = host.findDependencies(_('/path-alias/index.js'));
expect(dependencies.size).toBe(4);
expect(dependencies.has(_('/dist/components'))).toBe(true);
expect(dependencies.has(_('/dist/shared'))).toBe(true);

View File

@ -9,7 +9,7 @@
import * as mockFs from 'mock-fs';
import {AbsoluteFsPath} from '../../../src/ngtsc/path';
import {ModuleResolver, ResolvedDeepImport, ResolvedExternalModule, ResolvedRelativeModule} from '../../src/packages/module_resolver';
import {ModuleResolver, ResolvedDeepImport, ResolvedExternalModule, ResolvedRelativeModule} from '../../src/dependencies/module_resolver';
const _ = AbsoluteFsPath.from;

View File

@ -9,11 +9,11 @@
import * as mockFs from 'mock-fs';
import {AbsoluteFsPath} from '../../../src/ngtsc/path';
import {DependencyHost} from '../../src/packages/dependency_host';
import {DependencyResolver} from '../../src/packages/dependency_resolver';
import {DependencyResolver} from '../../src/dependencies/dependency_resolver';
import {EsmDependencyHost} from '../../src/dependencies/esm_dependency_host';
import {ModuleResolver} from '../../src/dependencies/module_resolver';
import {EntryPoint} from '../../src/packages/entry_point';
import {EntryPointFinder} from '../../src/packages/entry_point_finder';
import {ModuleResolver} from '../../src/packages/module_resolver';
import {MockLogger} from '../helpers/mock_logger';
const _ = AbsoluteFsPath.from;
@ -22,7 +22,8 @@ describe('findEntryPoints()', () => {
let resolver: DependencyResolver;
let finder: EntryPointFinder;
beforeEach(() => {
resolver = new DependencyResolver(new MockLogger(), new DependencyHost(new ModuleResolver()));
resolver =
new DependencyResolver(new MockLogger(), new EsmDependencyHost(new ModuleResolver()));
spyOn(resolver, 'sortEntryPointsByDependency').and.callFake((entryPoints: EntryPoint[]) => {
return {entryPoints, ignoredEntryPoints: [], ignoredDependencies: []};
});