From 8639a3b6d981ddef84c0f4d70b6a941fc866b82b 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 --- 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 e3c97805bd9a..8521b9af6674 100644 --- a/packages/angular/build/src/builders/dev-server/vite-server.ts +++ b/packages/angular/build/src/builders/dev-server/vite-server.ts @@ -422,6 +422,7 @@ export async function* serveWithVite( componentStyles, templateUpdates, browserOptions.loader as EsbuildLoaderOption | undefined, + browserOptions.define, extensions?.middleware, transformers?.indexHtml, thirdPartySourcemaps, @@ -653,6 +654,7 @@ export async function setupServer( componentStyles: Map, templateUpdates: Map, prebundleLoaderExtensions: EsbuildLoaderOption | undefined, + define: ApplicationBuilderInternalOptions['define'], extensionMiddleware?: Connect.NextHandleFunction[], indexHtmlTransformer?: (content: string) => Promise, thirdPartySourcemaps = false, @@ -765,6 +767,7 @@ export async function setupServer( target, loader: prebundleLoaderExtensions, thirdPartySourcemaps, + define, }), }, plugins: [ @@ -802,6 +805,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'],