From e20e739eabaec8a54c08c370688ff15f7a7ea3ef Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Tue, 10 Dec 2024 14:41:18 +0000 Subject: [PATCH] refactor(@angular/build): add `globalThis['ngServerMode']` only when `externalDependencies` are present This code is unnecessary when no external dependencies are involved. (cherry picked from commit 43127ddfb6fa35582b6b2c2e6700fd9eca6c4e7b) --- .../src/tools/esbuild/application-code-bundle.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/angular/build/src/tools/esbuild/application-code-bundle.ts b/packages/angular/build/src/tools/esbuild/application-code-bundle.ts index c776d30ca629..db2581eb9b00 100644 --- a/packages/angular/build/src/tools/esbuild/application-code-bundle.ts +++ b/packages/angular/build/src/tools/esbuild/application-code-bundle.ts @@ -200,7 +200,11 @@ export function createServerPolyfillBundleOptions( return; } - const jsBanner: string[] = [`globalThis['ngServerMode'] = true;`]; + const jsBanner: string[] = []; + if (polyfillBundleOptions.external?.length) { + jsBanner.push(`globalThis['ngServerMode'] = true;`); + } + if (isNodePlatform) { // Note: Needed as esbuild does not provide require shims / proxy from ESModules. // See: https://github.com/evanw/esbuild/issues/1921. @@ -394,7 +398,11 @@ export function createSsrEntryCodeBundleOptions( const ssrInjectManifestNamespace = 'angular:ssr-entry-inject-manifest'; const isNodePlatform = options.ssrOptions?.platform !== ExperimentalPlatform.Neutral; - const jsBanner: string[] = [`globalThis['ngServerMode'] = true;`]; + const jsBanner: string[] = []; + if (options.externalDependencies?.length) { + jsBanner.push(`globalThis['ngServerMode'] = true;`); + } + if (isNodePlatform) { // Note: Needed as esbuild does not provide require shims / proxy from ESModules. // See: https://github.com/evanw/esbuild/issues/1921.