Skip to content

Commit

Permalink
fix(react): normalize remote name and directory correctly when using …
Browse files Browse the repository at this point in the history
…new project root format
  • Loading branch information
leosvelperez committed Aug 22, 2023
1 parent 42d93b0 commit 428d6b3
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 15 deletions.
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);
}
4 changes: 1 addition & 3 deletions packages/react/src/generators/host/lib/setup-ssr-for-host.ts
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

0 comments on commit 428d6b3

Please sign in to comment.