feat(angular): install correct versions for setup-ssr (#14278)

This commit is contained in:
Colum Ferry 2023-01-12 10:10:12 +00:00 committed by GitHub
parent b7a134baf6
commit b8fc0f87ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 6 deletions

View File

@ -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');
});
});

View File

@ -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,
}
);

View File

@ -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',