From 06301f38703890a1e97ce99fcba649938ca68620 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Fri, 27 Jan 2023 08:12:24 -0500 Subject: [PATCH] fix(react): install @nrwl/web when module federation is used --- .../react/src/generators/host/host.spec.ts | 17 +++++++++++++++++ .../src/generators/remote/remote.spec.ts | 19 +++++++++++++++++-- .../rules/update-module-federation-project.ts | 7 ++++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/packages/react/src/generators/host/host.spec.ts b/packages/react/src/generators/host/host.spec.ts index e42b6f2c05ee0a..45232b799d4769 100644 --- a/packages/react/src/generators/host/host.spec.ts +++ b/packages/react/src/generators/host/host.spec.ts @@ -3,6 +3,7 @@ import { readJson } from '@nrwl/devkit'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; import hostGenerator from './host'; import { Linter } from '@nrwl/linter'; +import remote from '@nrwl/react/src/generators/remote/remote'; describe('hostGenerator', () => { let tree: Tree; @@ -28,6 +29,22 @@ describe('hostGenerator', () => { expect(tree.exists('apps/test/src/remotes.d.ts')); }); + it('should install @nrwl/web for the file-server executor', async () => { + const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); + await remote(tree, { + name: 'test', + devServerPort: 4201, + e2eTestRunner: 'cypress', + linter: Linter.EsLint, + skipFormat: false, + style: 'css', + unitTestRunner: 'jest', + }); + + const packageJson = readJson(tree, 'package.json'); + expect(packageJson.devDependencies['@nrwl/web']).toBeDefined(); + }); + it('should generate host files and configs for SSR', async () => { await hostGenerator(tree, { name: 'test', diff --git a/packages/react/src/generators/remote/remote.spec.ts b/packages/react/src/generators/remote/remote.spec.ts index 0370c9fc389144..fc282887356777 100644 --- a/packages/react/src/generators/remote/remote.spec.ts +++ b/packages/react/src/generators/remote/remote.spec.ts @@ -1,12 +1,27 @@ -import { readNxJson } from '@nrwl/devkit'; +import { readJson, readNxJson } from '@nrwl/devkit'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; import { Linter } from '@nrwl/linter'; import remote from './remote'; describe('remote generator', () => { - it('should not set the remote as the default project', async () => { + it('should install @nrwl/web for the file-server executor', async () => { const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); + await remote(tree, { + name: 'test', + devServerPort: 4201, + e2eTestRunner: 'cypress', + linter: Linter.EsLint, + skipFormat: false, + style: 'css', + unitTestRunner: 'jest', + }); + const packageJson = readJson(tree, 'package.json'); + expect(packageJson.devDependencies['@nrwl/web']).toBeDefined(); + }); + + it('should not set the remote as the default project', async () => { + const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); await remote(tree, { name: 'test', devServerPort: 4201, diff --git a/packages/react/src/rules/update-module-federation-project.ts b/packages/react/src/rules/update-module-federation-project.ts index 0448ee78d6c0a8..e622c10af033df 100644 --- a/packages/react/src/rules/update-module-federation-project.ts +++ b/packages/react/src/rules/update-module-federation-project.ts @@ -1,8 +1,11 @@ import { + addDependenciesToPackageJson, + GeneratorCallback, readProjectConfiguration, Tree, updateProjectConfiguration, } from '@nrwl/devkit'; +import { nxVersion } from '../utils/versions'; export function updateModuleFederationProject( host: Tree, @@ -11,7 +14,7 @@ export function updateModuleFederationProject( appProjectRoot: string; devServerPort?: number; } -) { +): GeneratorCallback { const projectConfig = readProjectConfiguration(host, options.projectName); projectConfig.targets.build.options = { @@ -48,4 +51,6 @@ export function updateModuleFederationProject( }; updateProjectConfiguration(host, options.projectName, projectConfig); + + return addDependenciesToPackageJson(host, {}, { '@nrwl/web': nxVersion }); }