diff --git a/packages/angular/build/src/private.ts b/packages/angular/build/src/private.ts index 175dd39530ae..38cfc3a07181 100644 --- a/packages/angular/build/src/private.ts +++ b/packages/angular/build/src/private.ts @@ -59,6 +59,7 @@ export function createCompilerPlugin( ), ); } +export { getSharedCompilationStateCleanup } from './tools/esbuild/angular/compilation-state'; export type { AngularCompilation } from './tools/angular/compilation'; export { DiagnosticModes } from './tools/angular/compilation'; diff --git a/packages/angular/build/src/tools/esbuild/angular/compilation-state.ts b/packages/angular/build/src/tools/esbuild/angular/compilation-state.ts index 79b46313f1ec..a9e190e15a7e 100644 --- a/packages/angular/build/src/tools/esbuild/angular/compilation-state.ts +++ b/packages/angular/build/src/tools/esbuild/angular/compilation-state.ts @@ -10,6 +10,17 @@ export class SharedTSCompilationState { #pendingCompilation = true; #resolveCompilationReady: ((value: boolean) => void) | undefined; #compilationReadyPromise: Promise | undefined; + + /** + * ESbuild doesnt allow for awaiting the cleanup of plugins, therefore this + * allows consumers of the compiler-plugin to await the disposal of the plugin + * after ESbuild dispose function was called. + */ + #disposeCompilation: (() => void) | undefined; + awaitCompilationDisposed: Promise = new Promise((resolve) => { + this.#disposeCompilation = resolve; + }); + #hasErrors = true; get waitUntilReady(): Promise { @@ -37,6 +48,7 @@ export class SharedTSCompilationState { dispose(): void { this.markAsReady(true); + this.#disposeCompilation?.(); globalSharedCompilationState = undefined; } } @@ -46,3 +58,7 @@ let globalSharedCompilationState: SharedTSCompilationState | undefined; export function getSharedCompilationState(): SharedTSCompilationState { return (globalSharedCompilationState ??= new SharedTSCompilationState()); } + +export function getSharedCompilationStateCleanup() { + return globalSharedCompilationState?.awaitCompilationDisposed ?? Promise.resolve(); +} diff --git a/packages/angular/build/src/tools/esbuild/angular/compiler-plugin.ts b/packages/angular/build/src/tools/esbuild/angular/compiler-plugin.ts index 95e6694b728b..a8af37814b79 100644 --- a/packages/angular/build/src/tools/esbuild/angular/compiler-plugin.ts +++ b/packages/angular/build/src/tools/esbuild/angular/compiler-plugin.ts @@ -588,11 +588,14 @@ export function createCompilerPlugin( logCumulativeDurations(); }); - build.onDispose(() => { - sharedTSCompilationState?.dispose(); - void compilation.close?.(); - void cacheStore?.close(); - }); + build.onDispose( + () => + void Promise.all( + [compilation?.close?.(), cacheStore?.close(), javascriptTransformer.close()].filter( + Boolean, + ), + ).then(() => sharedTSCompilationState?.dispose()), + ); /** * Checks if the file has side-effects when `advancedOptimizations` is enabled.