From 53799efed24cfea466e75588228d7fbe3db6a82d Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Thu, 24 Mar 2022 10:55:06 +0000 Subject: [PATCH] fix(angular): failing tests related to mfe (#9491) --- .../convert-to-with-mf/convert-to-with-mf.ts | 2 +- packages/angular/src/utils/get-mfe-projects.ts | 10 ++++++++-- .../__snapshots__/with-module-federation.spec.ts.snap | 2 +- .../src/utils/mfe/with-module-federation.spec.ts | 2 +- .../angular/src/utils/mfe/with-module-federation.ts | 11 +++++++---- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/angular/src/generators/convert-to-with-mf/convert-to-with-mf.ts b/packages/angular/src/generators/convert-to-with-mf/convert-to-with-mf.ts index ef331267956ba..bbec9cbdc59ea 100644 --- a/packages/angular/src/generators/convert-to-with-mf/convert-to-with-mf.ts +++ b/packages/angular/src/generators/convert-to-with-mf/convert-to-with-mf.ts @@ -13,7 +13,7 @@ import { } from './lib'; export default async function convertToWithMF(tree: Tree, schema: Schema) { - const projects = new Set(getMfeProjects(tree)); + const projects = new Set(getMfeProjects(tree, { legacy: true })); if (!projects.has(schema.project)) { throw new Error( diff --git a/packages/angular/src/utils/get-mfe-projects.ts b/packages/angular/src/utils/get-mfe-projects.ts index 602c2ffe2b9ee..095201f4005f3 100644 --- a/packages/angular/src/utils/get-mfe-projects.ts +++ b/packages/angular/src/utils/get-mfe-projects.ts @@ -2,9 +2,15 @@ import type { Tree } from '@nrwl/devkit'; import { tsquery } from '@phenomnomnominal/tsquery'; import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils'; -export function getMfeProjects(tree: Tree) { +export function getMfeProjects( + tree: Tree, + { legacy }: { legacy: boolean } = { legacy: false } +) { const NRWL_WEBPACK_BROWSER_BUILDER = '@nrwl/angular:webpack-browser'; const CUSTOM_WEBPACK_OPTION = 'customWebpackConfig'; + const MODULE_FEDERATION_IDENTIFIER = legacy + ? 'Identifier[name=ModuleFederationPlugin]' + : 'Identifier[name=withModuleFederation]'; const projects: string[] = []; forEachExecutorOptions( @@ -19,7 +25,7 @@ export function getMfeProjects(tree: Tree) { const ast = tsquery.ast(webpackConfig); const moduleFederationWebpackConfig = tsquery( ast, - 'Identifier[name=withModuleFederation]', + MODULE_FEDERATION_IDENTIFIER, { visitAllChildren: true, } diff --git a/packages/angular/src/utils/mfe/__snapshots__/with-module-federation.spec.ts.snap b/packages/angular/src/utils/mfe/__snapshots__/with-module-federation.spec.ts.snap index 9b085650596bf..36940b5a55263 100644 --- a/packages/angular/src/utils/mfe/__snapshots__/with-module-federation.spec.ts.snap +++ b/packages/angular/src/utils/mfe/__snapshots__/with-module-federation.spec.ts.snap @@ -48,7 +48,7 @@ Array [ }, "name": "host", "remotes": Object { - "remote1": "http:/localhost:4201/remoteEntry.mjs", + "remote1": "http://localhost:4201/remoteEntry.mjs", }, "shared": Object { "@angular/core": Object { diff --git a/packages/angular/src/utils/mfe/with-module-federation.spec.ts b/packages/angular/src/utils/mfe/with-module-federation.spec.ts index c94854aa0c40c..8b3b3416957fe 100644 --- a/packages/angular/src/utils/mfe/with-module-federation.spec.ts +++ b/packages/angular/src/utils/mfe/with-module-federation.spec.ts @@ -10,7 +10,7 @@ import * as fs from 'fs'; import { withModuleFederation } from './with-module-federation'; -xdescribe('withModuleFederation', () => { +describe('withModuleFederation', () => { afterEach(() => jest.clearAllMocks()); it('should create a host config correctly', async () => { // ARRANGE diff --git a/packages/angular/src/utils/mfe/with-module-federation.ts b/packages/angular/src/utils/mfe/with-module-federation.ts index 1a7d09a46bac7..8cc7255ea6f0f 100644 --- a/packages/angular/src/utils/mfe/with-module-federation.ts +++ b/packages/angular/src/utils/mfe/with-module-federation.ts @@ -4,9 +4,8 @@ import { shareWorkspaceLibraries, } from './mfe-webpack'; import { - appRootPath, + workspaceRoot, createProjectGraphAsync, - joinPathFragments, ProjectGraph, readCachedProjectGraph, Workspaces, @@ -62,7 +61,9 @@ function recursivelyResolveWorkspaceDependents( } function mapWorkspaceLibrariesToTsConfigImport(workspaceLibraries: string[]) { - const { projects } = new Workspaces(appRootPath).readWorkspaceConfiguration(); + const { projects } = new Workspaces( + workspaceRoot + ).readWorkspaceConfiguration(); const tsConfigPath = process.env.NX_TSCONFIG_PATH ?? getRootTsConfigPath(); const tsConfig: ParsedCommandLine = readTsConfig(tsConfigPath); @@ -150,7 +151,9 @@ function determineRemoteUrl(remote: string) { You can also use the tuple syntax in your webpack config to configure your remotes. e.g. \`remotes: [['remote1', 'http://localhost:4201']]\`` ); } - return joinPathFragments(publicHost, 'remoteEntry.mjs'); + return `${ + publicHost.endsWith('/') ? publicHost.slice(0, -1) : publicHost + }/remoteEntry.mjs`; } function mapRemotes(remotes: MFERemotes) {