From f143171fd030fa1cc8df84ed5f0b96f5ad0f9e10 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Mon, 7 Nov 2022 17:42:53 +0000 Subject: [PATCH] fix(@angular-devkit/build-angular): only add `@angular/platform-server/init` when package is installed. This commit fixes an issue where `@angular/platform-server/init` was added as an entry-point during the server build even when this was not installed. Closes #24188 (cherry picked from commit d754b72d4e7c9b5cb5c466af730b353d41fc4b83) --- .../src/builders/server/index.ts | 33 +++++++++---------- .../src/webpack/configs/common.ts | 3 +- .../src/webpack/utils/helpers.ts | 15 ++++++++- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/packages/angular_devkit/build_angular/src/builders/server/index.ts b/packages/angular_devkit/build_angular/src/builders/server/index.ts index 62737cbbb14c..dd38c82af7d2 100644 --- a/packages/angular_devkit/build_angular/src/builders/server/index.ts +++ b/packages/angular_devkit/build_angular/src/builders/server/index.ts @@ -24,6 +24,7 @@ import { generateI18nBrowserWebpackConfigFromContext, } from '../../utils/webpack-browser-config'; import { getCommonConfig, getStylesConfig } from '../../webpack/configs'; +import { isPlatformServerInstalled } from '../../webpack/utils/helpers'; import { webpackStatsLogger } from '../../webpack/utils/stats'; import { Schema as ServerBuilderOptions } from './schema'; @@ -175,23 +176,21 @@ async function initialize( function getPlatformServerExportsConfig(wco: BrowserWebpackConfigOptions): Partial { // Add `@angular/platform-server` exports. // This is needed so that DI tokens can be referenced and set at runtime outside of the bundle. - try { - // Only add `@angular/platform-server` exports when it is installed. - // In some cases this builder is used when `@angular/platform-server` is not installed. - // Example: when using `@nguniversal/common/clover` which does not need `@angular/platform-server`. - require.resolve('@angular/platform-server', { paths: [wco.root] }); - } catch { - return {}; - } - return { - module: { - rules: [ - { - loader: require.resolve('./platform-server-exports-loader'), - include: [path.resolve(wco.root, wco.buildOptions.main)], + // Only add `@angular/platform-server` exports when it is installed. + // In some cases this builder is used when `@angular/platform-server` is not installed. + // Example: when using `@nguniversal/common/clover` which does not need `@angular/platform-server`. + + return isPlatformServerInstalled(wco.root) + ? { + module: { + rules: [ + { + loader: require.resolve('./platform-server-exports-loader'), + include: [path.resolve(wco.root, wco.buildOptions.main)], + }, + ], }, - ], - }, - }; + } + : {}; } diff --git a/packages/angular_devkit/build_angular/src/webpack/configs/common.ts b/packages/angular_devkit/build_angular/src/webpack/configs/common.ts index 8e98c191e91b..84fba1774ead 100644 --- a/packages/angular_devkit/build_angular/src/webpack/configs/common.ts +++ b/packages/angular_devkit/build_angular/src/webpack/configs/common.ts @@ -42,6 +42,7 @@ import { getOutputHashFormat, getStatsOptions, globalScriptsByBundleName, + isPlatformServerInstalled, } from '../utils/helpers'; const VENDORS_TEST = /[\\/]node_modules[\\/]/; @@ -118,7 +119,7 @@ export async function getCommonConfig(wco: WebpackConfigOptions): Promise