Skip to content

Commit 09f9aa9

Browse files
authored
fix(webpack): fix some problems with errors not reported. (angular#3444)
When analyzing the program or generating code AoT, the plugin was sometimes missing errors. On build, nothing was outputted. Now it will show the error encountered before leaving the process. On serve, hte problem is in webpack-dev-server which does not give us a way to report the error. It seems like webpack-dev-server is not compatible with async plugins, which the @ngtools/webpack is. This is a problem with webpack-dev-server itself.
1 parent 83dfc96 commit 09f9aa9

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

packages/@ngtools/webpack/src/plugin.ts

+26-25
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,11 @@ export class AotPlugin implements Tapable {
196196
});
197197

198198
// Virtual file system.
199-
compiler.resolvers.normal.plugin('resolve', (request: any, cb?: () => void) => {
199+
compiler.resolvers.normal.plugin('resolve', (request: any, cb?: (err?: any) => void) => {
200200
if (request.request.match(/\.ts$/)) {
201-
this.done.then(() => cb());
201+
this.done
202+
.then(() => cb())
203+
.catch((err) => cb(err));
202204
} else {
203205
cb();
204206
}
@@ -212,9 +214,8 @@ export class AotPlugin implements Tapable {
212214

213215
private _make(compilation: any, cb: (err?: any, request?: any) => void) {
214216
this._compilation = compilation;
215-
216217
if (this._compilation._ngToolsWebpackPluginInstance) {
217-
cb(new Error('An @ngtools/webpack plugin already exist for this compilation.'));
218+
return cb(new Error('An @ngtools/webpack plugin already exist for this compilation.'));
218219
}
219220
this._compilation._ngToolsWebpackPluginInstance = this;
220221

@@ -227,28 +228,28 @@ export class AotPlugin implements Tapable {
227228
basePath: this.basePath
228229
};
229230

230-
let promise = Promise.resolve();
231-
if (!this._skipCodeGeneration) {
232-
// Create the Code Generator.
233-
const codeGenerator = ngCompiler.CodeGenerator.create(
234-
this._angularCompilerOptions,
235-
i18nOptions,
236-
this._program,
237-
this._compilerHost,
238-
new ngCompiler.NodeReflectorHostContext(this._compilerHost),
239-
this._resourceLoader
240-
);
241-
242-
// We need to temporarily patch the CodeGenerator until either it's patched or allows us
243-
// to pass in our own ReflectorHost.
244-
// TODO: remove this.
245-
patchReflectorHost(codeGenerator);
246-
promise = promise.then(() => codeGenerator.codegen({
247-
transitiveModules: true
248-
}));
249-
}
231+
this._donePromise = Promise.resolve()
232+
.then(() => {
233+
if (this._skipCodeGeneration) {
234+
return;
235+
}
250236

251-
this._donePromise = promise
237+
// Create the Code Generator.
238+
const codeGenerator = ngCompiler.CodeGenerator.create(
239+
this._angularCompilerOptions,
240+
i18nOptions,
241+
this._program,
242+
this._compilerHost,
243+
new ngCompiler.NodeReflectorHostContext(this._compilerHost),
244+
this._resourceLoader
245+
);
246+
247+
// We need to temporarily patch the CodeGenerator until either it's patched or allows us
248+
// to pass in our own ReflectorHost.
249+
// TODO: remove this.
250+
patchReflectorHost(codeGenerator);
251+
return codeGenerator.codegen({ transitiveModules: true });
252+
})
252253
.then(() => {
253254
// Create a new Program, based on the old one. This will trigger a resolution of all
254255
// transitive modules, which include files that might just have been generated.

packages/angular-cli/tasks/build-webpack.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { NgCliWebpackConfig } from '../models/webpack-config';
77
import { getWebpackStatsConfig } from '../models/';
88
import { CliConfig } from '../models/config';
99

10+
1011
// Configure build and output;
1112
let lastHash: any = null;
1213

@@ -36,7 +37,9 @@ export default <any>Task.extend({
3637

3738
return new Promise((resolve, reject) => {
3839
webpackCompiler.run((err: any, stats: any) => {
39-
if (err) { return reject(err); }
40+
if (err) {
41+
return reject(err);
42+
}
4043

4144
// Don't keep cache
4245
// TODO: Make conditional if using --watch
@@ -47,8 +50,18 @@ export default <any>Task.extend({
4750
process.stdout.write(stats.toString(statsConfig) + '\n');
4851
}
4952

50-
return stats.hasErrors() ? reject() : resolve();
53+
if (stats.hasErrors()) {
54+
reject();
55+
} else {
56+
resolve();
57+
}
5158
});
59+
})
60+
.catch((err: Error) => {
61+
if (err) {
62+
this.ui.writeError('\nAn error occured during the build:\n' + ((err && err.stack) || err));
63+
}
64+
throw err;
5265
});
5366
}
5467
});

packages/angular-cli/tasks/serve-webpack.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@ export default Task.extend({
115115

116116
const server = new WebpackDevServer(webpackCompiler, webpackDevServerConfiguration);
117117
return new Promise((resolve, reject) => {
118-
server.listen(serveTaskOptions.port,
119-
`${serveTaskOptions.host}`,
120-
function(err: any, stats: any) {
118+
server.listen(serveTaskOptions.port, `${serveTaskOptions.host}`, (err: any, stats: any) => {
121119
if (err) {
122120
console.error(err.stack || err);
123121
if (err.details) { console.error(err.details); }

0 commit comments

Comments
 (0)