Skip to content

Commit c534c8a

Browse files
committed
fix(native-federation): Added promise to wait for dispose function
1 parent 82ec5e0 commit c534c8a

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

libs/native-federation/src/utils/angular-esbuild-adapter.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { RebuildEvents, RebuildHubs } from './rebuild-events';
4242

4343
import JSON5 from 'json5';
4444
import { isDeepStrictEqual } from 'node:util';
45+
import { createAwaitableCompilerPlugin } from './create-awaitable-compiler-plugin';
4546

4647
export type MemResultHandler = (
4748
outfiles: esbuild.OutputFile[],
@@ -270,6 +271,11 @@ async function runEsbuild(
270271

271272
pluginOptions.styleOptions.externalDependencies = [];
272273

274+
const [compilerPlugin, pluginDisposed] = createAwaitableCompilerPlugin(
275+
pluginOptions.pluginOptions,
276+
pluginOptions.styleOptions,
277+
);
278+
273279
const config: esbuild.BuildOptions = {
274280
entryPoints: entryPoints.map((ep) => ({
275281
in: ep.fileName,
@@ -294,10 +300,7 @@ async function runEsbuild(
294300
target: target,
295301
logLimit: kind === 'shared-package' ? 1 : 0,
296302
plugins: (plugins as any) || [
297-
createCompilerPlugin(
298-
pluginOptions.pluginOptions,
299-
pluginOptions.styleOptions,
300-
),
303+
compilerPlugin,
301304
...(mappedPaths && mappedPaths.length > 0
302305
? [createSharedMappingsPlugin(mappedPaths)]
303306
: []),
@@ -317,6 +320,7 @@ async function runEsbuild(
317320
const abortHandler = async () => {
318321
await ctx.cancel();
319322
await ctx.dispose();
323+
await pluginDisposed;
320324
};
321325

322326
if (signal) {
@@ -342,6 +346,7 @@ async function runEsbuild(
342346
} else {
343347
if (signal) signal.removeEventListener('abort', abortHandler);
344348
await ctx.dispose();
349+
await pluginDisposed;
345350
}
346351
return writtenFiles;
347352
} catch (error) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as esbuild from 'esbuild';
2+
import { createCompilerPlugin } from '@angular/build/private';
3+
4+
export function createAwaitableCompilerPlugin(
5+
pluginOptions: any,
6+
styleOptions: any,
7+
): [esbuild.Plugin, Promise<void>] {
8+
const originalPlugin = createCompilerPlugin(pluginOptions, styleOptions);
9+
10+
let resolveDispose: () => void;
11+
const onDisposePromise = new Promise<void>((resolve) => {
12+
resolveDispose = resolve;
13+
});
14+
15+
const wrappedPlugin: esbuild.Plugin = {
16+
...originalPlugin,
17+
setup(build: esbuild.PluginBuild) {
18+
let onDisposeCallback: (() => void | Promise<void>) | undefined;
19+
20+
// Wrap the build object to intercept onDispose
21+
const wrappedBuild = new Proxy(build, {
22+
get(target, prop) {
23+
if (prop === 'onDispose') {
24+
return (callback: () => void | Promise<void>) => {
25+
onDisposeCallback = callback;
26+
return target.onDispose(async () => {
27+
await callback();
28+
resolveDispose();
29+
});
30+
};
31+
}
32+
return target[prop as keyof esbuild.PluginBuild];
33+
},
34+
});
35+
36+
// Call original setup with wrapped build
37+
return originalPlugin.setup(wrappedBuild);
38+
},
39+
};
40+
41+
return [wrappedPlugin, onDisposePromise];
42+
}

0 commit comments

Comments
 (0)