Skip to content

fix(@angular-devkit/build-angular): only add @angular/platform-server/init when package is installed. #24191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions packages/angular_devkit/build_angular/src/builders/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -175,23 +176,21 @@ async function initialize(
function getPlatformServerExportsConfig(wco: BrowserWebpackConfigOptions): Partial<Configuration> {
// 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)],
},
],
},
],
},
};
}
: {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
getOutputHashFormat,
getStatsOptions,
globalScriptsByBundleName,
isPlatformServerInstalled,
} from '../utils/helpers';

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

if (Array.isArray(entryPoints['main'])) {
if (isPlatformServerInstalled(wco.root) && Array.isArray(entryPoints['main'])) {
// This import must come before any imports (direct or transitive) that rely on DOM built-ins being
// available, such as `@angular/elements`.
entryPoints['main'].unshift('@angular/platform-server/init');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import type { ObjectPattern } from 'copy-webpack-plugin';
import { createHash } from 'crypto';
import { existsSync } from 'fs';
import glob from 'glob';
import * as path from 'path';
import type { Configuration, WebpackOptionsNormalized } from 'webpack';
Expand Down Expand Up @@ -306,3 +305,17 @@ export function getStatsOptions(verbose = false): WebpackStatsOptions {
? { ...webpackOutputOptions, ...verboseWebpackOutputOptions }
: webpackOutputOptions;
}

/**
* @param root the workspace root
* @returns `true` when `@angular/platform-server` is installed.
*/
export function isPlatformServerInstalled(root: string): boolean {
try {
require.resolve('@angular/platform-server', { paths: [root] });

return true;
} catch {
return false;
}
}