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

This commit is contained in:
Leosvel Pérez Espinosa 2023-08-22 16:18:02 +01:00 committed by GitHub
parent 3f3f2475be
commit d1da057811
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 15 deletions

View File

@ -71,4 +71,26 @@ describe('hostGenerator', () => {
include: ['src/remotes.d.ts', 'src/main.server.tsx', 'server.ts'], 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'`);
});
}); });

View File

@ -7,18 +7,19 @@ import {
Tree, Tree,
updateProjectConfiguration, updateProjectConfiguration,
} from '@nx/devkit'; } from '@nx/devkit';
import { updateModuleFederationProject } from '../../rules/update-module-federation-project';
import applicationGenerator from '../application/application'; import applicationGenerator from '../application/application';
import { normalizeOptions } from '../application/lib/normalize-options'; 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 remoteGenerator from '../remote/remote';
import setupSsrGenerator from '../setup-ssr/setup-ssr'; 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 { 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) { export async function hostGenerator(host: Tree, schema: Schema) {
return hostGeneratorInternal(host, { return hostGeneratorInternal(host, {
@ -50,10 +51,12 @@ export async function hostGeneratorInternal(host: Tree, schema: Schema) {
if (schema.remotes) { if (schema.remotes) {
let remotePort = options.devServerPort + 1; let remotePort = options.devServerPort + 1;
for (const remote of schema.remotes) { 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, { await remoteGenerator(host, {
name: remote, name: remote,
directory: options.directory, directory: normalizeRemoteDirectory(remote, options),
style: options.style, style: options.style,
unitTestRunner: options.unitTestRunner, unitTestRunner: options.unitTestRunner,
e2eTestRunner: options.e2eTestRunner, e2eTestRunner: options.e2eTestRunner,
@ -61,6 +64,7 @@ export async function hostGeneratorInternal(host: Tree, schema: Schema) {
devServerPort: remotePort, devServerPort: remotePort,
ssr: options.ssr, ssr: options.ssr,
skipFormat: true, skipFormat: true,
projectNameAndRootFormat: options.projectNameAndRootFormat,
}); });
remotePort++; remotePort++;
} }

View File

@ -1,7 +1,6 @@
import { NormalizedSchema } from '../schema'; import { NormalizedSchema } from '../schema';
import { generateFiles, names } from '@nx/devkit'; import { generateFiles, names } from '@nx/devkit';
import { join } from 'path'; import { join } from 'path';
import { normalizeProjectName } from '../../application/lib/normalize-options';
export function addModuleFederationFiles( export function addModuleFederationFiles(
host, host,
@ -13,9 +12,8 @@ export function addModuleFederationFiles(
...options, ...options,
tmpl: '', tmpl: '',
remotes: defaultRemoteManifest.map(({ name, port }) => { remotes: defaultRemoteManifest.map(({ name, port }) => {
const remote = normalizeProjectName({ ...options, name });
return { return {
...names(remote), ...names(name),
port, port,
}; };
}), }),

View File

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

View File

@ -11,7 +11,6 @@ import {
import type { Schema } from '../schema'; import type { Schema } from '../schema';
import { moduleFederationNodeVersion } from '../../../utils/versions'; import { moduleFederationNodeVersion } from '../../../utils/versions';
import { normalizeProjectName } from '../../application/lib/normalize-options';
export async function setupSsrForHost( export async function setupSsrForHost(
tree: Tree, tree: Tree,
@ -31,9 +30,8 @@ export async function setupSsrForHost(
{ {
...options, ...options,
remotes: defaultRemoteManifest.map(({ name, port }) => { remotes: defaultRemoteManifest.map(({ name, port }) => {
const remote = normalizeProjectName({ ...options, name });
return { return {
...names(remote), ...names(name),
port, port,
}; };
}), }),