Skip to content

Commit f60070e

Browse files
Merge pull request #1017 from Aukevanoost/issues/970
fix(native-federation): Windows heap crash
2 parents 82ec5e0 + 0b773bb commit f60070e

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as esbuild from 'esbuild';
2+
import { createCompilerPlugin } from '@angular/build/private';
3+
4+
type CreateCompilerPluginParams = Parameters<typeof createCompilerPlugin>;
5+
6+
export function createAwaitableCompilerPlugin(
7+
pluginOptions: CreateCompilerPluginParams[0],
8+
styleOptions: CreateCompilerPluginParams[1],
9+
): [esbuild.Plugin, Promise<void>] {
10+
const originalPlugin = createCompilerPlugin(pluginOptions, styleOptions);
11+
12+
let resolveDispose: () => void;
13+
const onDisposePromise = new Promise<void>((resolve) => {
14+
resolveDispose = resolve;
15+
});
16+
17+
const wrappedPlugin: esbuild.Plugin = {
18+
...originalPlugin,
19+
setup(build: esbuild.PluginBuild) {
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+
return target.onDispose(() => {
26+
callback();
27+
resolveDispose();
28+
});
29+
};
30+
}
31+
return target[prop as keyof esbuild.PluginBuild];
32+
},
33+
});
34+
35+
return originalPlugin.setup(wrappedBuild);
36+
},
37+
};
38+
39+
return [wrappedPlugin, onDisposePromise];
40+
}

tsconfig.base.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"experimentalDecorators": true,
1010
"esModuleInterop": true,
1111
"importHelpers": true,
12-
"target": "es2015",
12+
"target": "es2022",
1313
"module": "esnext",
14-
"lib": ["es2017", "dom"],
14+
"lib": ["es2022", "dom"],
1515
"skipLibCheck": true,
1616
"skipDefaultLibCheck": true,
1717
"baseUrl": ".",

0 commit comments

Comments
 (0)