Skip to content

Commit 341005b

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 a563c57 commit 341005b

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

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

+12-20
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

@@ -173,25 +174,16 @@ async function initialize(
173174
* This is needed so that DI tokens can be referenced and set at runtime outside of the bundle.
174175
*/
175176
function getPlatformServerExportsConfig(wco: BrowserWebpackConfigOptions): Partial<Configuration> {
176-
// Add `@angular/platform-server` exports.
177-
// 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-
}
186-
187-
return {
188-
module: {
189-
rules: [
190-
{
191-
loader: require.resolve('./platform-server-exports-loader'),
192-
include: [path.resolve(wco.root, wco.buildOptions.main)],
177+
return isPlatformServerInstalled(wco.root)
178+
? {
179+
module: {
180+
rules: [
181+
{
182+
loader: require.resolve('./platform-server-exports-loader'),
183+
include: [path.resolve(wco.root, wco.buildOptions.main)],
184+
},
185+
],
193186
},
194-
],
195-
},
196-
};
187+
}
188+
: {};
197189
}

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

+20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { existsSync } from 'fs';
1212
import glob from 'glob';
1313
import * as path from 'path';
1414
import type { Configuration, WebpackOptionsNormalized } from 'webpack';
15+
import { boolean } from 'yargs';
1516
import {
1617
AssetPatternClass,
1718
OutputHashing,
@@ -306,3 +307,22 @@ export function getStatsOptions(verbose = false): WebpackStatsOptions {
306307
? { ...webpackOutputOptions, ...verboseWebpackOutputOptions }
307308
: webpackOutputOptions;
308309
}
310+
311+
/**
312+
* @param root the workspace root
313+
* @returns `true` when `@angular/platform-server` is installed.
314+
*/
315+
export function isPlatformServerInstalled(root: string): boolean {
316+
// Add `@angular/platform-server` exports.
317+
// This is needed so that DI tokens can be referenced and set at runtime outside of the bundle.
318+
try {
319+
// Only add `@angular/platform-server` exports when it is installed.
320+
// In some cases this builder is used when `@angular/platform-server` is not installed.
321+
// Example: when using `@nguniversal/common/clover` which does not need `@angular/platform-server`.
322+
require.resolve('@angular/platform-server', { paths: [root] });
323+
324+
return true;
325+
} catch {
326+
return false;
327+
}
328+
}

0 commit comments

Comments
 (0)