Skip to content

Commit 67a1fd3

Browse files
committed
fix(@angular/build): added JavaScriptTransformer close to dispose method
This change allows for awaiting the disposed plugin and added the missing close method of the JavaScript transformer to the onDispose. When the plugin is run multiple times on different projects/bundles without proper closing it will result in a node heap corruption. This allows us to cleanup the build before we start a new one. Closes #31973
1 parent bf990dc commit 67a1fd3

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

packages/angular/build/src/private.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export function createCompilerPlugin(
5959
),
6060
);
6161
}
62+
export { getSharedCompilationStateCleanup } from './tools/esbuild/angular/compilation-state';
6263

6364
export type { AngularCompilation } from './tools/angular/compilation';
6465
export { DiagnosticModes } from './tools/angular/compilation';

packages/angular/build/src/tools/esbuild/angular/compilation-state.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ export class SharedTSCompilationState {
1010
#pendingCompilation = true;
1111
#resolveCompilationReady: ((value: boolean) => void) | undefined;
1212
#compilationReadyPromise: Promise<boolean> | undefined;
13+
14+
/**
15+
* ESbuild doesnt allow for awaiting the cleanup of plugins, therefore this
16+
* allows consumers of the compiler-plugin to await the disposal of the plugin
17+
* after ESbuild dispose function was called.
18+
*/
19+
#disposeCompilation: (() => void) | undefined;
20+
awaitCompilationDisposed: Promise<void> = new Promise((resolve) => {
21+
this.#disposeCompilation = resolve;
22+
});
23+
1324
#hasErrors = true;
1425

1526
get waitUntilReady(): Promise<boolean> {
@@ -37,6 +48,7 @@ export class SharedTSCompilationState {
3748

3849
dispose(): void {
3950
this.markAsReady(true);
51+
this.#disposeCompilation?.();
4052
globalSharedCompilationState = undefined;
4153
}
4254
}
@@ -46,3 +58,7 @@ let globalSharedCompilationState: SharedTSCompilationState | undefined;
4658
export function getSharedCompilationState(): SharedTSCompilationState {
4759
return (globalSharedCompilationState ??= new SharedTSCompilationState());
4860
}
61+
62+
export function getSharedCompilationStateCleanup() {
63+
return globalSharedCompilationState?.awaitCompilationDisposed ?? Promise.resolve();
64+
}

packages/angular/build/src/tools/esbuild/angular/compiler-plugin.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -588,11 +588,14 @@ export function createCompilerPlugin(
588588
logCumulativeDurations();
589589
});
590590

591-
build.onDispose(() => {
592-
sharedTSCompilationState?.dispose();
593-
void compilation.close?.();
594-
void cacheStore?.close();
595-
});
591+
build.onDispose(
592+
() =>
593+
void Promise.all(
594+
[compilation?.close?.(), cacheStore?.close(), javascriptTransformer.close()].filter(
595+
Boolean,
596+
),
597+
).then(() => sharedTSCompilationState?.dispose()),
598+
);
596599

597600
/**
598601
* Checks if the file has side-effects when `advancedOptimizations` is enabled.

0 commit comments

Comments
 (0)