Skip to content

Commit a598d16

Browse files
committed
fix(webpack): fix some problems with errors not reported.
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 61dd440 commit a598d16

File tree

3 files changed

+51
-38
lines changed

3 files changed

+51
-38
lines changed

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

+27-25
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export class AotPlugin implements Tapable {
7070
private _tsConfigPath: string;
7171

7272
private _donePromise: Promise<void>;
73+
private _err: Error;
7374
private _compiler: any = null;
7475
private _compilation: any = null;
7576

@@ -196,9 +197,11 @@ export class AotPlugin implements Tapable {
196197
});
197198

198199
// Virtual file system.
199-
compiler.resolvers.normal.plugin('resolve', (request: any, cb?: () => void) => {
200+
compiler.resolvers.normal.plugin('resolve', (request: any, cb?: (err?: any) => void) => {
200201
if (request.request.match(/\.ts$/)) {
201-
this.done.then(() => cb());
202+
this.done
203+
.then(() => cb())
204+
.catch((err) => cb(err));
202205
} else {
203206
cb();
204207
}
@@ -212,9 +215,8 @@ export class AotPlugin implements Tapable {
212215

213216
private _make(compilation: any, cb: (err?: any, request?: any) => void) {
214217
this._compilation = compilation;
215-
216218
if (this._compilation._ngToolsWebpackPluginInstance) {
217-
cb(new Error('An @ngtools/webpack plugin already exist for this compilation.'));
219+
return cb(new Error('An @ngtools/webpack plugin already exist for this compilation.'));
218220
}
219221
this._compilation._ngToolsWebpackPluginInstance = this;
220222

@@ -227,28 +229,28 @@ export class AotPlugin implements Tapable {
227229
basePath: this.basePath
228230
};
229231

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-
}
232+
this._donePromise = Promise.resolve()
233+
.then(() => {
234+
if (this._skipCodeGeneration) {
235+
return;
236+
}
250237

251-
this._donePromise = promise
238+
// Create the Code Generator.
239+
const codeGenerator = ngCompiler.CodeGenerator.create(
240+
this._angularCompilerOptions,
241+
i18nOptions,
242+
this._program,
243+
this._compilerHost,
244+
new ngCompiler.NodeReflectorHostContext(this._compilerHost),
245+
this._resourceLoader
246+
);
247+
248+
// We need to temporarily patch the CodeGenerator until either it's patched or allows us
249+
// to pass in our own ReflectorHost.
250+
// TODO: remove this.
251+
patchReflectorHost(codeGenerator);
252+
return codeGenerator.codegen({ transitiveModules: true });
253+
})
252254
.then(() => {
253255
// Create a new Program, based on the old one. This will trigger a resolution of all
254256
// 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
@@ -6,6 +6,7 @@ import { BuildOptions } from '../commands/build';
66
import { NgCliWebpackConfig } from '../models/webpack-config';
77
import { getWebpackStatsConfig } from '../models/';
88
import { CliConfig } from '../models/config';
9+
import { stripIndents } from 'common-tags';
910

1011
// Configure build and output;
1112
let lastHash: any = null;
@@ -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

+9-11
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,18 @@ 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: Error, stats: any) => {
121119
if (err) {
122-
console.error(err.stack || err);
123-
if (err.details) { console.error(err.details); }
124-
reject(err.details);
125-
} else {
126-
const { open, host, port } = serveTaskOptions;
127-
if (open) {
128-
opn(url.format({ protocol: 'http', hostname: host, port: port.toString() }));
129-
}
120+
return reject(err);
121+
}
122+
123+
const { open, host, port } = serveTaskOptions;
124+
if (open) {
125+
opn(url.format({ protocol: 'http', hostname: host, port: port.toString() }));
130126
}
131127
});
128+
}).catch((err) => {
129+
this.ui.writeError(err.stack);
132130
});
133131
}
134132
});

0 commit comments

Comments
 (0)