From b8fc0f87bad6fbb0798f37d95e2aaf0ee4af60ea Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Thu, 12 Jan 2023 10:10:12 +0000 Subject: [PATCH] feat(angular): install correct versions for setup-ssr (#14278) --- .../generators/setup-ssr/setup-ssr.spec.ts | 28 +++++++++++++++++++ .../src/generators/setup-ssr/setup-ssr.ts | 27 +++++++++++++++--- packages/angular/src/utils/versions.ts | 4 +-- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/packages/angular/src/generators/setup-ssr/setup-ssr.spec.ts b/packages/angular/src/generators/setup-ssr/setup-ssr.spec.ts index 7fd6a4355379d..18ced23b799f5 100644 --- a/packages/angular/src/generators/setup-ssr/setup-ssr.spec.ts +++ b/packages/angular/src/generators/setup-ssr/setup-ssr.spec.ts @@ -2,6 +2,7 @@ import { NxJsonConfiguration, readJson, readProjectConfiguration, + updateJson, updateProjectConfiguration, } from '@nrwl/devkit'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; @@ -177,4 +178,31 @@ describe('setupSSR', () => { readProjectConfiguration(tree, 'app1').targets.server ).toMatchSnapshot(); }); + + it('should install the correct versions when using older versions of Angular', async () => { + // ARRANGE + const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); + + await applicationGenerator(tree, { + name: 'app1', + }); + + updateJson(tree, 'package.json', (json) => ({ + ...json, + dependencies: { + '@angular/core': '14.2.0', + }, + })); + + // ACT + await setupSsr(tree, { project: 'app1' }); + + // ASSERT + const pkgJson = readJson(tree, 'package.json'); + expect(pkgJson.dependencies['@angular/platform-server']).toEqual('~14.2.0'); + expect(pkgJson.dependencies['@nguniversal/express-engine']).toEqual( + '~14.2.0' + ); + expect(pkgJson.devDependencies['@nguniversal/builders']).toEqual('~14.2.0'); + }); }); diff --git a/packages/angular/src/generators/setup-ssr/setup-ssr.ts b/packages/angular/src/generators/setup-ssr/setup-ssr.ts index 8db7118bb2a2b..fc242668c25c2 100644 --- a/packages/angular/src/generators/setup-ssr/setup-ssr.ts +++ b/packages/angular/src/generators/setup-ssr/setup-ssr.ts @@ -11,7 +11,13 @@ import { updateAppModule, updateProjectConfig, } from './lib'; -import { angularVersion, ngUniversalVersion } from '../../utils/versions'; +import { + angularVersion, + ngUniversalVersion, + versions, +} from '../../utils/versions'; +import { getInstalledAngularVersionInfo } from '../utils/angular-version-utils'; +import { coerce, major } from 'semver'; export async function setupSsr(tree: Tree, schema: Schema) { const options = normalizeOptions(tree, schema); @@ -20,14 +26,27 @@ export async function setupSsr(tree: Tree, schema: Schema) { updateAppModule(tree, options); updateProjectConfig(tree, options); + const installedAngularVersion = getInstalledAngularVersionInfo(tree); + const ngUniversalVersionToUse = + installedAngularVersion.major < major(coerce(angularVersion)) + ? versions[`angularV${installedAngularVersion.major}`] + ?.ngUniversalVersion ?? ngUniversalVersion + : ngUniversalVersion; + + const ngPlatformServerVersionToUse = + installedAngularVersion.major < major(coerce(angularVersion)) + ? versions[`angularV${installedAngularVersion.major}`]?.angularVersion ?? + angularVersion + : angularVersion; + addDependenciesToPackageJson( tree, { - '@nguniversal/express-engine': ngUniversalVersion, - '@angular/platform-server': angularVersion, + '@nguniversal/express-engine': ngUniversalVersionToUse, + '@angular/platform-server': ngPlatformServerVersionToUse, }, { - '@nguniversal/builders': ngUniversalVersion, + '@nguniversal/builders': ngUniversalVersionToUse, } ); diff --git a/packages/angular/src/utils/versions.ts b/packages/angular/src/utils/versions.ts index e6ef81ae31cce..edcae6a7520b6 100644 --- a/packages/angular/src/utils/versions.ts +++ b/packages/angular/src/utils/versions.ts @@ -12,7 +12,7 @@ export const tsLibVersion = '^2.3.0'; export const ngUniversalVersion = '~15.0.0'; export const corsVersion = '~2.8.5'; export const expressVersion = '~4.18.2'; -export const moduleFederationNodeVersion = '~0.9.9'; +export const moduleFederationNodeVersion = '~0.10.1'; export const angularEslintVersion = '~15.0.0'; export const tailwindVersion = '^3.0.2'; @@ -50,7 +50,7 @@ export const versions = { ngUniversalVersion: '~14.2.0', corsVersion: '~2.8.5', expressVersion: '~4.18.2', - moduleFederationNodeVersion: '~0.9.9', + moduleFederationNodeVersion: '^0.10.1', angularEslintVersion: '~14.0.4', tailwindVersion: '^3.0.2', postcssVersion: '^8.4.5',