Skip to content

Commit

Permalink
fix(module-federation): map static remote locations correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Feb 8, 2024
1 parent 49bba42 commit dc4b780
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ export async function buildStaticRemotes(
) {
return;
}
const mappedLocationOfRemotes: Record<string, string> = {};
for (const app of remotes.staticRemotes) {
mappedLocationOfRemotes[app] = `http${options.ssl ? 's' : ''}://${
options.host
}:${options.staticRemotesPort}/${app}`;
}
process.env.NX_MF_DEV_SERVER_STATIC_REMOTES = JSON.stringify(
mappedLocationOfRemotes
);

await new Promise<void>((res) => {
logger.info(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type ExecutorContext, workspaceRoot } from '@nx/devkit';
import { type Schema } from '../schema';
import fileServerExecutor from '@nx/web/src/executors/file-server/file-server.impl';
import { dirname, join } from 'path';
import { basename, dirname, join } from 'path';
import { cpSync } from 'fs';

export function startStaticRemotesFileServer(
Expand All @@ -15,10 +15,16 @@ export function startStaticRemotesFileServer(
) {
let shouldMoveToCommonLocation = false;
let commonOutputDirectory: string;
const remoteLocations: Record<string, string> = {};
const remoteUrls: Record<string, string> = {};
for (const app of remotes.staticRemotes) {
const outputPath =
context.projectGraph.nodes[app].data.targets['build'].options.outputPath;
const directoryOfOutputPath = dirname(outputPath);
remoteLocations[app] = basename(outputPath);
remoteUrls[app] = `http${options.ssl ? 's' : ''}://${options.host}:${
options.staticRemotesPort
}/${remoteLocations[app]}`;

if (!commonOutputDirectory) {
commonOutputDirectory = directoryOfOutputPath;
Expand All @@ -36,13 +42,15 @@ export function startStaticRemotesFileServer(
const outputPath =
context.projectGraph.nodes[app].data.targets['build'].options
.outputPath;
cpSync(outputPath, join(commonOutputDirectory, app), {
cpSync(outputPath, join(commonOutputDirectory, remoteLocations[app]), {
force: true,
recursive: true,
});
}
}

process.env.NX_MF_DEV_SERVER_STATIC_REMOTES = JSON.stringify(remoteUrls);

const staticRemotesIter = fileServerExecutor(
{
cors: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from '@nx/devkit/src/utils/async-iterable';
import { waitForPortOpen } from '@nx/web/src/utils/wait-for-port-open';
import { fork } from 'child_process';
import { dirname, join } from 'path';
import { basename, dirname, join } from 'path';
import { cpSync } from 'fs';

type ModuleFederationDevServerOptions = WebDevServerOptions & {
Expand Down Expand Up @@ -52,10 +52,16 @@ function startStaticRemotesFileServer(
) {
let shouldMoveToCommonLocation = false;
let commonOutputDirectory: string;
const remoteLocations: Record<string, string> = {};
const remoteUrls: Record<string, string> = {};
for (const app of remotes.staticRemotes) {
const outputPath =
context.projectGraph.nodes[app].data.targets['build'].options.outputPath;
const directoryOfOutputPath = dirname(outputPath);
remoteLocations[app] = basename(outputPath);
remoteUrls[app] = `http${options.ssl ? 's' : ''}://${options.host}:${
options.staticRemotesPort
}/${remoteLocations[app]}`;

if (!commonOutputDirectory) {
commonOutputDirectory = directoryOfOutputPath;
Expand All @@ -73,13 +79,15 @@ function startStaticRemotesFileServer(
const outputPath =
context.projectGraph.nodes[app].data.targets['build'].options
.outputPath;
cpSync(outputPath, join(commonOutputDirectory, app), {
cpSync(outputPath, join(commonOutputDirectory, remoteLocations[app]), {
force: true,
recursive: true,
});
}
}

process.env.NX_MF_DEV_SERVER_STATIC_REMOTES = JSON.stringify(remoteUrls);

const staticRemotesIter = fileServerExecutor(
{
cors: true,
Expand Down Expand Up @@ -151,17 +159,6 @@ async function buildStaticRemotes(
return;
}
logger.info(`NX Building ${remotes.staticRemotes.length} static remotes...`);
const mappedLocationOfRemotes: Record<string, string> = {};

for (const app of remotes.staticRemotes) {
mappedLocationOfRemotes[app] = `http${options.ssl ? 's' : ''}://${
options.host
}:${options.staticRemotesPort}/${app}`;
}

process.env.NX_MF_DEV_SERVER_STATIC_REMOTES = JSON.stringify(
mappedLocationOfRemotes
);

await new Promise<void>((res) => {
const staticProcess = fork(
Expand Down

0 comments on commit dc4b780

Please sign in to comment.