Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react): normalize remote name and directory correctly when using new project root format #18770

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/react/src/generators/host/host.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,26 @@ describe('hostGenerator', () => {
include: ['src/remotes.d.ts', 'src/main.server.tsx', 'server.ts'],
});
});

it('should generate a host and remotes in a directory correctly when using --projectNameAndRootFormat=as-provided', async () => {
const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });

await hostGenerator(tree, {
name: 'hostApp',
directory: 'foo/hostApp',
remotes: ['remote1', 'remote2', 'remote3'],
projectNameAndRootFormat: 'as-provided',
e2eTestRunner: 'none',
linter: Linter.None,
style: 'css',
unitTestRunner: 'none',
});

expect(tree.exists('foo/remote1/project.json')).toBeTruthy();
expect(tree.exists('foo/remote2/project.json')).toBeTruthy();
expect(tree.exists('foo/remote3/project.json')).toBeTruthy();
expect(
tree.read('foo/host-app/module-federation.config.js', 'utf-8')
).toContain(`'remote1', 'remote2', 'remote3'`);
});
});
22 changes: 13 additions & 9 deletions packages/react/src/generators/host/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import {
Tree,
updateProjectConfiguration,
} from '@nx/devkit';

import { updateModuleFederationProject } from '../../rules/update-module-federation-project';
import applicationGenerator from '../application/application';
import { normalizeOptions } from '../application/lib/normalize-options';
import { updateModuleFederationProject } from '../../rules/update-module-federation-project';
import { addModuleFederationFiles } from './lib/add-module-federation-files';
import { updateModuleFederationE2eProject } from './lib/update-module-federation-e2e-project';

import { Schema } from './schema';
import remoteGenerator from '../remote/remote';

import setupSsrGenerator from '../setup-ssr/setup-ssr';
import { addModuleFederationFiles } from './lib/add-module-federation-files';
import {
normalizeRemoteDirectory,
normalizeRemoteName,
} from './lib/normalize-remote';
import { setupSsrForHost } from './lib/setup-ssr-for-host';
import { updateModuleFederationE2eProject } from './lib/update-module-federation-e2e-project';
import { Schema } from './schema';

export async function hostGenerator(host: Tree, schema: Schema) {
return hostGeneratorInternal(host, {
Expand Down Expand Up @@ -50,17 +51,20 @@ export async function hostGeneratorInternal(host: Tree, schema: Schema) {
if (schema.remotes) {
let remotePort = options.devServerPort + 1;
for (const remote of schema.remotes) {
remotesWithPorts.push({ name: remote, port: remotePort });
const remoteName = await normalizeRemoteName(host, remote, options);
remotesWithPorts.push({ name: remoteName, port: remotePort });

await remoteGenerator(host, {
name: remote,
directory: options.directory,
directory: normalizeRemoteDirectory(remote, options),
style: options.style,
unitTestRunner: options.unitTestRunner,
e2eTestRunner: options.e2eTestRunner,
linter: options.linter,
devServerPort: remotePort,
ssr: options.ssr,
skipFormat: true,
projectNameAndRootFormat: options.projectNameAndRootFormat,
});
remotePort++;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { NormalizedSchema } from '../schema';
import { generateFiles, names } from '@nx/devkit';
import { join } from 'path';
import { normalizeProjectName } from '../../application/lib/normalize-options';

export function addModuleFederationFiles(
host,
Expand All @@ -13,9 +12,8 @@ export function addModuleFederationFiles(
...options,
tmpl: '',
remotes: defaultRemoteManifest.map(({ name, port }) => {
const remote = normalizeProjectName({ ...options, name });
return {
...names(remote),
...names(name),
port,
};
}),
Expand Down
38 changes: 38 additions & 0 deletions packages/react/src/generators/host/lib/normalize-remote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Tree, joinPathFragments } from '@nx/devkit';
import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils';
import { NormalizedSchema } from '../schema';

export async function normalizeRemoteName(
tree: Tree,
remote: string,
options: NormalizedSchema
) {
const { projectName: remoteName } = await determineProjectNameAndRootOptions(
tree,
{
name: remote,
projectType: 'application',
directory: options.directory,
projectNameAndRootFormat: options.projectNameAndRootFormat,
callingGenerator: '@nx/react:host',
}
);

return remoteName;
}

export function normalizeRemoteDirectory(
remote: string,
options: NormalizedSchema
) {
if (options.projectNameAndRootFormat === 'derived' || !options.directory) {
return options.directory;
}

/**
* With the `as-provided` format, the provided directory would be the root
* of the host application. Append the remote name to the host parent
* directory to get the remote directory.
*/
return joinPathFragments(options.directory, '..', remote);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {

import type { Schema } from '../schema';
import { moduleFederationNodeVersion } from '../../../utils/versions';
import { normalizeProjectName } from '../../application/lib/normalize-options';

export async function setupSsrForHost(
tree: Tree,
Expand All @@ -31,9 +30,8 @@ export async function setupSsrForHost(
{
...options,
remotes: defaultRemoteManifest.map(({ name, port }) => {
const remote = normalizeProjectName({ ...options, name });
return {
...names(remote),
...names(name),
port,
};
}),
Expand Down