Skip to content

Commit 5852f39

Browse files
filipesilvaKeen Yee Liau
authored and
Keen Yee Liau
committed
feat(@ngtools/webpack): support custom logging
1 parent b89f6b1 commit 5852f39

File tree

4 files changed

+99
-39
lines changed

4 files changed

+99
-39
lines changed

packages/ngtools/webpack/src/angular_compiler_plugin.ts

+41-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { Path, dirname, getSystemPath, normalize, resolve, virtualFs } from '@angular-devkit/core';
8+
import {
9+
Path,
10+
dirname,
11+
getSystemPath,
12+
logging,
13+
normalize,
14+
resolve,
15+
virtualFs,
16+
} from '@angular-devkit/core';
17+
import { createConsoleLogger } from '@angular-devkit/core/node';
918
import {
1019
CompilerHost,
1120
CompilerOptions,
@@ -45,7 +54,15 @@ import {
4554
replaceServerBootstrap,
4655
} from './transformers';
4756
import { collectDeepNodes } from './transformers/ast_helpers';
48-
import { AUTO_START_ARG, InitMessage, UpdateMessage } from './type_checker';
57+
import {
58+
AUTO_START_ARG,
59+
} from './type_checker';
60+
import {
61+
InitMessage,
62+
LogMessage,
63+
MESSAGE_KIND,
64+
UpdateMessage,
65+
} from './type_checker_messages';
4966
import {
5067
VirtualFileSystemDecorator,
5168
VirtualWatchFileSystemDecorator,
@@ -59,7 +76,7 @@ import { WebpackInputHost } from './webpack-input-host';
5976

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

62-
export interface ContextElementDependency {}
79+
export interface ContextElementDependency { }
6380

6481
export interface ContextElementDependencyConstructor {
6582
new(modulePath: string, name: string): ContextElementDependency;
@@ -85,6 +102,7 @@ export interface AngularCompilerPluginOptions {
85102
missingTranslation?: string;
86103
platform?: PLATFORM;
87104
nameLazyFiles?: boolean;
105+
logger?: logging.Logger;
88106

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

162+
// Logging.
163+
private _logger: logging.Logger;
164+
144165
private get _ngCompilerSupportsNewApi() {
145166
if (this._JitMode) {
146167
return false;
@@ -179,6 +200,8 @@ export class AngularCompilerPlugin {
179200

180201
private _setupOptions(options: AngularCompilerPluginOptions) {
181202
time('AngularCompilerPlugin._setupOptions');
203+
this._logger = options.logger || createConsoleLogger();
204+
182205
// Fill in the missing options.
183206
if (!options.hasOwnProperty('tsConfigPath')) {
184207
throw new Error('Must specify "tsConfigPath" in the configuration of @ngtools/webpack.');
@@ -415,7 +438,7 @@ export class AngularCompilerPlugin {
415438
}),
416439
// TODO: fix compiler-cli typings; entryModule should not be string, but also optional.
417440
// tslint:disable-next-line:no-non-null-assertion
418-
entryModule: this._entryModule !,
441+
entryModule: this._entryModule!,
419442
});
420443
timeEnd('AngularCompilerPlugin._getLazyRoutesFromNgtools');
421444

@@ -542,6 +565,18 @@ export class AngularCompilerPlugin {
542565
forkArgs,
543566
forkOptions);
544567

568+
// Handle child messages.
569+
this._typeCheckerProcess.on('message', message => {
570+
switch (message.kind) {
571+
case MESSAGE_KIND.Log:
572+
const logMessage = message as LogMessage;
573+
this._logger.log(logMessage.level, logMessage.message);
574+
break;
575+
default:
576+
throw new Error(`TypeChecker: Unexpected message received: ${message}.`);
577+
}
578+
});
579+
545580
// Handle child process exit.
546581
this._typeCheckerProcess.once('exit', (_, signal) => {
547582
this._typeCheckerProcess = null;
@@ -748,10 +783,10 @@ export class AngularCompilerPlugin {
748783
const name = request.request;
749784
const issuer = request.contextInfo.issuer;
750785
if (name.endsWith('.ts') || name.endsWith('.tsx')
751-
|| (issuer && /\.ts|ngfactory\.js$/.test(issuer))) {
786+
|| (issuer && /\.ts|ngfactory\.js$/.test(issuer))) {
752787
try {
753788
await this.done;
754-
} catch {}
789+
} catch { }
755790
}
756791
}
757792

packages/ngtools/webpack/src/type_checker.ts

+9-31
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { terminal } from '@angular-devkit/core';
98
import { NodeJsSyncHost } from '@angular-devkit/core/node';
109
import {
1110
CompilerHost,
@@ -19,37 +18,10 @@ import * as ts from 'typescript';
1918
import { time, timeEnd } from './benchmark';
2019
import { WebpackCompilerHost } from './compiler_host';
2120
import { CancellationToken, gatherDiagnostics } from './gather_diagnostics';
21+
import { LogMessage, TypeCheckerMessage } from './type_checker_messages';
2222

2323

2424
// This file should run in a child process with the AUTO_START_ARG argument
25-
26-
27-
export enum MESSAGE_KIND {
28-
Init,
29-
Update,
30-
}
31-
32-
export abstract class TypeCheckerMessage {
33-
constructor(public kind: MESSAGE_KIND) { }
34-
}
35-
36-
export class InitMessage extends TypeCheckerMessage {
37-
constructor(
38-
public compilerOptions: ts.CompilerOptions,
39-
public basePath: string,
40-
public jitMode: boolean,
41-
public rootNames: string[],
42-
) {
43-
super(MESSAGE_KIND.Init);
44-
}
45-
}
46-
47-
export class UpdateMessage extends TypeCheckerMessage {
48-
constructor(public rootNames: string[], public changedCompilationFiles: string[]) {
49-
super(MESSAGE_KIND.Update);
50-
}
51-
}
52-
5325
export const AUTO_START_ARG = '9d93e901-158a-4cf9-ba1b-2f0582ffcfeb';
5426

5527
export class TypeChecker {
@@ -124,19 +96,25 @@ export class TypeChecker {
12496

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

133105
if (warnings.length > 0) {
134106
const message = formatDiagnostics(warnings);
135-
console.error(terminal.bold(terminal.yellow('WARNING in ' + message)));
107+
this.sendMessage(new LogMessage('warn', 'WARNING in ' + message));
136108
}
137109
}
138110
}
139111

112+
private sendMessage(msg: TypeCheckerMessage) {
113+
if (process.send) {
114+
process.send(msg);
115+
}
116+
}
117+
140118
public update(rootNames: string[], changedCompilationFiles: string[],
141119
cancellationToken: CancellationToken) {
142120
this._update(rootNames, changedCompilationFiles);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { logging } from '@angular-devkit/core';
9+
import * as ts from 'typescript';
10+
11+
12+
export enum MESSAGE_KIND {
13+
Init,
14+
Update,
15+
Log,
16+
}
17+
18+
export abstract class TypeCheckerMessage {
19+
constructor(public kind: MESSAGE_KIND) { }
20+
}
21+
22+
export class InitMessage extends TypeCheckerMessage {
23+
constructor(
24+
public compilerOptions: ts.CompilerOptions,
25+
public basePath: string,
26+
public jitMode: boolean,
27+
public rootNames: string[],
28+
public hostReplacementPaths: { [path: string]: string },
29+
) {
30+
super(MESSAGE_KIND.Init);
31+
}
32+
}
33+
34+
export class UpdateMessage extends TypeCheckerMessage {
35+
constructor(public rootNames: string[], public changedCompilationFiles: string[]) {
36+
super(MESSAGE_KIND.Update);
37+
}
38+
}
39+
40+
export class LogMessage extends TypeCheckerMessage {
41+
constructor(public level: logging.LogLevel, public message: string) {
42+
super(MESSAGE_KIND.Log);
43+
}
44+
}
45+

packages/ngtools/webpack/src/type_checker_worker.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import { time, timeEnd } from './benchmark';
1010
import { CancellationToken } from './gather_diagnostics';
1111
import {
1212
AUTO_START_ARG,
13+
TypeChecker,
14+
} from './type_checker';
15+
import {
1316
InitMessage,
1417
MESSAGE_KIND,
15-
TypeChecker,
1618
TypeCheckerMessage,
1719
UpdateMessage,
18-
} from './type_checker';
20+
} from './type_checker_messages';
1921

2022
let typeChecker: TypeChecker;
2123
let lastCancellationToken: CancellationToken;

0 commit comments

Comments
 (0)