Skip to content

Commit

Permalink
feat(angular): install correct versions for setup-ssr (#14278)
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 authored Jan 12, 2023
1 parent b7a134b commit b8fc0f8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
28 changes: 28 additions & 0 deletions packages/angular/src/generators/setup-ssr/setup-ssr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
NxJsonConfiguration,
readJson,
readProjectConfiguration,
updateJson,
updateProjectConfiguration,
} from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
Expand Down Expand Up @@ -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');
});
});
27 changes: 23 additions & 4 deletions packages/angular/src/generators/setup-ssr/setup-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
}
);

Expand Down
4 changes: 2 additions & 2 deletions packages/angular/src/utils/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit b8fc0f8

Please sign in to comment.