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

feat(module-federation): move common executor logic to module-federation package #29151

Merged
merged 5 commits into from
Dec 3, 2024
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
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
Loading