diff --git a/packages/@ngtools/webpack/src/loader.ts b/packages/@ngtools/webpack/src/loader.ts index 4be0c7e04905..143eade4c435 100644 --- a/packages/@ngtools/webpack/src/loader.ts +++ b/packages/@ngtools/webpack/src/loader.ts @@ -602,7 +602,14 @@ export function ngcLoader(this: LoaderContext & { _compilation: any }, source: s }); const result = refactor.transpile(compilerOptions); - cb(null, result.outputText, result.sourceMap); + + if (plugin.failedCompilation) { + // Return an empty string to prevent extra loader errors (missing imports etc). + // Plugin errors were already pushed to the compilation errors. + cb(null, ''); + } else { + cb(null, result.outputText, result.sourceMap); + } }) .catch(err => cb(err)); } else { diff --git a/packages/@ngtools/webpack/src/plugin.ts b/packages/@ngtools/webpack/src/plugin.ts index 81ae899bd757..5fdf11462c02 100644 --- a/packages/@ngtools/webpack/src/plugin.ts +++ b/packages/@ngtools/webpack/src/plugin.ts @@ -61,6 +61,7 @@ export class AotPlugin implements Tapable { private _donePromise: Promise | null; private _compiler: any = null; private _compilation: any = null; + private _failedCompilation = false; private _typeCheck = true; private _skipCodeGeneration = false; @@ -87,6 +88,7 @@ export class AotPlugin implements Tapable { get compilerHost() { return this._compilerHost; } get compilerOptions() { return this._compilerOptions; } get done() { return this._donePromise; } + get failedCompilation() { return this._failedCompilation; } get entryModule() { const splitted = this._entryModule.split('#'); const path = splitted[0]; @@ -374,11 +376,14 @@ export class AotPlugin implements Tapable { compiler.plugin('make', (compilation: any, cb: any) => this._make(compilation, cb)); compiler.plugin('after-emit', (compilation: any, cb: any) => { - this._donePromise = null; - this._compilation = null; compilation._ngToolsWebpackPluginInstance = null; cb(); }); + compiler.plugin('done', () => { + this._donePromise = null; + this._compilation = null; + this._failedCompilation = false; + }); compiler.plugin('after-resolvers', (compiler: any) => { // Virtual file system. @@ -578,10 +583,13 @@ export class AotPlugin implements Tapable { .then(() => { if (this._compilation.errors == 0) { this._compilerHost.resetChangedFileTracker(); + } else { + this._failedCompilation = true; } cb(); }, (err: any) => { + this._failedCompilation = true; compilation.errors.push(err.stack); cb(); });