Skip to content

Commit

Permalink
fix(@angular-devkit/build-webpack): fully close Webpack 5 compiler
Browse files Browse the repository at this point in the history
The Webpack 5 compiler now contains a close function that should be called when the compiler is finished.

(cherry picked from commit dc3cdae)
  • Loading branch information
clydin committed Oct 22, 2020
1 parent db4c88c commit 63a5559
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions packages/angular_devkit/build_webpack/src/webpack/index.ts
Expand Up @@ -52,6 +52,12 @@ export function runWebpack(

return createWebpack({ ...config, watch: false }).pipe(
switchMap(webpackCompiler => new Observable<BuildResult>(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);
Expand All @@ -71,7 +77,11 @@ export function runWebpack(
} as unknown as BuildResult);

if (!config.watch) {
obs.complete();
if (compilerClose) {
compilerClose(() => obs.complete());
} else {
obs.complete();
}
}
};

Expand All @@ -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);
}
Expand Down

0 comments on commit 63a5559

Please sign in to comment.