From 1f2481a4f5155368aa571fc6679e3ef8af0ce56f Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Wed, 8 Jan 2025 12:01:53 +0000 Subject: [PATCH] fix(@angular/build): pass `define` option defined in application builder to Vite prebundling This update ensures that the `define` option is correctly passed to Vite during the prebundling phase of the application builder, improving compatibility and optimization of the build process. Closes #29278 (cherry picked from commit 8639a3b6d981ddef84c0f4d70b6a941fc866b82b) --- packages/angular/build/src/builders/dev-server/vite-server.ts | 4 ++++ packages/angular/build/src/tools/vite/utils.ts | 3 +++ 2 files changed, 7 insertions(+) diff --git a/packages/angular/build/src/builders/dev-server/vite-server.ts b/packages/angular/build/src/builders/dev-server/vite-server.ts index 9dfdfc5da39b..1c4a788103b3 100644 --- a/packages/angular/build/src/builders/dev-server/vite-server.ts +++ b/packages/angular/build/src/builders/dev-server/vite-server.ts @@ -390,6 +390,7 @@ export async function* serveWithVite( componentStyles, templateUpdates, browserOptions.loader as EsbuildLoaderOption | undefined, + browserOptions.define, extensions?.middleware, transformers?.indexHtml, thirdPartySourcemaps, @@ -635,6 +636,7 @@ export async function setupServer( componentStyles: Map, templateUpdates: Map, prebundleLoaderExtensions: EsbuildLoaderOption | undefined, + define: ApplicationBuilderInternalOptions['define'], extensionMiddleware?: Connect.NextHandleFunction[], indexHtmlTransformer?: (content: string) => Promise, thirdPartySourcemaps = false, @@ -741,6 +743,7 @@ export async function setupServer( target, loader: prebundleLoaderExtensions, thirdPartySourcemaps, + define, }), }, plugins: [ @@ -778,6 +781,7 @@ export async function setupServer( zoneless, loader: prebundleLoaderExtensions, thirdPartySourcemaps, + define, }), }; diff --git a/packages/angular/build/src/tools/vite/utils.ts b/packages/angular/build/src/tools/vite/utils.ts index 83085d910f60..fc0d82c2ce62 100644 --- a/packages/angular/build/src/tools/vite/utils.ts +++ b/packages/angular/build/src/tools/vite/utils.ts @@ -57,6 +57,7 @@ export function getDepOptimizationConfig({ ssr, loader, thirdPartySourcemaps, + define = {}, }: { disabled: boolean; exclude: string[]; @@ -67,6 +68,7 @@ export function getDepOptimizationConfig({ zoneless: boolean; loader?: EsbuildLoaderOption; thirdPartySourcemaps: boolean; + define: Record | undefined; }): DepOptimizationConfig { const plugins: ViteEsBuildPlugin[] = [ { @@ -99,6 +101,7 @@ export function getDepOptimizationConfig({ plugins, loader, define: { + ...define, 'ngServerMode': `${ssr}`, }, resolveExtensions: ['.mjs', '.js', '.cjs'],