Skip to content

Commit

Permalink
chore(module-federation): clean up module-federation executors for react
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Dec 3, 2024
1 parent 283698c commit ca75e46
Show file tree
Hide file tree
Showing 21 changed files with 450 additions and 426 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function normalizeOptions(options: Schema): NormalizedSchema {
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 @@ -13,4 +13,5 @@ export interface Schema extends SSRDevServerBuilderOptions {
export interface NormalizedSchema extends Schema {
devRemotes: DevRemoteDefinition[];
ssl: boolean;
verbose: boolean;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './normalize-options';
export * from './start-remotes';
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
ExecutorContext,
parseTargetString,
readTargetOptions,
} from '@nx/devkit';
import {
ModuleFederationDevServerOptions,
NormalizedModuleFederationDevServerOptions,
} from '../schema';

export function getBuildOptions(buildTarget: string, context: ExecutorContext) {
const target = parseTargetString(buildTarget, context);

const buildOptions = readTargetOptions(target, context);

return {
...buildOptions,
};
}

export function normalizeOptions(
options: ModuleFederationDevServerOptions
): NormalizedModuleFederationDevServerOptions {
return {
...options,
devRemotes: options.devRemotes ?? [],
verbose: options.verbose ?? false,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { ExecutorContext, ProjectConfiguration, runExecutor } from '@nx/devkit';
import { NormalizedModuleFederationDevServerOptions } from '../schema';

export async function startRemotes(
remotes: string[],
workspaceProjects: Record<string, ProjectConfiguration>,
options: Pick<
NormalizedModuleFederationDevServerOptions,
'devRemotes' | 'host' | 'ssl' | 'sslCert' | 'sslKey' | 'verbose'
>,
context: ExecutorContext,
target: 'serve' | 'serve-static' = 'serve'
) {
const remoteIters: AsyncIterable<{ success: boolean }>[] = [];

for (const app of remotes) {
const remoteProjectServeTarget = workspaceProjects[app].targets[target];
const isUsingModuleFederationDevServerExecutor =
remoteProjectServeTarget.executor.includes(
'module-federation-dev-server'
);

const configurationOverride = options.devRemotes?.find(
(
r
): r is {
remoteName: string;
configuration: string;
} => typeof r !== 'string' && r.remoteName === app
)?.configuration;

const defaultOverrides = {
...(options.host ? { host: options.host } : {}),
...(options.ssl ? { ssl: options.ssl } : {}),
...(options.sslCert ? { sslCert: options.sslCert } : {}),
...(options.sslKey ? { sslKey: options.sslKey } : {}),
};
const overrides =
target === 'serve'
? {
watch: true,
...(isUsingModuleFederationDevServerExecutor
? { isInitialHost: false }
: {}),
...defaultOverrides,
}
: { ...defaultOverrides };

remoteIters.push(
await runExecutor(
{
project: app,
target,
configuration: configurationOverride ?? context.configurationName,
},
overrides,
context
)
);
}
return remoteIters;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import {
ExecutorContext,
logger,
parseTargetString,
ProjectConfiguration,
readTargetOptions,
runExecutor,
} from '@nx/devkit';
import { ExecutorContext, logger } from '@nx/devkit';
import devServerExecutor from '@nx/webpack/src/executors/dev-server/dev-server.impl';
import fileServerExecutor from '@nx/web/src/executors/file-server/file-server.impl';
import {
ModuleFederationDevServerOptions,
NormalizedModuleFederationDevServerOptions,
} from './schema';
import { ModuleFederationDevServerOptions } from './schema';
import { startRemoteIterators } from '@nx/module-federation/src/executors/utils';
import {
combineAsyncIterables,
Expand All @@ -20,86 +10,7 @@ import {
import { waitForPortOpen } from '@nx/web/src/utils/wait-for-port-open';
import { existsSync } from 'fs';
import { extname, join } from 'path';

function getBuildOptions(buildTarget: string, context: ExecutorContext) {
const target = parseTargetString(buildTarget, context);

const buildOptions = readTargetOptions(target, context);

return {
...buildOptions,
};
}

async function startRemotes(
remotes: string[],
workspaceProjects: Record<string, ProjectConfiguration>,
options: Pick<
NormalizedModuleFederationDevServerOptions,
'devRemotes' | 'host' | 'ssl' | 'sslCert' | 'sslKey' | 'verbose'
>,
context: ExecutorContext,
target: 'serve' | 'serve-static' = 'serve'
) {
const remoteIters: AsyncIterable<{ success: boolean }>[] = [];

for (const app of remotes) {
const remoteProjectServeTarget = workspaceProjects[app].targets[target];
const isUsingModuleFederationDevServerExecutor =
remoteProjectServeTarget.executor.includes(
'module-federation-dev-server'
);

const configurationOverride = options.devRemotes?.find(
(
r
): r is {
remoteName: string;
configuration: string;
} => typeof r !== 'string' && r.remoteName === app
)?.configuration;

const defaultOverrides = {
...(options.host ? { host: options.host } : {}),
...(options.ssl ? { ssl: options.ssl } : {}),
...(options.sslCert ? { sslCert: options.sslCert } : {}),
...(options.sslKey ? { sslKey: options.sslKey } : {}),
};
const overrides =
target === 'serve'
? {
watch: true,
...(isUsingModuleFederationDevServerExecutor
? { isInitialHost: false }
: {}),
...defaultOverrides,
}
: { ...defaultOverrides };

remoteIters.push(
await runExecutor(
{
project: app,
target,
configuration: configurationOverride ?? context.configurationName,
},
overrides,
context
)
);
}
return remoteIters;
}

function normalizeOptions(
options: ModuleFederationDevServerOptions
): NormalizedModuleFederationDevServerOptions {
return {
...options,
devRemotes: options.devRemotes ?? [],
verbose: options.verbose ?? false,
};
}
import { getBuildOptions, normalizeOptions, startRemotes } from './lib';

export default async function* moduleFederationDevServer(
schema: ModuleFederationDevServerOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './normalize-options';
export * from './start-remotes';
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
ModuleFederationSsrDevServerOptions,
NormalizedModuleFederationSsrDevServerOptions,
} from '../schema';
import { join } from 'path';
import {
workspaceRoot,
ExecutorContext,
parseTargetString,
readTargetOptions,
} from '@nx/devkit';

export function normalizeOptions(
options: ModuleFederationSsrDevServerOptions
): NormalizedModuleFederationSsrDevServerOptions {
return {
...options,
devRemotes: options.devRemotes ?? [],
verbose: options.verbose ?? false,
ssl: options.ssl ?? false,
sslCert: options.sslCert ? join(workspaceRoot, options.sslCert) : undefined,
sslKey: options.sslKey ? join(workspaceRoot, options.sslKey) : undefined,
};
}

export function getBuildOptions(buildTarget: string, context: ExecutorContext) {
const target = parseTargetString(buildTarget, context);

const buildOptions = readTargetOptions(target, context);

return {
...buildOptions,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { ModuleFederationSsrDevServerOptions } from '../schema';
import { runExecutor, ExecutorContext, ProjectConfiguration } from '@nx/devkit';

export async function startRemotes(
remotes: string[],
workspaceProjects: Record<string, ProjectConfiguration>,
options: Partial<
Pick<
ModuleFederationSsrDevServerOptions,
'devRemotes' | 'host' | 'ssl' | 'sslCert' | 'sslKey' | 'verbose'
>
>,
context: ExecutorContext
) {
const remoteIters: AsyncIterable<{ success: boolean }>[] = [];
const target = 'serve';
for (const app of remotes) {
const remoteProjectServeTarget = workspaceProjects[app].targets[target];
const isUsingModuleFederationSsrDevServerExecutor =
remoteProjectServeTarget.executor.includes(
'module-federation-ssr-dev-server'
);

const configurationOverride = options.devRemotes?.find(
(remote): remote is { remoteName: string; configuration: string } =>
typeof remote !== 'string' && remote.remoteName === app
)?.configuration;
{
const defaultOverrides = {
...(options.host ? { host: options.host } : {}),
...(options.ssl ? { ssl: options.ssl } : {}),
...(options.sslCert ? { sslCert: options.sslCert } : {}),
...(options.sslKey ? { sslKey: options.sslKey } : {}),
};

const overrides = {
watch: true,
...defaultOverrides,
...(isUsingModuleFederationSsrDevServerExecutor
? { isInitialHost: false }
: {}),
};

remoteIters.push(
await runExecutor(
{
project: app,
target,
configuration: configurationOverride ?? context.configurationName,
},
overrides,
context
)
);
}
}
return remoteIters;
}
Loading

0 comments on commit ca75e46

Please sign in to comment.