From 3ae3fa3a1deeca98c7a6d463aa003dba569de2c8 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Fri, 12 Oct 2018 10:37:02 -0400 Subject: [PATCH] fix(@ngtools/webpack): remove usage of private compiler API --- .../webpack/src/angular_compiler_plugin.ts | 92 +++++++------------ 1 file changed, 32 insertions(+), 60 deletions(-) diff --git a/packages/ngtools/webpack/src/angular_compiler_plugin.ts b/packages/ngtools/webpack/src/angular_compiler_plugin.ts index 67ecc6df9d..472603db10 100644 --- a/packages/ngtools/webpack/src/angular_compiler_plugin.ts +++ b/packages/ngtools/webpack/src/angular_compiler_plugin.ts @@ -21,11 +21,11 @@ import { DEFAULT_ERROR_CODE, Diagnostic, EmitFlags, + LazyRoute, Program, SOURCE, UNKNOWN_ERROR_CODE, VERSION, - __NGTOOLS_PRIVATE_API_2, createCompilerHost, createProgram, formatDiagnostics, @@ -162,14 +162,6 @@ export class AngularCompilerPlugin { // Logging. private _logger: logging.Logger; - private get _ngCompilerSupportsNewApi() { - if (this._JitMode) { - return false; - } else { - return !!(this._program as Program).listLazyRoutes; - } - } - constructor(options: AngularCompilerPluginOptions) { this._options = Object.assign({}, options); this._setupOptions(this._options); @@ -426,34 +418,6 @@ export class AngularCompilerPlugin { } } - private _getLazyRoutesFromNgtools() { - try { - time('AngularCompilerPlugin._getLazyRoutesFromNgtools'); - const result = __NGTOOLS_PRIVATE_API_2.listLazyRoutes({ - program: this._getTsProgram(), - host: this._compilerHost, - angularCompilerOptions: Object.assign({}, this._compilerOptions, { - // genDir seems to still be needed in @angular\compiler-cli\src\compiler_host.js:226. - genDir: '', - }), - // TODO: fix compiler-cli typings; entryModule should not be string, but also optional. - // tslint:disable-next-line:no-non-null-assertion - entryModule: this._entryModule!, - }); - timeEnd('AngularCompilerPlugin._getLazyRoutesFromNgtools'); - - return result; - } catch (err) { - // We silence the error that the @angular/router could not be found. In that case, there is - // basically no route supported by the app itself. - if (err.message.startsWith('Could not resolve module @angular/router')) { - return {}; - } else { - throw err; - } - } - } - private _findLazyRoutesInAst(changedFilePaths: string[]): LazyRouteMap { time('AngularCompilerPlugin._findLazyRoutesInAst'); const result: LazyRouteMap = Object.create(null); @@ -471,12 +435,24 @@ export class AngularCompilerPlugin { } private _listLazyRoutesFromProgram(): LazyRouteMap { - const ngProgram = this._program as Program; - if (!ngProgram.listLazyRoutes) { - throw new Error('_listLazyRoutesFromProgram was called with an old program.'); - } + let lazyRoutes: LazyRoute[]; + if (this._JitMode) { + if (!this.entryModule) { + return {}; + } - const lazyRoutes = ngProgram.listLazyRoutes(); + const ngProgram = createProgram({ + rootNames: this._rootNames, + options: { ...this._compilerOptions, genDir: '', collectAllErrors: true }, + host: this._compilerHost, + }); + + lazyRoutes = ngProgram.listLazyRoutes( + this.entryModule.path + '#' + this.entryModule.className, + ); + } else { + lazyRoutes = (this._program as Program).listLazyRoutes(); + } return lazyRoutes.reduce( (acc, curr) => { @@ -890,31 +866,27 @@ export class AngularCompilerPlugin { // If nothing we care about changed and it isn't the first run, don't do anything. if (changedFiles.length === 0 && !this._firstRun) { - return Promise.resolve(); + return; } // Make a new program and load the Angular structure. await this._createOrUpdateProgram(); - if (this.entryModule) { - // Try to find lazy routes if we have an entry module. - // We need to run the `listLazyRoutes` the first time because it also navigates libraries - // and other things that we might miss using the (faster) findLazyRoutesInAst. - // Lazy routes modules will be read with compilerHost and added to the changed files. - if (this._ngCompilerSupportsNewApi) { - this._processLazyRoutes(this._listLazyRoutesFromProgram()); - } else if (this._firstRun) { - this._processLazyRoutes(this._getLazyRoutesFromNgtools()); - } else { - const changedTsFiles = this._getChangedTsFiles(); - if (changedTsFiles.length > 0) { - this._processLazyRoutes(this._findLazyRoutesInAst(changedTsFiles)); - } - } - if (this._options.additionalLazyModules) { - this._processLazyRoutes(this._options.additionalLazyModules); + // Try to find lazy routes if we have an entry module. + // We need to run the `listLazyRoutes` the first time because it also navigates libraries + // and other things that we might miss using the (faster) findLazyRoutesInAst. + // Lazy routes modules will be read with compilerHost and added to the changed files. + if (this._firstRun || !this._JitMode) { + this._processLazyRoutes(this._listLazyRoutesFromProgram()); + } else { + const changedTsFiles = this._getChangedTsFiles(); + if (changedTsFiles.length > 0) { + this._processLazyRoutes(this._findLazyRoutesInAst(changedTsFiles)); } } + if (this._options.additionalLazyModules) { + this._processLazyRoutes(this._options.additionalLazyModules); + } // Emit and report errors.