Skip to content

Commit 704bc85

Browse files
committed
feat(@ngtools/webpack): support custom logging
1 parent ab1dfd0 commit 704bc85

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

packages/ngtools/webpack/src/angular_compiler_plugin.ts

+39-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,13 @@ 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+
InitMessage,
60+
LogMessage,
61+
MESSAGE_KIND,
62+
UpdateMessage,
63+
} from './type_checker';
4964
import {
5065
VirtualFileSystemDecorator,
5166
VirtualWatchFileSystemDecorator,
@@ -59,7 +74,7 @@ import { WebpackInputHost } from './webpack-input-host';
5974

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

62-
export interface ContextElementDependency {}
77+
export interface ContextElementDependency { }
6378

6479
export interface ContextElementDependencyConstructor {
6580
new(modulePath: string, name: string): ContextElementDependency;
@@ -85,6 +100,7 @@ export interface AngularCompilerPluginOptions {
85100
missingTranslation?: string;
86101
platform?: PLATFORM;
87102
nameLazyFiles?: boolean;
103+
logger?: logging.Logger;
88104

89105
// added to the list of lazy routes
90106
additionalLazyModules?: { [module: string]: string };
@@ -141,6 +157,9 @@ export class AngularCompilerPlugin {
141157
private _typeCheckerProcess: ChildProcess | null;
142158
private _forkedTypeCheckerInitialized = false;
143159

160+
// Logging.
161+
private _logger: logging.Logger;
162+
144163
private get _ngCompilerSupportsNewApi() {
145164
if (this._JitMode) {
146165
return false;
@@ -179,6 +198,8 @@ export class AngularCompilerPlugin {
179198

180199
private _setupOptions(options: AngularCompilerPluginOptions) {
181200
time('AngularCompilerPlugin._setupOptions');
201+
this._logger = options.logger || createConsoleLogger();
202+
182203
// Fill in the missing options.
183204
if (!options.hasOwnProperty('tsConfigPath')) {
184205
throw new Error('Must specify "tsConfigPath" in the configuration of @ngtools/webpack.');
@@ -415,7 +436,7 @@ export class AngularCompilerPlugin {
415436
}),
416437
// TODO: fix compiler-cli typings; entryModule should not be string, but also optional.
417438
// tslint:disable-next-line:no-non-null-assertion
418-
entryModule: this._entryModule !,
439+
entryModule: this._entryModule!,
419440
});
420441
timeEnd('AngularCompilerPlugin._getLazyRoutesFromNgtools');
421442

@@ -542,6 +563,18 @@ export class AngularCompilerPlugin {
542563
forkArgs,
543564
forkOptions);
544565

566+
// Handle child messages.
567+
this._typeCheckerProcess.on('message', message => {
568+
switch (message.kind) {
569+
case MESSAGE_KIND.Log:
570+
const logMessage = message as LogMessage;
571+
this._logger.log(logMessage.level, logMessage.message);
572+
break;
573+
default:
574+
throw new Error(`TypeChecker: Unexpected message received: ${message}.`);
575+
}
576+
});
577+
545578
// Handle child process exit.
546579
this._typeCheckerProcess.once('exit', (_, signal) => {
547580
this._typeCheckerProcess = null;
@@ -748,10 +781,10 @@ export class AngularCompilerPlugin {
748781
const name = request.request;
749782
const issuer = request.contextInfo.issuer;
750783
if (name.endsWith('.ts') || name.endsWith('.tsx')
751-
|| (issuer && /\.ts|ngfactory\.js$/.test(issuer))) {
784+
|| (issuer && /\.ts|ngfactory\.js$/.test(issuer))) {
752785
try {
753786
await this.done;
754-
} catch {}
787+
} catch { }
755788
}
756789
}
757790

packages/ngtools/webpack/src/type_checker.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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';
8+
import { logging } from '@angular-devkit/core';
99
import { NodeJsSyncHost } from '@angular-devkit/core/node';
1010
import {
1111
CompilerHost,
@@ -27,6 +27,7 @@ import { CancellationToken, gatherDiagnostics } from './gather_diagnostics';
2727
export enum MESSAGE_KIND {
2828
Init,
2929
Update,
30+
Log,
3031
}
3132

3233
export abstract class TypeCheckerMessage {
@@ -50,6 +51,12 @@ export class UpdateMessage extends TypeCheckerMessage {
5051
}
5152
}
5253

54+
export class LogMessage extends TypeCheckerMessage {
55+
constructor(public level: logging.LogLevel, public message: string) {
56+
super(MESSAGE_KIND.Log);
57+
}
58+
}
59+
5360
export const AUTO_START_ARG = '9d93e901-158a-4cf9-ba1b-2f0582ffcfeb';
5461

5562
export class TypeChecker {
@@ -124,19 +131,25 @@ export class TypeChecker {
124131

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

133140
if (warnings.length > 0) {
134141
const message = formatDiagnostics(warnings);
135-
console.error(terminal.bold(terminal.yellow('WARNING in ' + message)));
142+
this.sendMessage(new LogMessage('warn', 'WARNING in ' + message));
136143
}
137144
}
138145
}
139146

147+
private sendMessage(msg: TypeCheckerMessage) {
148+
if (process.send) {
149+
process.send(msg);
150+
}
151+
}
152+
140153
public update(rootNames: string[], changedCompilationFiles: string[],
141154
cancellationToken: CancellationToken) {
142155
this._update(rootNames, changedCompilationFiles);

0 commit comments

Comments
 (0)