Skip to content

Commit

Permalink
feat(@ngtools/webpack): support custom logging
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesilva authored and Keen Yee Liau committed Oct 4, 2018
1 parent b89f6b1 commit 5852f39
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 39 deletions.
47 changes: 41 additions & 6 deletions packages/ngtools/webpack/src/angular_compiler_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@
* 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 { Path, dirname, getSystemPath, normalize, resolve, virtualFs } from '@angular-devkit/core';
import {
Path,
dirname,
getSystemPath,
logging,
normalize,
resolve,
virtualFs,
} from '@angular-devkit/core';
import { createConsoleLogger } from '@angular-devkit/core/node';
import {
CompilerHost,
CompilerOptions,
Expand Down Expand Up @@ -45,7 +54,15 @@ import {
replaceServerBootstrap,
} from './transformers';
import { collectDeepNodes } from './transformers/ast_helpers';
import { AUTO_START_ARG, InitMessage, UpdateMessage } from './type_checker';
import {
AUTO_START_ARG,
} from './type_checker';
import {
InitMessage,
LogMessage,
MESSAGE_KIND,
UpdateMessage,
} from './type_checker_messages';
import {
VirtualFileSystemDecorator,
VirtualWatchFileSystemDecorator,
Expand All @@ -59,7 +76,7 @@ import { WebpackInputHost } from './webpack-input-host';

const treeKill = require('tree-kill');

export interface ContextElementDependency {}
export interface ContextElementDependency { }

export interface ContextElementDependencyConstructor {
new(modulePath: string, name: string): ContextElementDependency;
Expand All @@ -85,6 +102,7 @@ export interface AngularCompilerPluginOptions {
missingTranslation?: string;
platform?: PLATFORM;
nameLazyFiles?: boolean;
logger?: logging.Logger;

// added to the list of lazy routes
additionalLazyModules?: { [module: string]: string };
Expand Down Expand Up @@ -141,6 +159,9 @@ export class AngularCompilerPlugin {
private _typeCheckerProcess: ChildProcess | null;
private _forkedTypeCheckerInitialized = false;

// Logging.
private _logger: logging.Logger;

private get _ngCompilerSupportsNewApi() {
if (this._JitMode) {
return false;
Expand Down Expand Up @@ -179,6 +200,8 @@ export class AngularCompilerPlugin {

private _setupOptions(options: AngularCompilerPluginOptions) {
time('AngularCompilerPlugin._setupOptions');
this._logger = options.logger || createConsoleLogger();

// Fill in the missing options.
if (!options.hasOwnProperty('tsConfigPath')) {
throw new Error('Must specify "tsConfigPath" in the configuration of @ngtools/webpack.');
Expand Down Expand Up @@ -415,7 +438,7 @@ export class AngularCompilerPlugin {
}),
// TODO: fix compiler-cli typings; entryModule should not be string, but also optional.
// tslint:disable-next-line:no-non-null-assertion
entryModule: this._entryModule !,
entryModule: this._entryModule!,
});
timeEnd('AngularCompilerPlugin._getLazyRoutesFromNgtools');

Expand Down Expand Up @@ -542,6 +565,18 @@ export class AngularCompilerPlugin {
forkArgs,
forkOptions);

// Handle child messages.
this._typeCheckerProcess.on('message', message => {
switch (message.kind) {
case MESSAGE_KIND.Log:
const logMessage = message as LogMessage;
this._logger.log(logMessage.level, logMessage.message);
break;
default:
throw new Error(`TypeChecker: Unexpected message received: ${message}.`);
}
});

// Handle child process exit.
this._typeCheckerProcess.once('exit', (_, signal) => {
this._typeCheckerProcess = null;
Expand Down Expand Up @@ -748,10 +783,10 @@ export class AngularCompilerPlugin {
const name = request.request;
const issuer = request.contextInfo.issuer;
if (name.endsWith('.ts') || name.endsWith('.tsx')
|| (issuer && /\.ts|ngfactory\.js$/.test(issuer))) {
|| (issuer && /\.ts|ngfactory\.js$/.test(issuer))) {
try {
await this.done;
} catch {}
} catch { }
}
}

Expand Down
40 changes: 9 additions & 31 deletions packages/ngtools/webpack/src/type_checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* 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 { terminal } from '@angular-devkit/core';
import { NodeJsSyncHost } from '@angular-devkit/core/node';
import {
CompilerHost,
Expand All @@ -19,37 +18,10 @@ import * as ts from 'typescript';
import { time, timeEnd } from './benchmark';
import { WebpackCompilerHost } from './compiler_host';
import { CancellationToken, gatherDiagnostics } from './gather_diagnostics';
import { LogMessage, TypeCheckerMessage } from './type_checker_messages';


// This file should run in a child process with the AUTO_START_ARG argument


export enum MESSAGE_KIND {
Init,
Update,
}

export abstract class TypeCheckerMessage {
constructor(public kind: MESSAGE_KIND) { }
}

export class InitMessage extends TypeCheckerMessage {
constructor(
public compilerOptions: ts.CompilerOptions,
public basePath: string,
public jitMode: boolean,
public rootNames: string[],
) {
super(MESSAGE_KIND.Init);
}
}

export class UpdateMessage extends TypeCheckerMessage {
constructor(public rootNames: string[], public changedCompilationFiles: string[]) {
super(MESSAGE_KIND.Update);
}
}

export const AUTO_START_ARG = '9d93e901-158a-4cf9-ba1b-2f0582ffcfeb';

export class TypeChecker {
Expand Down Expand Up @@ -124,19 +96,25 @@ export class TypeChecker {

if (errors.length > 0) {
const message = formatDiagnostics(errors);
console.error(terminal.bold(terminal.red('ERROR in ' + message)));
this.sendMessage(new LogMessage('error', 'ERROR in ' + message));
} else {
// Reset the changed file tracker only if there are no errors.
this._compilerHost.resetChangedFileTracker();
}

if (warnings.length > 0) {
const message = formatDiagnostics(warnings);
console.error(terminal.bold(terminal.yellow('WARNING in ' + message)));
this.sendMessage(new LogMessage('warn', 'WARNING in ' + message));
}
}
}

private sendMessage(msg: TypeCheckerMessage) {
if (process.send) {
process.send(msg);
}
}

public update(rootNames: string[], changedCompilationFiles: string[],
cancellationToken: CancellationToken) {
this._update(rootNames, changedCompilationFiles);
Expand Down
45 changes: 45 additions & 0 deletions packages/ngtools/webpack/src/type_checker_messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @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 { logging } from '@angular-devkit/core';
import * as ts from 'typescript';


export enum MESSAGE_KIND {
Init,
Update,
Log,
}

export abstract class TypeCheckerMessage {
constructor(public kind: MESSAGE_KIND) { }
}

export class InitMessage extends TypeCheckerMessage {
constructor(
public compilerOptions: ts.CompilerOptions,
public basePath: string,
public jitMode: boolean,
public rootNames: string[],
public hostReplacementPaths: { [path: string]: string },
) {
super(MESSAGE_KIND.Init);
}
}

export class UpdateMessage extends TypeCheckerMessage {
constructor(public rootNames: string[], public changedCompilationFiles: string[]) {
super(MESSAGE_KIND.Update);
}
}

export class LogMessage extends TypeCheckerMessage {
constructor(public level: logging.LogLevel, public message: string) {
super(MESSAGE_KIND.Log);
}
}

6 changes: 4 additions & 2 deletions packages/ngtools/webpack/src/type_checker_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import { time, timeEnd } from './benchmark';
import { CancellationToken } from './gather_diagnostics';
import {
AUTO_START_ARG,
TypeChecker,
} from './type_checker';
import {
InitMessage,
MESSAGE_KIND,
TypeChecker,
TypeCheckerMessage,
UpdateMessage,
} from './type_checker';
} from './type_checker_messages';

let typeChecker: TypeChecker;
let lastCancellationToken: CancellationToken;
Expand Down

0 comments on commit 5852f39

Please sign in to comment.