From 63a5559f0d8aa772361db350fb41d74d21ba679a Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Thu, 22 Oct 2020 11:29:14 -0400 Subject: [PATCH] fix(@angular-devkit/build-webpack): fully close Webpack 5 compiler The Webpack 5 compiler now contains a close function that should be called when the compiler is finished. (cherry picked from commit dc3cdae7b9038c7bd2e477aeda04d9a96368ef4f) --- .../build_webpack/src/webpack/index.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/angular_devkit/build_webpack/src/webpack/index.ts b/packages/angular_devkit/build_webpack/src/webpack/index.ts index 6fc1f3751e4a..f4624e0a8605 100644 --- a/packages/angular_devkit/build_webpack/src/webpack/index.ts +++ b/packages/angular_devkit/build_webpack/src/webpack/index.ts @@ -52,6 +52,12 @@ export function runWebpack( return createWebpack({ ...config, watch: false }).pipe( switchMap(webpackCompiler => new Observable(obs => { + // Webpack 5 has a compiler level close function + // The close function will crash if caching is disabled + const compilerClose = webpackCompiler.options.cache !== false + ? (webpackCompiler as { close?(callback: () => void): void }).close + : undefined; + const callback = (err?: Error, stats?: webpack.Stats) => { if (err) { return obs.error(err); @@ -71,7 +77,11 @@ export function runWebpack( } as unknown as BuildResult); if (!config.watch) { - obs.complete(); + if (compilerClose) { + compilerClose(() => obs.complete()); + } else { + obs.complete(); + } } }; @@ -81,7 +91,10 @@ export function runWebpack( const watching = webpackCompiler.watch(watchOptions, callback); // Teardown logic. Close the watcher when unsubscribed from. - return () => watching.close(() => { }); + return () => { + watching.close(() => { }); + compilerClose?.(() => { }); + }; } else { webpackCompiler.run(callback); }