Skip to content

Commit

Permalink
feat(module-federation): move common executor logic to module-federat…
Browse files Browse the repository at this point in the history
…ion package (#29151)

## Current Behavior
The logic for the `module-federation-dev-server` and
`module-federation-ssr-dev-server` is duplicated across Angular, React
and Rspack.

The majority of this logic is the same, and the duplication causes an
increased maintenance tax.

## Expected Behavior
Move the logic into a utility that is exposed from
`@nx/module-federation`.
  • Loading branch information
Coly010 authored Dec 3, 2024
1 parent da901de commit 5448046
Show file tree
Hide file tree
Showing 42 changed files with 761 additions and 1,586 deletions.
1 change: 0 additions & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ rust-toolchain @nrwl/nx-native-reviewers
/packages/webpack/** @nrwl/nx-js-reviewers
/e2e/webpack/** @nrwl/nx-js-reviewers
/packages/rspack/** @nrwl/nx-js-reviewers
/packages/rspack/src/utils/module-federation @nrwl/nx-js-reviewers
/e2e/rspack/** @nrwl/nx-js-reviewers
/packages/esbuild/** @nrwl/nx-js-reviewers
/e2e/esbuild/** @nrwl/nx-js-reviewers
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export * from './build-static-remotes';
export * from './normalize-options';
export * from './start-dev-remotes';
export * from './start-static-remotes-file-server';
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function normalizeOptions(schema: Schema): NormalizedSchema {
liveReload: schema.liveReload ?? true,
open: schema.open ?? false,
ssl: schema.ssl ?? false,
verbose: schema.verbose ?? false,
sslCert: schema.sslCert ? join(workspaceRoot, schema.sslCert) : undefined,
sslKey: schema.sslKey ? join(workspaceRoot, schema.sslKey) : undefined,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
export async function startRemotes(
remotes: string[],
workspaceProjects: Record<string, ProjectConfiguration>,
options: Schema,
options: Pick<Schema, 'devRemotes' | 'verbose'>,
context: ExecutorContext,
target: 'serve' | 'serve-static' = 'serve'
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,14 @@ import {
readProjectsConfigurationFromProjectGraph,
} from '@nx/devkit';
import { type Schema } from './schema';
import {
buildStaticRemotes,
normalizeOptions,
startRemotes,
startStaticRemotesFileServer,
} from './lib';
import { normalizeOptions, startRemotes } from './lib';
import { eachValueFrom } from '@nx/devkit/src/utils/rxjs-for-await';
import {
combineAsyncIterables,
createAsyncIterable,
mapAsyncIterable,
} from '@nx/devkit/src/utils/async-iterable';
import {
getModuleFederationConfig,
getRemotes,
startRemoteProxies,
parseStaticRemotesConfig,
} from '@nx/module-federation/src/utils';
import { startRemoteIterators } from '@nx/module-federation/src/executors/utils';
import { waitForPortOpen } from '@nx/web/src/utils/wait-for-port-open';
import fileServerExecutor from '@nx/web/src/executors/file-server/file-server.impl';
import { createBuilderContext } from 'nx/src/adapter/ngcli-adapter';
Expand All @@ -37,8 +27,6 @@ export async function* moduleFederationDevServerExecutor(
schema: Schema,
context: ExecutorContext
) {
// Force Node to resolve to look for the nx binary that is inside node_modules
const nxBin = require.resolve('nx/bin/nx');
const options = normalizeOptions(schema);

const { projects: workspaceProjects } =
Expand Down Expand Up @@ -101,76 +89,14 @@ export async function* moduleFederationDevServerExecutor(

validateDevRemotes(options, workspaceProjects);

const moduleFederationConfig = getModuleFederationConfig(
project.targets.build.options.tsConfig,
context.root,
project.root,
'angular'
);

const remoteNames = options.devRemotes.map((r) =>
typeof r === 'string' ? r : r.remoteName
);

const remotes = getRemotes(
remoteNames,
options.skipRemotes,
moduleFederationConfig,
{
projectName: project.name,
projectGraph: context.projectGraph,
root: context.root,
},
pathToManifestFile
);

options.staticRemotesPort ??= remotes.staticRemotePort;

// Set NX_MF_DEV_REMOTES for the Nx Runtime Library Control Plugin
process.env.NX_MF_DEV_REMOTES = JSON.stringify([
...(
remotes.devRemotes.map((r) =>
typeof r === 'string' ? r : r.remoteName
) ?? []
).map((r) => r.replace(/-/g, '_')),
project.name.replace(/-/g, '_'),
]);

const staticRemotesConfig = parseStaticRemotesConfig(
[...remotes.staticRemotes, ...remotes.dynamicRemotes],
context
);
const mappedLocationsOfStaticRemotes = await buildStaticRemotes(
staticRemotesConfig,
nxBin,
context,
options
);

const devRemoteIters = await startRemotes(
remotes.devRemotes,
workspaceProjects,
options,
context,
'serve'
);

const staticRemotesIter = startStaticRemotesFileServer(
staticRemotesConfig,
context,
options
);

startRemoteProxies(
staticRemotesConfig,
mappedLocationsOfStaticRemotes,
options.ssl
? {
pathToCert: options.sslCert,
pathToKey: options.sslKey,
}
: undefined
);
const { remotes, staticRemotesIter, devRemoteIters } =
await startRemoteIterators(
options,
context,
startRemotes,
pathToManifestFile,
'angular'
);

const removeBaseUrlEmission = (iter: AsyncIterable<unknown>) =>
mapAsyncIterable(iter, (v) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ export type NormalizedSchema = SchemaWithBuildTarget & {
liveReload: boolean;
open: boolean;
ssl: boolean;
verbose: boolean;
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { workspaceRoot } from '@nx/devkit';
import type { Schema } from '../schema';
import type { NormalizedSchema, Schema } from '../schema';
import { join } from 'path';

export function normalizeOptions(options: Schema): Schema {
export function normalizeOptions(options: Schema): NormalizedSchema {
const devServeRemotes = !options.devRemotes
? []
: Array.isArray(options.devRemotes)
Expand All @@ -12,6 +12,7 @@ export function normalizeOptions(options: Schema): Schema {
return {
...options,
devRemotes: devServeRemotes,
verbose: options.verbose ?? false,
ssl: options.ssl ?? false,
sslCert: options.sslCert ? join(workspaceRoot, options.sslCert) : undefined,
sslKey: options.sslKey ? join(workspaceRoot, options.sslKey) : undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
export async function startRemotes(
remotes: string[],
workspaceProjects: Record<string, ProjectConfiguration>,
options: Schema,
options: Pick<Schema, 'devRemotes' | 'verbose'>,
context: ExecutorContext
) {
const target = 'serve-ssr';
Expand Down
Loading

0 comments on commit 5448046

Please sign in to comment.