Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions libs/native-federation/src/utils/angular-esbuild-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { RebuildEvents, RebuildHubs } from './rebuild-events';

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

export type MemResultHandler = (
outfiles: esbuild.OutputFile[],
Expand Down Expand Up @@ -270,6 +271,11 @@ async function runEsbuild(

pluginOptions.styleOptions.externalDependencies = [];

const [compilerPlugin, pluginDisposed] = createAwaitableCompilerPlugin(
pluginOptions.pluginOptions,
pluginOptions.styleOptions,
);

const config: esbuild.BuildOptions = {
entryPoints: entryPoints.map((ep) => ({
in: ep.fileName,
Expand All @@ -294,10 +300,7 @@ async function runEsbuild(
target: target,
logLimit: kind === 'shared-package' ? 1 : 0,
plugins: (plugins as any) || [
createCompilerPlugin(
pluginOptions.pluginOptions,
pluginOptions.styleOptions,
),
compilerPlugin,
...(mappedPaths && mappedPaths.length > 0
? [createSharedMappingsPlugin(mappedPaths)]
: []),
Expand All @@ -317,6 +320,7 @@ async function runEsbuild(
const abortHandler = async () => {
await ctx.cancel();
await ctx.dispose();
await pluginDisposed;
};

if (signal) {
Expand All @@ -342,6 +346,7 @@ async function runEsbuild(
} else {
if (signal) signal.removeEventListener('abort', abortHandler);
await ctx.dispose();
await pluginDisposed;
}
return writtenFiles;
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as esbuild from 'esbuild';
import { createCompilerPlugin } from '@angular/build/private';

type CreateCompilerPluginParams = Parameters<typeof createCompilerPlugin>;

export function createAwaitableCompilerPlugin(
pluginOptions: CreateCompilerPluginParams[0],
styleOptions: CreateCompilerPluginParams[1],
): [esbuild.Plugin, Promise<void>] {
const originalPlugin = createCompilerPlugin(pluginOptions, styleOptions);

let resolveDispose: () => void;
const onDisposePromise = new Promise<void>((resolve) => {
resolveDispose = resolve;
});

const wrappedPlugin: esbuild.Plugin = {
...originalPlugin,
setup(build: esbuild.PluginBuild) {
// Wrap the build object to intercept onDispose
const wrappedBuild = new Proxy(build, {
get(target, prop) {
if (prop === 'onDispose') {
return (callback: () => void | Promise<void>) => {
return target.onDispose(() => {
callback();
resolveDispose();
});
};
}
return target[prop as keyof esbuild.PluginBuild];
},
});

return originalPlugin.setup(wrappedBuild);
},
};

return [wrappedPlugin, onDisposePromise];
}
4 changes: 2 additions & 2 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"experimentalDecorators": true,
"esModuleInterop": true,
"importHelpers": true,
"target": "es2015",
"target": "es2022",
"module": "esnext",
"lib": ["es2017", "dom"],
"lib": ["es2022", "dom"],
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"baseUrl": ".",
Expand Down