Skip to content

Commit

Permalink
cleanup(angular): address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Mar 15, 2022
1 parent cf44ab5 commit 6738b1b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 29 deletions.
8 changes: 4 additions & 4 deletions packages/angular/src/utils/mfe/mfe-webpack.spec.ts
@@ -1,7 +1,7 @@
jest.mock('fs');
jest.mock('@nrwl/workspace');
jest.mock('@nrwl/workspace/src/utilities/typescript');
import * as fs from 'fs';
import * as workspace from '@nrwl/workspace';
import * as workspace from '@nrwl/workspace/src/utilities/typescript';

import { sharePackages, shareWorkspaceLibraries } from './mfe-webpack';

Expand All @@ -18,8 +18,8 @@ describe('MFE Webpack Utils', () => {
shareWorkspaceLibraries(['@myorg/shared']);
} catch (error) {
// ASSERT
expect(error.message).toEqual(
'NX MFE: TsConfig Path for workspace libraries does not exist! (null)'
expect(error.message).toContain(
'NX MFE: TsConfig Path for workspace libraries does not exist!'
);
}
});
Expand Down
6 changes: 4 additions & 2 deletions packages/angular/src/utils/mfe/mfe-webpack.ts
@@ -1,11 +1,13 @@
import { readTsConfig } from '@nrwl/workspace';
import { existsSync, readFileSync } from 'fs';
import { NormalModuleReplacementPlugin } from 'webpack';
import { appRootPath as rootPath } from 'nx/src/utils/app-root';
import { normalizePath, joinPathFragments } from '@nrwl/devkit';
import { dirname } from 'path';
import { ParsedCommandLine } from 'typescript';
import { getRootTsConfigPath } from '@nrwl/workspace/src/utilities/typescript';
import {
getRootTsConfigPath,
readTsConfig,
} from '@nrwl/workspace/src/utilities/typescript';

export interface SharedLibraryConfig {
singleton: boolean;
Expand Down
14 changes: 8 additions & 6 deletions packages/angular/src/utils/mfe/with-module-federation.spec.ts
@@ -1,9 +1,11 @@
jest.mock('fs');
jest.mock('@nrwl/workspace/src/core/project-graph');
jest.mock('@nrwl/workspace');
jest.mock('@nrwl/workspace/src/utilities/typescript');
jest.mock('@nrwl/workspace/src/core/file-utils');
jest.mock('nx/src/shared/workspace');
import * as graph from '@nrwl/workspace/src/core/project-graph';
import * as workspace from '@nrwl/workspace';
import * as typescriptUtils from '@nrwl/workspace/src/utilities/typescript';
import * as workspace from '@nrwl/workspace/src/core/file-utils';
import * as taoWorkspace from 'nx/src/shared/workspace';
import * as fs from 'fs';

Expand Down Expand Up @@ -46,7 +48,7 @@ describe('withModuleFederation', () => {
})
);

(workspace.readTsConfig as jest.Mock).mockReturnValue({
(typescriptUtils.readTsConfig as jest.Mock).mockReturnValue({
options: {
paths: {
shared: ['/libs/shared/src/index.ts'],
Expand Down Expand Up @@ -97,7 +99,7 @@ describe('withModuleFederation', () => {
})
);

(workspace.readTsConfig as jest.Mock).mockReturnValue({
(typescriptUtils.readTsConfig as jest.Mock).mockReturnValue({
options: {
paths: {
shared: ['/libs/shared/src/index.ts'],
Expand Down Expand Up @@ -149,7 +151,7 @@ describe('withModuleFederation', () => {
})
);

(workspace.readTsConfig as jest.Mock).mockReturnValue({
(typescriptUtils.readTsConfig as jest.Mock).mockReturnValue({
options: {
paths: {
shared: ['/libs/shared/src/index.ts'],
Expand Down Expand Up @@ -205,7 +207,7 @@ describe('withModuleFederation', () => {
})
);

(workspace.readTsConfig as jest.Mock).mockImplementation(() => ({
(typescriptUtils.readTsConfig as jest.Mock).mockImplementation(() => ({
options: {
paths: {
shared: ['/libs/shared/src/index.ts'],
Expand Down
50 changes: 33 additions & 17 deletions packages/angular/src/utils/mfe/with-module-federation.ts
Expand Up @@ -7,13 +7,15 @@ import {
createProjectGraphAsync,
readCachedProjectGraph,
} from '@nrwl/workspace/src/core/project-graph';
import { readWorkspaceJson } from '@nrwl/workspace';
import { readWorkspaceJson } from '@nrwl/workspace/src/core/file-utils';
import { joinPathFragments, ProjectGraph } from '@nrwl/devkit';
import ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
import { Workspaces } from 'nx/src/shared/workspace';
import { appRootPath } from 'nx/src/utils/app-root';
import { getRootTsConfigPath } from '@nrwl/workspace/src/utilities/typescript';
import { readTsConfig } from '@nrwl/workspace';
import {
getRootTsConfigPath,
readTsConfig,
} from '@nrwl/workspace/src/utilities/typescript';
import { ParsedCommandLine } from 'typescript';

export type MFERemotes = string[] | [remoteName: string, remoteUrl: string][];
Expand All @@ -30,8 +32,12 @@ export interface MFEConfig {

function recursivelyResolveWorkspaceDependents(
projectGraph: ProjectGraph<any>,
target: string
target: string,
collectedDeps: Set<string> = new Set()
) {
if (collectedDeps.has(target)) {
return [];
}
let dependencies = [target];

const workspaceDependencies = (
Expand All @@ -41,11 +47,16 @@ function recursivelyResolveWorkspaceDependents(
for (const dep of workspaceDependencies) {
dependencies = [
...dependencies,
...recursivelyResolveWorkspaceDependents(projectGraph, dep.target),
...recursivelyResolveWorkspaceDependents(
projectGraph,
dep.target,
collectedDeps
),
];
}
}
return dependencies;
collectedDeps = new Set([...collectedDeps, ...dependencies]);
return [...collectedDeps];
}

function mapWorkspaceLibrariesToTsConfigImport(workspaceLibraries: string[]) {
Expand Down Expand Up @@ -90,18 +101,18 @@ async function getDependentPackagesForProject(name: string) {

const deps = projectGraph.dependencies[name].reduce(
(dependencies, dependency) => {
const workspaceLibraries = dependencies.workspaceLibraries;
const npmPackages = dependencies.npmPackages;
const workspaceLibraries = new Set(dependencies.workspaceLibraries);
const npmPackages = new Set(dependencies.npmPackages);

if (dependency.target.startsWith('npm')) {
npmPackages.push(dependency.target.replace('npm:', ''));
if (dependency.target.startsWith('npm:')) {
npmPackages.add(dependency.target.replace('npm:', ''));
} else {
workspaceLibraries.push(dependency.target);
workspaceLibraries.add(dependency.target);
}

return {
workspaceLibraries,
npmPackages,
workspaceLibraries: [...workspaceLibraries],
npmPackages: [...npmPackages],
};
},
{ workspaceLibraries: [], npmPackages: [] }
Expand All @@ -122,10 +133,15 @@ async function getDependentPackagesForProject(name: string) {

function determineRemoteUrl(remote: string) {
const workspace = readWorkspaceJson();
return joinPathFragments(
workspace.projects[remote].targets.serve.options.publicHost,
'remoteEntry.mjs'
);
let publicHost = '';
try {
publicHost = workspace.projects[remote].targets.serve.options.publicHost;
} catch (error) {
throw new Error(
`Cannot automatically determine URL of remote (${remote}). Looked for property "publicHost" in the project's "serve" target.`
);
}
return joinPathFragments(publicHost, 'remoteEntry.mjs');
}

function mapRemotes(remotes: MFERemotes) {
Expand Down

0 comments on commit 6738b1b

Please sign in to comment.