diff --git a/packages/angular/src/utils/mfe-webpack.spec.ts b/packages/angular/src/utils/mfe-webpack.spec.ts index 82b5346cdfbdcb..1469d5a876af38 100644 --- a/packages/angular/src/utils/mfe-webpack.spec.ts +++ b/packages/angular/src/utils/mfe-webpack.spec.ts @@ -3,65 +3,123 @@ jest.mock('@nrwl/workspace'); import * as fs from 'fs'; import * as workspace from '@nrwl/workspace'; -import { shareWorkspaceLibraries } from './mfe-webpack'; +import { sharePackages, shareWorkspaceLibraries } from './mfe-webpack'; describe('MFE Webpack Utils', () => { afterEach(() => jest.clearAllMocks()); - it('should error when the tsconfig file does not exist', () => { - // ARRANGE - (fs.existsSync as jest.Mock).mockReturnValue(false); + describe('ShareWorkspaceLibraries', () => { + it('should error when the tsconfig file does not exist', () => { + // ARRANGE + (fs.existsSync as jest.Mock).mockReturnValue(false); - // ACT - try { - shareWorkspaceLibraries(['@myorg/shared']); - } catch (error) { + // ACT + try { + shareWorkspaceLibraries(['@myorg/shared']); + } catch (error) { + // ASSERT + expect(error.message).toEqual( + 'NX MFE: TsConfig Path for workspace libraries does not exist! (undefined)' + ); + } + }); + + it('should create an object with correct setup', () => { + // ARRANGE + (fs.existsSync as jest.Mock).mockReturnValue(true); + (workspace.readTsConfig as jest.Mock).mockReturnValue({ + options: { + paths: { + '@myorg/shared': ['/libs/shared/src/index.ts'], + }, + }, + }); + + // ACT + const sharedLibraries = shareWorkspaceLibraries(['@myorg/shared']); // ASSERT - expect(error.message).toEqual( - 'NX MFE: TsConfig Path for workspace libraries does not exist! (undefined)' + expect(sharedLibraries.getAliases()).toHaveProperty('@myorg/shared'); + expect(sharedLibraries.getAliases()['@myorg/shared']).toContain( + 'libs/shared/src/index.ts' ); - } - }); - - it('should create an object with correct setup', () => { - // ARRANGE - (fs.existsSync as jest.Mock).mockReturnValue(true); - (workspace.readTsConfig as jest.Mock).mockReturnValue({ - options: { - paths: { - '@myorg/shared': ['/libs/shared/src/index.ts'], + expect(sharedLibraries.getLibraries()).toEqual({ + '@myorg/shared': { + eager: undefined, + requiredVersion: false, }, - }, + }); }); - // ACT - const sharedLibraries = shareWorkspaceLibraries(['@myorg/shared']); - // ASSERT - expect(sharedLibraries.getAliases()).toHaveProperty('@myorg/shared'); - expect(sharedLibraries.getAliases()['@myorg/shared']).toContain( - 'libs/shared/src/index.ts' - ); - expect(sharedLibraries.getLibraries()).toEqual({ - '@myorg/shared': { - eager: undefined, - requiredVersion: false, - }, + it('should create an object with empty setup when tsconfig does not contain the shared lib', () => { + // ARRANGE + (fs.existsSync as jest.Mock).mockReturnValue(true); + (workspace.readTsConfig as jest.Mock).mockReturnValue({ + options: { + paths: {}, + }, + }); + + // ACT + const sharedLibraries = shareWorkspaceLibraries(['@myorg/shared']); + // ASSERT + expect(sharedLibraries.getAliases()).toEqual({}); + expect(sharedLibraries.getLibraries()).toEqual({}); }); }); - it('should create an object with empty setup when tsconfig does not contain the shared lib', () => { - // ARRANGE - (fs.existsSync as jest.Mock).mockReturnValue(true); - (workspace.readTsConfig as jest.Mock).mockReturnValue({ - options: { - paths: {}, - }, + describe('SharePackages', () => { + it('should throw when it cannot find root package.json', () => { + // ARRANGE + (fs.existsSync as jest.Mock).mockReturnValue(false); + + // ACT + try { + sharePackages(['@angular/core']); + } catch (error) { + // ASSERT + expect(error.message).toEqual( + 'NX MFE: Could not find root package.json to determine dependency versions.' + ); + } }); - // ACT - const sharedLibraries = shareWorkspaceLibraries(['@myorg/shared']); - // ASSERT - expect(sharedLibraries.getAliases()).toEqual({}); - expect(sharedLibraries.getLibraries()).toEqual({}); + it('should correctly map the shared packages to objects', () => { + // ARRANGE + (fs.existsSync as jest.Mock).mockReturnValue(true); + (fs.readFileSync as jest.Mock).mockReturnValue( + JSON.stringify({ + dependencies: { + '@angular/core': '~13.2.0', + '@angular/common': '~13.2.0', + rxjs: '~7.4.0', + }, + }) + ); + + // ACT + const packages = sharePackages([ + '@angular/core', + '@angular/common', + 'rxjs', + ]); + // ASSERT + expect(packages).toEqual({ + '@angular/core': { + singleton: true, + strictVersion: true, + requiredVersion: '~13.2.0', + }, + '@angular/common': { + singleton: true, + strictVersion: true, + requiredVersion: '~13.2.0', + }, + rxjs: { + singleton: true, + strictVersion: true, + requiredVersion: '~7.4.0', + }, + }); + }); }); }); diff --git a/packages/angular/src/utils/mfe-webpack.ts b/packages/angular/src/utils/mfe-webpack.ts index 6bd8364664ef64..57a6ecdd1dfa22 100644 --- a/packages/angular/src/utils/mfe-webpack.ts +++ b/packages/angular/src/utils/mfe-webpack.ts @@ -1,5 +1,5 @@ import { readTsConfig } from '@nrwl/workspace'; -import { existsSync } from 'fs'; +import { existsSync, readFileSync } from 'fs'; import { NormalModuleReplacementPlugin } from 'webpack'; import { appRootPath as rootPath } from '@nrwl/tao/src/utils/app-root'; import { normalizePath, joinPathFragments } from '@nrwl/devkit'; @@ -71,3 +71,26 @@ export function shareWorkspaceLibraries( }), }; } + +export function sharePackages(packages: string[]) { + const pkgJsonPath = joinPathFragments(rootPath, 'package.json'); + if (!existsSync(pkgJsonPath)) { + throw new Error( + 'NX MFE: Could not find root package.json to determine dependency versions.' + ); + } + + const pkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf-8')); + + return packages.reduce( + (shared, pkgName) => ({ + ...shared, + [pkgName]: { + singleton: true, + strictVersion: true, + requiredVersion: pkgJson.dependencies[pkgName], + }, + }), + {} + ); +}