Skip to content

Commit 7db6ee7

Browse files
committed
refactor(@ngtools/webpack): add webpack diagnostic helpers
1 parent c4473ee commit 7db6ee7

File tree

3 files changed

+76
-19
lines changed

3 files changed

+76
-19
lines changed

packages/ngtools/webpack/src/angular_compiler_plugin.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ import {
7979
NodeWatchFileSystemInterface,
8080
NormalModuleFactoryRequest,
8181
} from './webpack';
82+
import { addError, addWarning } from './webpack-diagnostics';
8283
import { createWebpackInputHost } from './webpack-input-host';
84+
import { isWebpackFiveOrHigher } from './webpack-version';
8385

8486
export class AngularCompilerPlugin {
8587
private _options: AngularCompilerPluginOptions;
@@ -117,8 +119,8 @@ export class AngularCompilerPlugin {
117119
private _firstRun = true;
118120
private _donePromise: Promise<void> | null = null;
119121
private _normalizedLocale: string | null = null;
120-
private _warnings: (string | Error)[] = [];
121-
private _errors: (string | Error)[] = [];
122+
private _warnings: string[] = [];
123+
private _errors: string[] = [];
122124
private _contextElementDependencyConstructor!: ContextElementDependencyConstructor;
123125

124126
// TypeChecker process.
@@ -270,24 +272,22 @@ export class AngularCompilerPlugin {
270272
if (this._discoverLazyRoutes === false && this.options.additionalLazyModuleResources
271273
&& this.options.additionalLazyModuleResources.length > 0) {
272274
this._warnings.push(
273-
new Error(`Lazy route discovery is disabled but additional Lazy Module Resources were`
274-
+ ` provided. These will be ignored.`),
275+
`Lazy route discovery is disabled but additional Lazy Module Resources were` +
276+
` provided. These will be ignored.`,
275277
);
276278
}
277279

278280
if (this._compilerOptions.strictMetadataEmit) {
279281
this._warnings.push(
280-
new Error(
281-
`Using Angular compiler option 'strictMetadataEmit' for applications might cause undefined behavior.`,
282-
),
282+
`Using Angular compiler option 'strictMetadataEmit' for applications might cause undefined behavior.`,
283283
);
284284
}
285285

286286
if (this._discoverLazyRoutes === false && this.options.additionalLazyModules
287287
&& Object.keys(this.options.additionalLazyModules).length > 0) {
288288
this._warnings.push(
289-
new Error(`Lazy route discovery is disabled but additional lazy modules were provided.`
290-
+ `These will be ignored.`),
289+
`Lazy route discovery is disabled but additional lazy modules were provided.` +
290+
`These will be ignored.`,
291291
);
292292
}
293293

@@ -544,9 +544,9 @@ export class AngularCompilerPlugin {
544544
if (this._lazyRoutes[moduleKey] !== modulePath) {
545545
// Found a duplicate, this is an error.
546546
this._warnings.push(
547-
new Error(`Duplicated path in loadChildren detected during a rebuild. `
548-
+ `We will take the latest version detected and override it to save rebuild time. `
549-
+ `You should perform a full build to validate that your routes don't overlap.`),
547+
`Duplicated path in loadChildren detected during a rebuild. ` +
548+
`We will take the latest version detected and override it to save rebuild time. ` +
549+
`You should perform a full build to validate that your routes don't overlap.`,
550550
);
551551
}
552552
} else {
@@ -679,9 +679,10 @@ export class AngularCompilerPlugin {
679679
// Anything that remains is unused, because it wasn't referenced directly or transitively
680680
// on the files in the compilation.
681681
for (const fileName of unusedSourceFileNames) {
682-
compilation.warnings.push(
682+
addWarning(
683+
compilation,
683684
`${fileName} is part of the TypeScript compilation but it's unused.\n` +
684-
`Add only entry points to the 'files' or 'include' properties in your tsconfig.`,
685+
`Add only entry points to the 'files' or 'include' properties in your tsconfig.`,
685686
);
686687
this._unusedFiles.add(fileName);
687688
// Remove the truly unused from the type dep list.
@@ -965,7 +966,9 @@ export class AngularCompilerPlugin {
965966
}
966967
}
967968

968-
return request;
969+
if (!isWebpackFiveOrHigher()) {
970+
return request;
971+
}
969972
},
970973
);
971974
});
@@ -1010,16 +1013,16 @@ export class AngularCompilerPlugin {
10101013
await this._update();
10111014
this.pushCompilationErrors(compilation);
10121015
} catch (err) {
1013-
compilation.errors.push(err);
1016+
addError(compilation, err.message || err);
10141017
this.pushCompilationErrors(compilation);
10151018
}
10161019

10171020
timeEnd('AngularCompilerPlugin._make');
10181021
}
10191022

10201023
private pushCompilationErrors(compilation: compilation.Compilation) {
1021-
compilation.errors.push(...this._errors);
1022-
compilation.warnings.push(...this._warnings);
1024+
this._errors.forEach((error) => addError(compilation, error));
1025+
this._warnings.forEach((warning) => addWarning(compilation, warning));
10231026
this._errors = [];
10241027
this._warnings = [];
10251028
}
@@ -1160,7 +1163,7 @@ export class AngularCompilerPlugin {
11601163
// Report any diagnostics.
11611164
reportDiagnostics(
11621165
diagnostics,
1163-
msg => this._errors.push(new Error(msg)),
1166+
msg => this._errors.push(msg),
11641167
msg => this._warnings.push(msg),
11651168
);
11661169

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 * as webpack from 'webpack';
9+
import { isWebpackFiveOrHigher } from './webpack-version';
10+
11+
const WebpackError = require('webpack/lib/WebpackError');
12+
13+
export function addWarning(compilation: webpack.compilation.Compilation, message: string): void {
14+
if (isWebpackFiveOrHigher()) {
15+
compilation.warnings.push(new WebpackError(message));
16+
} else {
17+
// Allows building with either Webpack 4 or 5+ types
18+
// tslint:disable-next-line: no-any
19+
compilation.warnings.push(message as any);
20+
}
21+
}
22+
23+
export function addError(compilation: webpack.compilation.Compilation, message: string): void {
24+
if (isWebpackFiveOrHigher()) {
25+
compilation.errors.push(new WebpackError(message));
26+
} else {
27+
// Allows building with either Webpack 4 or 5+ types
28+
// tslint:disable-next-line: no-any
29+
compilation.errors.push(new Error(message) as any);
30+
}
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 * as webpack from 'webpack';
9+
10+
let cachedIsWebpackFiveOrHigher: boolean | undefined;
11+
export function isWebpackFiveOrHigher(): boolean {
12+
if (cachedIsWebpackFiveOrHigher === undefined) {
13+
cachedIsWebpackFiveOrHigher = false;
14+
if (typeof webpack.version === 'string') {
15+
const versionParts = webpack.version.split('.');
16+
if (versionParts[0] && Number(versionParts[0]) >= 5) {
17+
cachedIsWebpackFiveOrHigher = true;
18+
}
19+
}
20+
}
21+
22+
return cachedIsWebpackFiveOrHigher;
23+
}

0 commit comments

Comments
 (0)