diff --git a/CODEOWNERS b/CODEOWNERS index b983bc355a0df..7419f6615225c 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -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 diff --git a/packages/angular/src/executors/module-federation-ssr-dev-server/lib/normalize-options.ts b/packages/angular/src/executors/module-federation-ssr-dev-server/lib/normalize-options.ts index 884a4c0ff4241..89d4a67204485 100644 --- a/packages/angular/src/executors/module-federation-ssr-dev-server/lib/normalize-options.ts +++ b/packages/angular/src/executors/module-federation-ssr-dev-server/lib/normalize-options.ts @@ -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, diff --git a/packages/angular/src/executors/module-federation-ssr-dev-server/schema.d.ts b/packages/angular/src/executors/module-federation-ssr-dev-server/schema.d.ts index 185b6caa09dc0..821a5834dd974 100644 --- a/packages/angular/src/executors/module-federation-ssr-dev-server/schema.d.ts +++ b/packages/angular/src/executors/module-federation-ssr-dev-server/schema.d.ts @@ -13,4 +13,5 @@ export interface Schema extends SSRDevServerBuilderOptions { export interface NormalizedSchema extends Schema { devRemotes: DevRemoteDefinition[]; ssl: boolean; + verbose: boolean; } diff --git a/packages/react/src/executors/module-federation-dev-server/lib/index.ts b/packages/react/src/executors/module-federation-dev-server/lib/index.ts new file mode 100644 index 0000000000000..7923ac83c9890 --- /dev/null +++ b/packages/react/src/executors/module-federation-dev-server/lib/index.ts @@ -0,0 +1,2 @@ +export * from './normalize-options'; +export * from './start-remotes'; diff --git a/packages/react/src/executors/module-federation-dev-server/lib/normalize-options.ts b/packages/react/src/executors/module-federation-dev-server/lib/normalize-options.ts new file mode 100644 index 0000000000000..1cebe6567eb20 --- /dev/null +++ b/packages/react/src/executors/module-federation-dev-server/lib/normalize-options.ts @@ -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, + }; +} diff --git a/packages/react/src/executors/module-federation-dev-server/lib/start-remotes.ts b/packages/react/src/executors/module-federation-dev-server/lib/start-remotes.ts new file mode 100644 index 0000000000000..d680317e152dc --- /dev/null +++ b/packages/react/src/executors/module-federation-dev-server/lib/start-remotes.ts @@ -0,0 +1,62 @@ +import { ExecutorContext, ProjectConfiguration, runExecutor } from '@nx/devkit'; +import { NormalizedModuleFederationDevServerOptions } from '../schema'; + +export async function startRemotes( + remotes: string[], + workspaceProjects: Record, + 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; +} diff --git a/packages/react/src/executors/module-federation-dev-server/module-federation-dev-server.impl.ts b/packages/react/src/executors/module-federation-dev-server/module-federation-dev-server.impl.ts index e5ffcbba87fc3..59f01abdded67 100644 --- a/packages/react/src/executors/module-federation-dev-server/module-federation-dev-server.impl.ts +++ b/packages/react/src/executors/module-federation-dev-server/module-federation-dev-server.impl.ts @@ -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, @@ -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, - 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, diff --git a/packages/react/src/executors/module-federation-ssr-dev-server/lib/index.ts b/packages/react/src/executors/module-federation-ssr-dev-server/lib/index.ts new file mode 100644 index 0000000000000..7923ac83c9890 --- /dev/null +++ b/packages/react/src/executors/module-federation-ssr-dev-server/lib/index.ts @@ -0,0 +1,2 @@ +export * from './normalize-options'; +export * from './start-remotes'; diff --git a/packages/react/src/executors/module-federation-ssr-dev-server/lib/normalize-options.ts b/packages/react/src/executors/module-federation-ssr-dev-server/lib/normalize-options.ts new file mode 100644 index 0000000000000..d3b030d22adcb --- /dev/null +++ b/packages/react/src/executors/module-federation-ssr-dev-server/lib/normalize-options.ts @@ -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, + }; +} diff --git a/packages/react/src/executors/module-federation-ssr-dev-server/lib/start-remotes.ts b/packages/react/src/executors/module-federation-ssr-dev-server/lib/start-remotes.ts new file mode 100644 index 0000000000000..6072069beb368 --- /dev/null +++ b/packages/react/src/executors/module-federation-ssr-dev-server/lib/start-remotes.ts @@ -0,0 +1,58 @@ +import { ModuleFederationSsrDevServerOptions } from '../schema'; +import { runExecutor, ExecutorContext, ProjectConfiguration } from '@nx/devkit'; + +export async function startRemotes( + remotes: string[], + workspaceProjects: Record, + 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; +} diff --git a/packages/react/src/executors/module-federation-ssr-dev-server/module-federation-ssr-dev-server.impl.ts b/packages/react/src/executors/module-federation-ssr-dev-server/module-federation-ssr-dev-server.impl.ts index 9e48bd91236d7..2580abb00d217 100644 --- a/packages/react/src/executors/module-federation-ssr-dev-server/module-federation-ssr-dev-server.impl.ts +++ b/packages/react/src/executors/module-federation-ssr-dev-server/module-federation-ssr-dev-server.impl.ts @@ -1,130 +1,15 @@ -import { - ExecutorContext, - logger, - parseTargetString, - ProjectConfiguration, - readTargetOptions, - runExecutor, - workspaceRoot, -} from '@nx/devkit'; +import { ExecutorContext, logger } from '@nx/devkit'; import ssrDevServerExecutor from '@nx/webpack/src/executors/ssr-dev-server/ssr-dev-server.impl'; -import { WebSsrDevServerOptions } from '@nx/webpack/src/executors/ssr-dev-server/schema'; import { extname, join } from 'path'; -import { - DevRemoteDefinition, - startRemoteIterators, -} from '@nx/module-federation/src/executors/utils'; +import { startRemoteIterators } from '@nx/module-federation/src/executors/utils'; import { combineAsyncIterables, createAsyncIterable, } from '@nx/devkit/src/utils/async-iterable'; import { existsSync } from 'fs'; import { waitForPortOpen } from '@nx/web/src/utils/wait-for-port-open'; - -type ModuleFederationSsrDevServerOptions = WebSsrDevServerOptions & { - devRemotes?: ( - | string - | { - remoteName: string; - configuration: string; - } - )[]; - - skipRemotes?: string[]; - host: string; - pathToManifestFile?: string; - staticRemotesPort?: number; - parallel?: number; - ssl?: boolean; - sslKey?: string; - sslCert?: string; - isInitialHost?: boolean; - verbose?: boolean; -}; -type NormalizedModuleFederationSsrDevServerOptions = - ModuleFederationSsrDevServerOptions & { - devRemotes: DevRemoteDefinition[]; - verbose: boolean; - }; - -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, - }; -} - -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, - 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; -} +import { ModuleFederationSsrDevServerOptions } from './schema'; +import { getBuildOptions, normalizeOptions, startRemotes } from './lib'; export default async function* moduleFederationSsrDevServer( ssrDevServerOptions: ModuleFederationSsrDevServerOptions, diff --git a/packages/react/src/executors/module-federation-ssr-dev-server/schema.d.ts b/packages/react/src/executors/module-federation-ssr-dev-server/schema.d.ts new file mode 100644 index 0000000000000..d37070f0209cb --- /dev/null +++ b/packages/react/src/executors/module-federation-ssr-dev-server/schema.d.ts @@ -0,0 +1,29 @@ +import { WebSsrDevServerOptions } from '@nx/webpack/src/executors/ssr-dev-server/schema'; +import { DevRemoteDefinition } from '@nx/module-federation/src/executors/utils'; + +export type ModuleFederationSsrDevServerOptions = WebSsrDevServerOptions & { + devRemotes?: ( + | string + | { + remoteName: string; + configuration: string; + } + )[]; + + skipRemotes?: string[]; + host: string; + pathToManifestFile?: string; + staticRemotesPort?: number; + parallel?: number; + ssl?: boolean; + sslKey?: string; + sslCert?: string; + isInitialHost?: boolean; + verbose?: boolean; +}; + +export type NormalizedModuleFederationSsrDevServerOptions = + ModuleFederationSsrDevServerOptions & { + devRemotes: DevRemoteDefinition[]; + verbose: boolean; + }; diff --git a/packages/rspack/src/executors/module-federation-dev-server/lib/index.ts b/packages/rspack/src/executors/module-federation-dev-server/lib/index.ts new file mode 100644 index 0000000000000..7923ac83c9890 --- /dev/null +++ b/packages/rspack/src/executors/module-federation-dev-server/lib/index.ts @@ -0,0 +1,2 @@ +export * from './normalize-options'; +export * from './start-remotes'; diff --git a/packages/rspack/src/executors/module-federation-dev-server/lib/normalize-options.ts b/packages/rspack/src/executors/module-federation-dev-server/lib/normalize-options.ts new file mode 100644 index 0000000000000..1cebe6567eb20 --- /dev/null +++ b/packages/rspack/src/executors/module-federation-dev-server/lib/normalize-options.ts @@ -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, + }; +} diff --git a/packages/rspack/src/executors/module-federation-dev-server/lib/start-remotes.ts b/packages/rspack/src/executors/module-federation-dev-server/lib/start-remotes.ts new file mode 100644 index 0000000000000..a64079d44a37b --- /dev/null +++ b/packages/rspack/src/executors/module-federation-dev-server/lib/start-remotes.ts @@ -0,0 +1,64 @@ +import { ModuleFederationDevServerOptions } from '../schema'; +import { ProjectConfiguration, ExecutorContext, runExecutor } from '@nx/devkit'; + +export async function startRemotes( + remotes: string[], + workspaceProjects: Record, + options: Partial< + Pick< + ModuleFederationDevServerOptions, + '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; +} diff --git a/packages/rspack/src/executors/module-federation-dev-server/module-federation-dev-server.impl.ts b/packages/rspack/src/executors/module-federation-dev-server/module-federation-dev-server.impl.ts index 1b2ac9ecf86cd..2d00b01a3eae6 100644 --- a/packages/rspack/src/executors/module-federation-dev-server/module-federation-dev-server.impl.ts +++ b/packages/rspack/src/executors/module-federation-dev-server/module-federation-dev-server.impl.ts @@ -1,11 +1,4 @@ -import { - ExecutorContext, - logger, - parseTargetString, - ProjectConfiguration, - readTargetOptions, - runExecutor, -} from '@nx/devkit'; +import { ExecutorContext, logger } from '@nx/devkit'; import { combineAsyncIterables, createAsyncIterable, @@ -16,92 +9,8 @@ import { existsSync } from 'fs'; import { extname, join } from 'path'; import { startRemoteIterators } from '@nx/module-federation/src/executors/utils'; import devServerExecutor from '../dev-server/dev-server.impl'; -import { - ModuleFederationDevServerOptions, - NormalizedModuleFederationDevServerOptions, -} from './schema'; - -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, - options: Partial< - Pick< - ModuleFederationDevServerOptions, - '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 { ModuleFederationDevServerOptions } from './schema'; +import { getBuildOptions, normalizeOptions, startRemotes } from './lib'; export default async function* moduleFederationDevServer( schema: ModuleFederationDevServerOptions, diff --git a/packages/rspack/src/executors/module-federation-ssr-dev-server/lib/index.ts b/packages/rspack/src/executors/module-federation-ssr-dev-server/lib/index.ts new file mode 100644 index 0000000000000..7923ac83c9890 --- /dev/null +++ b/packages/rspack/src/executors/module-federation-ssr-dev-server/lib/index.ts @@ -0,0 +1,2 @@ +export * from './normalize-options'; +export * from './start-remotes'; diff --git a/packages/rspack/src/executors/module-federation-ssr-dev-server/lib/normalize-options.ts b/packages/rspack/src/executors/module-federation-ssr-dev-server/lib/normalize-options.ts new file mode 100644 index 0000000000000..d3b030d22adcb --- /dev/null +++ b/packages/rspack/src/executors/module-federation-ssr-dev-server/lib/normalize-options.ts @@ -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, + }; +} diff --git a/packages/rspack/src/executors/module-federation-ssr-dev-server/lib/start-remotes.ts b/packages/rspack/src/executors/module-federation-ssr-dev-server/lib/start-remotes.ts new file mode 100644 index 0000000000000..6072069beb368 --- /dev/null +++ b/packages/rspack/src/executors/module-federation-ssr-dev-server/lib/start-remotes.ts @@ -0,0 +1,58 @@ +import { ModuleFederationSsrDevServerOptions } from '../schema'; +import { runExecutor, ExecutorContext, ProjectConfiguration } from '@nx/devkit'; + +export async function startRemotes( + remotes: string[], + workspaceProjects: Record, + 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; +} diff --git a/packages/rspack/src/executors/module-federation-ssr-dev-server/module-federation-ssr-dev-server.impl.ts b/packages/rspack/src/executors/module-federation-ssr-dev-server/module-federation-ssr-dev-server.impl.ts index 8f23f0fa9ea77..5e8b673ac5332 100644 --- a/packages/rspack/src/executors/module-federation-ssr-dev-server/module-federation-ssr-dev-server.impl.ts +++ b/packages/rspack/src/executors/module-federation-ssr-dev-server/module-federation-ssr-dev-server.impl.ts @@ -1,18 +1,6 @@ -import { - ExecutorContext, - logger, - parseTargetString, - ProjectConfiguration, - readTargetOptions, - runExecutor, - workspaceRoot, -} from '@nx/devkit'; +import { ExecutorContext, logger } from '@nx/devkit'; import { extname, join } from 'path'; -import { - DevRemoteDefinition, - startRemoteIterators, -} from '@nx/module-federation/src/executors/utils'; -import { RspackSsrDevServerOptions } from '../ssr-dev-server/schema'; +import { startRemoteIterators } from '@nx/module-federation/src/executors/utils'; import ssrDevServerExecutor from '../ssr-dev-server/ssr-dev-server.impl'; import { combineAsyncIterables, @@ -20,112 +8,8 @@ import { } from '@nx/devkit/src/utils/async-iterable'; import { existsSync } from 'fs'; import { waitForPortOpen } from '@nx/web/src/utils/wait-for-port-open'; - -type ModuleFederationSsrDevServerOptions = RspackSsrDevServerOptions & { - devRemotes?: ( - | string - | { - remoteName: string; - configuration: string; - } - )[]; - - skipRemotes?: string[]; - host: string; - pathToManifestFile?: string; - staticRemotesPort?: number; - parallel?: number; - ssl?: boolean; - sslKey?: string; - sslCert?: string; - isInitialHost?: boolean; - verbose?: boolean; -}; - -type NormalizedModuleFederationSsrDevServerOptions = - ModuleFederationSsrDevServerOptions & { - devRemotes: DevRemoteDefinition[]; - verbose: boolean; - }; - -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, - }; -} - -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, - 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; -} +import { ModuleFederationSsrDevServerOptions } from './schema'; +import { getBuildOptions, normalizeOptions, startRemotes } from './lib'; export default async function* moduleFederationSsrDevServer( ssrDevServerOptions: ModuleFederationSsrDevServerOptions, diff --git a/packages/rspack/src/executors/module-federation-ssr-dev-server/schema.d.ts b/packages/rspack/src/executors/module-federation-ssr-dev-server/schema.d.ts new file mode 100644 index 0000000000000..8df7a459c72e1 --- /dev/null +++ b/packages/rspack/src/executors/module-federation-ssr-dev-server/schema.d.ts @@ -0,0 +1,29 @@ +import { DevRemoteDefinition } from '@nx/module-federation/src/executors/utils'; +import { RspackSsrDevServerOptions } from '../ssr-dev-server/schema'; + +export type ModuleFederationSsrDevServerOptions = RspackSsrDevServerOptions & { + devRemotes?: ( + | string + | { + remoteName: string; + configuration: string; + } + )[]; + + skipRemotes?: string[]; + host: string; + pathToManifestFile?: string; + staticRemotesPort?: number; + parallel?: number; + ssl?: boolean; + sslKey?: string; + sslCert?: string; + isInitialHost?: boolean; + verbose?: boolean; +}; + +export type NormalizedModuleFederationSsrDevServerOptions = + ModuleFederationSsrDevServerOptions & { + devRemotes: DevRemoteDefinition[]; + verbose: boolean; + };