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 34d3d8aab915..ff54f980b100 100644 --- a/packages/angular/build/src/tools/esbuild/application-code-bundle.ts +++ b/packages/angular/build/src/tools/esbuild/application-code-bundle.ts @@ -83,24 +83,7 @@ export function createBrowserCodeBundleOptions( buildOptions.plugins?.push(...options.plugins); } - if (options.externalPackages) { - // Package files affected by a customized loader should not be implicitly marked as external - if ( - options.loaderExtensions || - options.plugins || - typeof options.externalPackages === 'object' - ) { - // Plugin must be added after custom plugins to ensure any added loader options are considered - buildOptions.plugins?.push( - createExternalPackagesPlugin( - options.externalPackages !== true ? options.externalPackages : undefined, - ), - ); - } else { - // Safe to use the packages external option directly - buildOptions.packages = 'external'; - } - } + appendOptionsForExternalPackages(options, buildOptions); return buildOptions; }; @@ -302,9 +285,7 @@ export function createServerMainCodeBundleOptions( buildOptions.plugins ??= []; - if (externalPackages) { - buildOptions.packages = 'external'; - } else { + if (!externalPackages) { buildOptions.plugins.push(createRxjsEsmResolutionPlugin()); } @@ -381,6 +362,8 @@ export function createServerMainCodeBundleOptions( buildOptions.plugins.push(...options.plugins); } + appendOptionsForExternalPackages(options, buildOptions); + return buildOptions; }; } @@ -442,9 +425,7 @@ export function createSsrEntryCodeBundleOptions( buildOptions.plugins ??= []; - if (externalPackages) { - buildOptions.packages = 'external'; - } else { + if (!externalPackages) { buildOptions.plugins.push(createRxjsEsmResolutionPlugin()); } @@ -516,6 +497,8 @@ export function createSsrEntryCodeBundleOptions( buildOptions.plugins.push(...options.plugins); } + appendOptionsForExternalPackages(options, buildOptions); + return buildOptions; }; } @@ -721,3 +704,29 @@ function entryFileToWorkspaceRelative(workspaceRoot: string, entryFile: string): .replace(/\\/g, '/') ); } + +function appendOptionsForExternalPackages( + options: NormalizedApplicationBuildOptions, + buildOptions: BuildOptions, +): void { + if (!options.externalPackages) { + return; + } + + buildOptions.plugins ??= []; + + // Package files affected by a customized loader should not be implicitly marked as external + if (options.loaderExtensions || options.plugins || typeof options.externalPackages === 'object') { + // Plugin must be added after custom plugins to ensure any added loader options are considered + buildOptions.plugins.push( + createExternalPackagesPlugin( + options.externalPackages !== true ? options.externalPackages : undefined, + ), + ); + + buildOptions.packages = undefined; + } else { + // Safe to use the packages external option directly + buildOptions.packages = 'external'; + } +}