fix(angular): set up host and remote ssr apps correctly #29442 (#29447)

## Current Behavior
During the update to support Angular 19, the host and remote generators'
`updateSsrSetup` function was missed.

This led to the creation of a second `server.ts` file, which would
result in the node server trying to instantiate twice.

## Expected Behavior
Fix the path normalization dependent on angular major version to ensure
only one server will be instantiated.

## Related Issue(s)

Fixes #29442
This commit is contained in:
Colum Ferry 2024-12-20 13:40:40 +00:00 committed by GitHub
parent efa5ba2bb6
commit 2143ea5e30
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 31 additions and 21 deletions

View File

@ -158,7 +158,7 @@ export default AppServerModule;
`; `;
exports[`Host App Generator --ssr should generate the correct files 5`] = ` exports[`Host App Generator --ssr should generate the correct files 5`] = `
"import('./src/main.server'); "import('./main.server');
" "
`; `;
@ -350,7 +350,7 @@ export default bootstrap;
`; `;
exports[`Host App Generator --ssr should generate the correct files for standalone 4`] = ` exports[`Host App Generator --ssr should generate the correct files for standalone 4`] = `
"import('./src/main.server'); "import('./main.server');
" "
`; `;
@ -573,7 +573,7 @@ export default bootstrap;
" "
`; `;
exports[`Host App Generator --ssr should generate the correct files for standalone when --typescript=true 4`] = `"import('./src/main.server');"`; exports[`Host App Generator --ssr should generate the correct files for standalone when --typescript=true 4`] = `"import('./main.server');"`;
exports[`Host App Generator --ssr should generate the correct files for standalone when --typescript=true 5`] = ` exports[`Host App Generator --ssr should generate the correct files for standalone when --typescript=true 5`] = `
"import { ModuleFederationConfig } from '@nx/module-federation'; "import { ModuleFederationConfig } from '@nx/module-federation';
@ -815,7 +815,7 @@ export default AppServerModule;
" "
`; `;
exports[`Host App Generator --ssr should generate the correct files when --typescript=true 5`] = `"import('./src/main.server');"`; exports[`Host App Generator --ssr should generate the correct files when --typescript=true 5`] = `"import('./main.server');"`;
exports[`Host App Generator --ssr should generate the correct files when --typescript=true 6`] = ` exports[`Host App Generator --ssr should generate the correct files when --typescript=true 6`] = `
"import { ModuleFederationConfig } from '@nx/module-federation'; "import { ModuleFederationConfig } from '@nx/module-federation';

View File

@ -365,7 +365,7 @@ describe('Host App Generator', () => {
tree.read(`test/src/bootstrap.server.ts`, 'utf-8') tree.read(`test/src/bootstrap.server.ts`, 'utf-8')
).toMatchSnapshot(); ).toMatchSnapshot();
expect(tree.read(`test/src/main.server.ts`, 'utf-8')).toMatchSnapshot(); expect(tree.read(`test/src/main.server.ts`, 'utf-8')).toMatchSnapshot();
expect(tree.read(`test/server.ts`, 'utf-8')).toMatchSnapshot(); expect(tree.read(`test/src/server.ts`, 'utf-8')).toMatchSnapshot();
expect( expect(
tree.read(`test/module-federation.config.js`, 'utf-8') tree.read(`test/module-federation.config.js`, 'utf-8')
).toMatchSnapshot(); ).toMatchSnapshot();
@ -402,7 +402,7 @@ describe('Host App Generator', () => {
tree.read(`test/src/bootstrap.server.ts`, 'utf-8') tree.read(`test/src/bootstrap.server.ts`, 'utf-8')
).toMatchSnapshot(); ).toMatchSnapshot();
expect(tree.read(`test/src/main.server.ts`, 'utf-8')).toMatchSnapshot(); expect(tree.read(`test/src/main.server.ts`, 'utf-8')).toMatchSnapshot();
expect(tree.read(`test/server.ts`, 'utf-8')).toMatchSnapshot(); expect(tree.read(`test/src/server.ts`, 'utf-8')).toMatchSnapshot();
expect( expect(
tree.read(`test/module-federation.config.ts`, 'utf-8') tree.read(`test/module-federation.config.ts`, 'utf-8')
).toMatchSnapshot(); ).toMatchSnapshot();
@ -435,7 +435,7 @@ describe('Host App Generator', () => {
tree.read(`test/src/bootstrap.server.ts`, 'utf-8') tree.read(`test/src/bootstrap.server.ts`, 'utf-8')
).toMatchSnapshot(); ).toMatchSnapshot();
expect(tree.read(`test/src/main.server.ts`, 'utf-8')).toMatchSnapshot(); expect(tree.read(`test/src/main.server.ts`, 'utf-8')).toMatchSnapshot();
expect(tree.read(`test/server.ts`, 'utf-8')).toMatchSnapshot(); expect(tree.read(`test/src/server.ts`, 'utf-8')).toMatchSnapshot();
expect( expect(
tree.read(`test/module-federation.config.js`, 'utf-8') tree.read(`test/module-federation.config.js`, 'utf-8')
).toMatchSnapshot(); ).toMatchSnapshot();
@ -475,7 +475,7 @@ describe('Host App Generator', () => {
tree.read(`test/src/bootstrap.server.ts`, 'utf-8') tree.read(`test/src/bootstrap.server.ts`, 'utf-8')
).toMatchSnapshot(); ).toMatchSnapshot();
expect(tree.read(`test/src/main.server.ts`, 'utf-8')).toMatchSnapshot(); expect(tree.read(`test/src/main.server.ts`, 'utf-8')).toMatchSnapshot();
expect(tree.read(`test/server.ts`, 'utf-8')).toMatchSnapshot(); expect(tree.read(`test/src/server.ts`, 'utf-8')).toMatchSnapshot();
expect( expect(
tree.read(`test/module-federation.config.ts`, 'utf-8') tree.read(`test/module-federation.config.ts`, 'utf-8')
).toMatchSnapshot(); ).toMatchSnapshot();

View File

@ -21,18 +21,23 @@ export async function updateSsrSetup(
appName: string, appName: string,
typescriptConfiguration: boolean typescriptConfiguration: boolean
) { ) {
const { major: angularMajorVersion } = getInstalledAngularVersionInfo(tree);
let project = readProjectConfiguration(tree, appName); let project = readProjectConfiguration(tree, appName);
tree.rename( tree.rename(
joinPathFragments(project.sourceRoot, 'main.server.ts'), joinPathFragments(project.sourceRoot, 'main.server.ts'),
joinPathFragments(project.sourceRoot, 'bootstrap.server.ts') joinPathFragments(project.sourceRoot, 'bootstrap.server.ts')
); );
tree.write( const pathToServerEntry = joinPathFragments(
joinPathFragments(project.root, 'server.ts'), angularMajorVersion >= 19
"import('./src/main.server');" ? project.sourceRoot ?? joinPathFragments(project.root, 'src')
: project.root,
'server.ts'
);
tree.write(
pathToServerEntry,
`import('./${angularMajorVersion >= 19 ? '' : 'src/'}main.server');`
); );
const { major: angularMajorVersion } = getInstalledAngularVersionInfo(tree);
generateFiles(tree, join(__dirname, '../files/common'), project.root, { generateFiles(tree, join(__dirname, '../files/common'), project.root, {
appName, appName,

View File

@ -125,7 +125,7 @@ export default AppServerModule;
`; `;
exports[`MF Remote App Generator --ssr should generate the correct files 5`] = ` exports[`MF Remote App Generator --ssr should generate the correct files 5`] = `
"import('./src/main.server'); "import('./main.server');
" "
`; `;
@ -357,7 +357,7 @@ export default AppServerModule;
" "
`; `;
exports[`MF Remote App Generator --ssr should generate the correct files when --typescriptConfiguration=true 5`] = `"import('./src/main.server');"`; exports[`MF Remote App Generator --ssr should generate the correct files when --typescriptConfiguration=true 5`] = `"import('./main.server');"`;
exports[`MF Remote App Generator --ssr should generate the correct files when --typescriptConfiguration=true 6`] = ` exports[`MF Remote App Generator --ssr should generate the correct files when --typescriptConfiguration=true 6`] = `
"import { ModuleFederationConfig } from '@nx/module-federation'; "import { ModuleFederationConfig } from '@nx/module-federation';

View File

@ -30,6 +30,7 @@ export async function updateSsrSetup(
skipPackageJson?: boolean; skipPackageJson?: boolean;
} }
) { ) {
const { major: angularMajorVersion } = getInstalledAngularVersionInfo(tree);
let project = readProjectConfiguration(tree, appName); let project = readProjectConfiguration(tree, appName);
tree.rename( tree.rename(
@ -37,9 +38,15 @@ export async function updateSsrSetup(
joinPathFragments(project.sourceRoot, 'bootstrap.server.ts') joinPathFragments(project.sourceRoot, 'bootstrap.server.ts')
); );
const pathToServerEntry = joinPathFragments(
angularMajorVersion >= 19
? project.sourceRoot ?? joinPathFragments(project.root, 'src')
: project.root,
'server.ts'
);
tree.write( tree.write(
joinPathFragments(project.root, 'server.ts'), pathToServerEntry,
"import('./src/main.server');" `import('./${angularMajorVersion >= 19 ? '' : 'src/'}main.server');`
); );
const browserBundleOutput = project.targets.build.options.outputPath; const browserBundleOutput = project.targets.build.options.outputPath;
@ -48,8 +55,6 @@ export async function updateSsrSetup(
'/server' '/server'
); );
const { major: angularMajorVersion } = getInstalledAngularVersionInfo(tree);
generateFiles(tree, join(__dirname, '../files/common'), project.root, { generateFiles(tree, join(__dirname, '../files/common'), project.root, {
appName, appName,
browserBundleOutput, browserBundleOutput,

View File

@ -329,7 +329,7 @@ describe('MF Remote App Generator', () => {
tree.read(`test/src/bootstrap.server.ts`, 'utf-8') tree.read(`test/src/bootstrap.server.ts`, 'utf-8')
).toMatchSnapshot(); ).toMatchSnapshot();
expect(tree.read(`test/src/main.server.ts`, 'utf-8')).toMatchSnapshot(); expect(tree.read(`test/src/main.server.ts`, 'utf-8')).toMatchSnapshot();
expect(tree.read(`test/server.ts`, 'utf-8')).toMatchSnapshot(); expect(tree.read(`test/src/server.ts`, 'utf-8')).toMatchSnapshot();
expect( expect(
tree.read(`test/module-federation.config.js`, 'utf-8') tree.read(`test/module-federation.config.js`, 'utf-8')
).toMatchSnapshot(); ).toMatchSnapshot();
@ -378,7 +378,7 @@ describe('MF Remote App Generator', () => {
tree.read(`test/src/bootstrap.server.ts`, 'utf-8') tree.read(`test/src/bootstrap.server.ts`, 'utf-8')
).toMatchSnapshot(); ).toMatchSnapshot();
expect(tree.read(`test/src/main.server.ts`, 'utf-8')).toMatchSnapshot(); expect(tree.read(`test/src/main.server.ts`, 'utf-8')).toMatchSnapshot();
expect(tree.read(`test/server.ts`, 'utf-8')).toMatchSnapshot(); expect(tree.read(`test/src/server.ts`, 'utf-8')).toMatchSnapshot();
expect( expect(
tree.read(`test/module-federation.config.ts`, 'utf-8') tree.read(`test/module-federation.config.ts`, 'utf-8')
).toMatchSnapshot(); ).toMatchSnapshot();