Skip to content

Commit b8fc0f8

Browse files
authored
feat(angular): install correct versions for setup-ssr (#14278)
1 parent b7a134b commit b8fc0f8

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

packages/angular/src/generators/setup-ssr/setup-ssr.spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
NxJsonConfiguration,
33
readJson,
44
readProjectConfiguration,
5+
updateJson,
56
updateProjectConfiguration,
67
} from '@nrwl/devkit';
78
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
@@ -177,4 +178,31 @@ describe('setupSSR', () => {
177178
readProjectConfiguration(tree, 'app1').targets.server
178179
).toMatchSnapshot();
179180
});
181+
182+
it('should install the correct versions when using older versions of Angular', async () => {
183+
// ARRANGE
184+
const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
185+
186+
await applicationGenerator(tree, {
187+
name: 'app1',
188+
});
189+
190+
updateJson(tree, 'package.json', (json) => ({
191+
...json,
192+
dependencies: {
193+
'@angular/core': '14.2.0',
194+
},
195+
}));
196+
197+
// ACT
198+
await setupSsr(tree, { project: 'app1' });
199+
200+
// ASSERT
201+
const pkgJson = readJson(tree, 'package.json');
202+
expect(pkgJson.dependencies['@angular/platform-server']).toEqual('~14.2.0');
203+
expect(pkgJson.dependencies['@nguniversal/express-engine']).toEqual(
204+
'~14.2.0'
205+
);
206+
expect(pkgJson.devDependencies['@nguniversal/builders']).toEqual('~14.2.0');
207+
});
180208
});

packages/angular/src/generators/setup-ssr/setup-ssr.ts

+23-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ import {
1111
updateAppModule,
1212
updateProjectConfig,
1313
} from './lib';
14-
import { angularVersion, ngUniversalVersion } from '../../utils/versions';
14+
import {
15+
angularVersion,
16+
ngUniversalVersion,
17+
versions,
18+
} from '../../utils/versions';
19+
import { getInstalledAngularVersionInfo } from '../utils/angular-version-utils';
20+
import { coerce, major } from 'semver';
1521

1622
export async function setupSsr(tree: Tree, schema: Schema) {
1723
const options = normalizeOptions(tree, schema);
@@ -20,14 +26,27 @@ export async function setupSsr(tree: Tree, schema: Schema) {
2026
updateAppModule(tree, options);
2127
updateProjectConfig(tree, options);
2228

29+
const installedAngularVersion = getInstalledAngularVersionInfo(tree);
30+
const ngUniversalVersionToUse =
31+
installedAngularVersion.major < major(coerce(angularVersion))
32+
? versions[`angularV${installedAngularVersion.major}`]
33+
?.ngUniversalVersion ?? ngUniversalVersion
34+
: ngUniversalVersion;
35+
36+
const ngPlatformServerVersionToUse =
37+
installedAngularVersion.major < major(coerce(angularVersion))
38+
? versions[`angularV${installedAngularVersion.major}`]?.angularVersion ??
39+
angularVersion
40+
: angularVersion;
41+
2342
addDependenciesToPackageJson(
2443
tree,
2544
{
26-
'@nguniversal/express-engine': ngUniversalVersion,
27-
'@angular/platform-server': angularVersion,
45+
'@nguniversal/express-engine': ngUniversalVersionToUse,
46+
'@angular/platform-server': ngPlatformServerVersionToUse,
2847
},
2948
{
30-
'@nguniversal/builders': ngUniversalVersion,
49+
'@nguniversal/builders': ngUniversalVersionToUse,
3150
}
3251
);
3352

packages/angular/src/utils/versions.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const tsLibVersion = '^2.3.0';
1212
export const ngUniversalVersion = '~15.0.0';
1313
export const corsVersion = '~2.8.5';
1414
export const expressVersion = '~4.18.2';
15-
export const moduleFederationNodeVersion = '~0.9.9';
15+
export const moduleFederationNodeVersion = '~0.10.1';
1616

1717
export const angularEslintVersion = '~15.0.0';
1818
export const tailwindVersion = '^3.0.2';
@@ -50,7 +50,7 @@ export const versions = {
5050
ngUniversalVersion: '~14.2.0',
5151
corsVersion: '~2.8.5',
5252
expressVersion: '~4.18.2',
53-
moduleFederationNodeVersion: '~0.9.9',
53+
moduleFederationNodeVersion: '^0.10.1',
5454
angularEslintVersion: '~14.0.4',
5555
tailwindVersion: '^3.0.2',
5656
postcssVersion: '^8.4.5',

0 commit comments

Comments
 (0)