Skip to content

Commit 2c7581c

Browse files
committed
refactor(@angular/build): allow Bazel to inject a custom esbuild plugin
Introduces a mechanism to dynamically load a custom esbuild plugin when the application builder is executed within a Bazel environment. This is enabled by a new `bazelEsbuildPluginPath` option, which is derived from the `NG_INTERNAL_ESBUILD_PLUGINS_DO_NOT_USE` environment variable. The path is only resolved if the `BAZEL_BINDIR` and `JS_BINARY__EXECROOT` environment variables are also present, ensuring the logic is only active during a Bazel build. The builder dynamically imports the plugin from the specified path and adds it to the esbuild pipeline, allowing for build-system-specific customizations. (cherry picked from commit ec739d7)
1 parent 15b4495 commit 2c7581c

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

packages/angular/build/src/builders/application/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { BuildOutputFileType } from '../../tools/esbuild/bundler-context';
1414
import { createJsonBuildManifest, emitFilesToDisk } from '../../tools/esbuild/utils';
1515
import { colors as ansiColors } from '../../utils/color';
1616
import { deleteOutputDir } from '../../utils/delete-output-dir';
17-
import { useJSONBuildLogs } from '../../utils/environment-options';
17+
import { bazelEsbuildPluginPath, useJSONBuildLogs } from '../../utils/environment-options';
1818
import { purgeStaleBuildCache } from '../../utils/purge-cache';
1919
import { assertCompatibleAngularVersion } from '../../utils/version';
2020
import { runEsBuildBuildAction } from './build-action';
@@ -56,6 +56,14 @@ export async function* buildApplicationInternal(
5656
return;
5757
}
5858

59+
if (bazelEsbuildPluginPath) {
60+
extensions ??= {};
61+
extensions.codePlugins ??= [];
62+
63+
const { default: bazelEsbuildPlugin } = await import(bazelEsbuildPluginPath);
64+
extensions.codePlugins.push(bazelEsbuildPlugin);
65+
}
66+
5967
const normalizedOptions = await normalizeOptions(context, projectName, options, extensions);
6068

6169
if (!normalizedOptions.outputOptions.ignoreServer) {

packages/angular/build/src/utils/environment-options.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,11 @@ export const useComponentTemplateHmr =
112112
const partialSsrBuildVariable = process.env['NG_BUILD_PARTIAL_SSR'];
113113
export const usePartialSsrBuild =
114114
isPresent(partialSsrBuildVariable) && isEnabled(partialSsrBuildVariable);
115+
116+
const bazelBinDirectory = process.env['BAZEL_BINDIR'];
117+
const bazelExecRoot = process.env['JS_BINARY__EXECROOT'];
118+
119+
export const bazelEsbuildPluginPath =
120+
bazelBinDirectory && bazelExecRoot
121+
? process.env['NG_INTERNAL_ESBUILD_PLUGINS_DO_NOT_USE']
122+
: undefined;

0 commit comments

Comments
 (0)