Skip to content

Commit d754b72

Browse files
committed
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
1 parent b4e4512 commit d754b72

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

packages/angular_devkit/build_angular/src/builders/server/index.ts

+16-17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
generateI18nBrowserWebpackConfigFromContext,
2525
} from '../../utils/webpack-browser-config';
2626
import { getCommonConfig, getStylesConfig } from '../../webpack/configs';
27+
import { isPlatformServerInstalled } from '../../webpack/utils/helpers';
2728
import { webpackStatsLogger } from '../../webpack/utils/stats';
2829
import { Schema as ServerBuilderOptions } from './schema';
2930

@@ -175,23 +176,21 @@ async function initialize(
175176
function getPlatformServerExportsConfig(wco: BrowserWebpackConfigOptions): Partial<Configuration> {
176177
// Add `@angular/platform-server` exports.
177178
// This is needed so that DI tokens can be referenced and set at runtime outside of the bundle.
178-
try {
179-
// Only add `@angular/platform-server` exports when it is installed.
180-
// In some cases this builder is used when `@angular/platform-server` is not installed.
181-
// Example: when using `@nguniversal/common/clover` which does not need `@angular/platform-server`.
182-
require.resolve('@angular/platform-server', { paths: [wco.root] });
183-
} catch {
184-
return {};
185-
}
186179

187-
return {
188-
module: {
189-
rules: [
190-
{
191-
loader: require.resolve('./platform-server-exports-loader'),
192-
include: [path.resolve(wco.root, wco.buildOptions.main)],
180+
// Only add `@angular/platform-server` exports when it is installed.
181+
// In some cases this builder is used when `@angular/platform-server` is not installed.
182+
// Example: when using `@nguniversal/common/clover` which does not need `@angular/platform-server`.
183+
184+
return isPlatformServerInstalled(wco.root)
185+
? {
186+
module: {
187+
rules: [
188+
{
189+
loader: require.resolve('./platform-server-exports-loader'),
190+
include: [path.resolve(wco.root, wco.buildOptions.main)],
191+
},
192+
],
193193
},
194-
],
195-
},
196-
};
194+
}
195+
: {};
197196
}

packages/angular_devkit/build_angular/src/webpack/configs/common.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
getOutputHashFormat,
4343
getStatsOptions,
4444
globalScriptsByBundleName,
45+
isPlatformServerInstalled,
4546
} from '../utils/helpers';
4647

4748
const VENDORS_TEST = /[\\/]node_modules[\\/]/;
@@ -118,7 +119,7 @@ export async function getCommonConfig(wco: WebpackConfigOptions): Promise<Config
118119
// Fixes Critical dependency: the request of a dependency is an expression
119120
extraPlugins.push(new ContextReplacementPlugin(/@?hapi|express[\\/]/));
120121

121-
if (Array.isArray(entryPoints['main'])) {
122+
if (isPlatformServerInstalled(wco.root) && Array.isArray(entryPoints['main'])) {
122123
// This import must come before any imports (direct or transitive) that rely on DOM built-ins being
123124
// available, such as `@angular/elements`.
124125
entryPoints['main'].unshift('@angular/platform-server/init');

packages/angular_devkit/build_angular/src/webpack/utils/helpers.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import type { ObjectPattern } from 'copy-webpack-plugin';
1010
import { createHash } from 'crypto';
11-
import { existsSync } from 'fs';
1211
import glob from 'glob';
1312
import * as path from 'path';
1413
import type { Configuration, WebpackOptionsNormalized } from 'webpack';
@@ -306,3 +305,17 @@ export function getStatsOptions(verbose = false): WebpackStatsOptions {
306305
? { ...webpackOutputOptions, ...verboseWebpackOutputOptions }
307306
: webpackOutputOptions;
308307
}
308+
309+
/**
310+
* @param root the workspace root
311+
* @returns `true` when `@angular/platform-server` is installed.
312+
*/
313+
export function isPlatformServerInstalled(root: string): boolean {
314+
try {
315+
require.resolve('@angular/platform-server', { paths: [root] });
316+
317+
return true;
318+
} catch {
319+
return false;
320+
}
321+
}

0 commit comments

Comments
 (0)