Skip to content

Commit

Permalink
fix(react): module-federation-ssr-dev-server should use fork (#13791)
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Dec 13, 2022
1 parent 8ffa9fd commit 61af68b
Showing 1 changed file with 53 additions and 16 deletions.
@@ -1,12 +1,20 @@
import { ExecutorContext, logger, runExecutor } from '@nrwl/devkit';
import {
ExecutorContext,
logger,
runExecutor,
workspaceRoot,
} from '@nrwl/devkit';
import ssrDevServerExecutor from '@nrwl/webpack/src/executors/ssr-dev-server/ssr-dev-server.impl';
import { WebSsrDevServerOptions } from '@nrwl/webpack/src/executors/ssr-dev-server/schema';
import { join } from 'path';
import * as chalk from 'chalk';
import {
combineAsyncIterableIterators,
createAsyncIterable,
mapAsyncIterable,
tapAsyncIterable,
} from '@nrwl/devkit/src/utils/async-iterable';
import { execSync, fork } from 'child_process';

type ModuleFederationDevServerOptions = WebSsrDevServerOptions & {
devRemotes?: string | string[];
Expand All @@ -18,7 +26,7 @@ export default async function* moduleFederationSsrDevServer(
options: ModuleFederationDevServerOptions,
context: ExecutorContext
) {
let iter = ssrDevServerExecutor(options, context);
let iter: any = ssrDevServerExecutor(options, context);
const p = context.workspace.projects[context.projectName];

const moduleFederationConfigPath = join(
Expand Down Expand Up @@ -51,20 +59,49 @@ export default async function* moduleFederationSsrDevServer(
for (const app of knownRemotes) {
const [appName] = Array.isArray(app) ? app : [app];
const isDev = devServeApps.includes(appName);
iter = combineAsyncIterableIterators(
iter,
await runExecutor(
{
project: appName,
target: isDev ? 'serve' : 'serve-server',
configuration: context.configurationName,
},
{
watch: isDev,
},
context
)
);
const remoteServeIter = isDev
? await runExecutor(
{
project: appName,
target: 'serve',
configuration: context.configurationName,
},
{
watch: isDev,
},
context
)
: mapAsyncIterable(
createAsyncIterable(async ({ next, done }) => {
const remoteProject = context.workspace.projects[appName];
const remoteServerOutput = join(
workspaceRoot,
remoteProject.targets.server.options.outputPath,
'main.js'
);
execSync(
`npx nx run ${appName}:server${
context.configurationName ? `:${context.configurationName}` : ''
}`,
{ stdio: 'inherit' }
);
const child = fork(remoteServerOutput, {
env: {
PORT: remoteProject.targets['serve-browser'].options.port,
},
});

child.on('message', (msg) => {
if (msg === 'nx.server.ready') {
next(true);
done();
}
});
}),
(x) => x
);

iter = combineAsyncIterableIterators(iter, remoteServeIter);
}

let numAwaiting = knownRemotes.length + 1; // remotes + host
Expand Down

0 comments on commit 61af68b

Please sign in to comment.