refactor(compiler-cli): extract NgCompilerAdapter interface (#37118)

`NgCompiler` is the heart of ngtsc and can be used to analyze and compile
Angular programs in a variety of environments. Most of these integrations
rely on `NgProgram` and the creation of an `NgCompilerHost` in order to
create a `ts.Program` with the right shape for `NgCompiler`.

However, certain environments (such as the Angular Language Service) have
their own mechanisms for creating `ts.Program`s that don't make use of a
`ts.CompilerHost`. In such environments, an `NgCompilerHost` does not make
sense.

This commit breaks the dependency of `NgCompiler` on `NgCompilerHost` and
extracts the specific interface of the host on which `NgCompiler` depends
into a new interface, `NgCompilerAdapter`. This interface includes methods
from `ts.CompilerHost`, the `ExtendedTsCompilerHost`, as well as APIs from
`NgCompilerHost`.

A consumer such as the language service can implement this API without
needing to jump through hoops to create an `NgCompilerHost` implementation
that somehow wraps its specific environment.

PR Close #37118
This commit is contained in:
Alex Rickabaugh
2020-05-14 11:24:59 -07:00
committed by atscott
parent 965a688c97
commit e648a0c4ca
20 changed files with 185 additions and 88 deletions

View File

@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import {absoluteFrom} from '../../file_system';
import {getSourceFileOrNull, resolveModuleName} from '../../util/src/typescript';
@ -18,8 +19,8 @@ import {getSourceFileOrNull, resolveModuleName} from '../../util/src/typescript'
export class ModuleResolver {
constructor(
private program: ts.Program, private compilerOptions: ts.CompilerOptions,
private host: ts.CompilerHost, private moduleResolutionCache: ts.ModuleResolutionCache|null) {
}
private host: ts.ModuleResolutionHost&Pick<ts.CompilerHost, 'resolveModuleNames'>,
private moduleResolutionCache: ts.ModuleResolutionCache|null) {}
resolveModule(moduleName: string, containingFile: string): ts.SourceFile|null {
const resolved = resolveModuleName(