Skip to content

Commit

Permalink
fix(angular): skipping remotes should not only look at the current wo…
Browse files Browse the repository at this point in the history
…rkspace #17473
  • Loading branch information
Coly010 committed Jun 30, 2023
1 parent 3008be1 commit 58c71ff
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { Schema } from './schema';
import { readCachedProjectGraph, workspaceRoot, Workspaces } from '@nx/devkit';
import {
logger,
readCachedProjectGraph,
workspaceRoot,
Workspaces,
} from '@nx/devkit';
import { scheduleTarget } from 'nx/src/adapter/ngcli-adapter';
import { executeWebpackDevServerBuilder } from '../webpack-dev-server/webpack-dev-server.impl';
import { readProjectsConfigurationFromProjectGraph } from 'nx/src/project-graph/project-graph';
Expand Down Expand Up @@ -49,8 +54,16 @@ export function executeModuleFederationDevServerBuilder(
validateDevRemotes(options, workspaceProjects);

const remotesToSkip = new Set(
findMatchingProjects(options.skipRemotes, projectGraph.nodes) ?? []
findMatchingProjects(options.skipRemotes, projectGraph.nodes)
);

if (remotesToSkip.size > 0) {
logger.info(
`Not automatically serving remotes: ${[...Array.from(remotesToSkip)].join(
', '
)}`
);
}
const staticRemotes = getStaticRemotes(
project,
context,
Expand Down
52 changes: 21 additions & 31 deletions packages/angular/src/builders/utilities/module-federation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ProjectConfiguration } from 'nx/src/config/workspace-json-project-json';
import { join } from 'path';
import { existsSync, readFileSync } from 'fs';
import { Remotes } from '@nx/devkit';
import { logger, Remotes } from '@nx/devkit';

export function getDynamicRemotes(
project: ProjectConfiguration,
Expand Down Expand Up @@ -45,23 +45,19 @@ export function getDynamicRemotes(
return [];
}

const dynamicRemotes = Object.entries(parsedManifest)
const allDynamicRemotes = Object.entries(parsedManifest)
.map(([remoteName]) => remoteName)
.filter((r) => !remotesToSkip.has(r));
const invalidDynamicRemotes = dynamicRemotes.filter(
(remote) => !workspaceProjects[remote]
);
if (invalidDynamicRemotes.length) {
throw new Error(
invalidDynamicRemotes.length === 1
? `Invalid dynamic remote configured in "${pathToManifestFile}": ${invalidDynamicRemotes[0]}.`
: `Invalid dynamic remotes configured in "${pathToManifestFile}": ${invalidDynamicRemotes.join(
', '
)}.`
);
}

return dynamicRemotes;
return allDynamicRemotes.filter((remote) => {
if (!workspaceProjects[remote]) {
logger.warn(
`Skipping serving ${remote} as it could not be found in the workspace. Ensure it is served correctly.`
);
return false;
}
return true;
});
}

export function getStaticRemotes(
Expand Down Expand Up @@ -89,26 +85,20 @@ export function getStaticRemotes(
Array.isArray(mfeConfig.remotes) && mfeConfig.remotes.length > 0
? mfeConfig.remotes
: [];
const staticRemotes = remotesConfig
const allStaticRemotes = remotesConfig
.map((remoteDefinition) =>
Array.isArray(remoteDefinition) ? remoteDefinition[0] : remoteDefinition
)
.filter((r) => !remotesToSkip.has(r));

const invalidStaticRemotes = staticRemotes.filter(
(remote) => !workspaceProjects[remote]
);
if (invalidStaticRemotes.length) {
throw new Error(
invalidStaticRemotes.length === 1
? `Invalid static remote configured in "${mfConfigPath}": ${invalidStaticRemotes[0]}.`
: `Invalid static remotes configured in "${mfConfigPath}": ${invalidStaticRemotes.join(
', '
)}.`
);
}

return staticRemotes;
return allStaticRemotes.filter((remote) => {
if (!workspaceProjects[remote]) {
logger.warn(
`Skipping serving ${remote} as it could not be found in the workspace. Ensure it is served correctly.`
);
return false;
}
return true;
});
}

export function validateDevRemotes(
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/utils/find-matching-projects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {
findMatchingProjects,
getMatchingStringsWithCache,
} from './find-matching-projects';
import minimatch = require('minimatch');
import type { ProjectGraphProjectNode } from '../config/project-graph';
import minimatch = require('minimatch');

describe('findMatchingProjects', () => {
let projectGraph: Record<string, ProjectGraphProjectNode> = {
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/utils/find-matching-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function addMatchingProjectsByTag(
}
}

function parseStringPattern(
export function parseStringPattern(
pattern: string,
projects: Record<string, ProjectGraphProjectNode>
): ProjectPattern {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,29 @@ export default async function* moduleFederationDevServer(
}

const remotesToSkip = new Set(
findMatchingProjects(options.skipRemotes ?? [], context.projectGraph.nodes)
findMatchingProjects(options.skipRemotes, context.projectGraph.nodes)
);

if (remotesToSkip.size > 0) {
logger.info(
`Not automatically serving remotes: ${[...remotesToSkip.values()].join(
', '
)}`
);
}
const knownRemotes = (moduleFederationConfig.remotes ?? []).filter((r) => {
const validRemote = Array.isArray(r) ? r[0] : r;
return !remotesToSkip.has(validRemote);

if (remotesToSkip.has(validRemote)) {
return false;
} else if (!context.projectGraph.nodes[validRemote]) {
logger.warn(
`Skipping serving ${validRemote} as it could not be found in the workspace. Ensure it is served correctly.`
);
return false;
} else {
return true;
}
});
const remotePorts = knownRemotes.map(
(r) => context.projectGraph.nodes[r].data.targets['serve'].options.port
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,20 @@ export default async function* moduleFederationSsrDevServer(
}

const remotesToSkip = new Set(options.skipRemotes ?? []);
const knownRemotes = (moduleFederationConfig.remotes ?? []).filter(
(r) => !remotesToSkip.has(r)
);
const knownRemotes = (moduleFederationConfig.remotes ?? []).filter((r) => {
const validRemote = Array.isArray(r) ? r[0] : r;

if (remotesToSkip.has(validRemote)) {
return false;
} else if (!context.projectGraph.nodes[validRemote]) {
logger.warn(
`Skipping serving ${validRemote} as it could not be found in the workspace. Ensure it is served correctly.`
);
return false;
} else {
return true;
}
});

const devServeApps = !options.devRemotes
? []
Expand Down

0 comments on commit 58c71ff

Please sign in to comment.