Skip to content

Commit b621454

Browse files
Coly010FrozenPandaz
authored andcommitted
fix(module-federation): ensure target defaults are set correctly #27448 (#27472)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> Target Defaults set up for the Module Federation builds is incorrect or missing. ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> Target Defaults for Module Federation builds is set up correctly ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #27448 (cherry picked from commit 396a5de)
1 parent 0c4857d commit b621454

File tree

10 files changed

+466
-0
lines changed

10 files changed

+466
-0
lines changed

packages/angular/migrations.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,12 @@
428428
},
429429
"description": "Update the @angular/cli package version to ~18.2.0.",
430430
"factory": "./src/migrations/update-19-6-0/update-angular-cli"
431+
},
432+
"update-19-6-1-ensure-module-federation-target-defaults": {
433+
"cli": "nx",
434+
"version": "19.6.1-beta.0",
435+
"description": "Ensure Target Defaults are set correctly for Module Federation.",
436+
"factory": "./src/migrations/update-19-6-1/ensure-depends-on-for-mf"
431437
}
432438
},
433439
"packageJsonUpdates": {

packages/angular/src/generators/utils/add-mf-env-to-inputs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export function addMfEnvToTargetDefaultInputs(tree: Tree) {
1111
'production',
1212
'^production',
1313
];
14+
nxJson.targetDefaults[webpackExecutor].dependsOn ??= ['^build'];
1415

1516
let mfEnvVarExists = false;
1617
for (const input of nxJson.targetDefaults[webpackExecutor].inputs) {

packages/angular/src/migrations/update-18-0-0/add-mf-env-var-to-target-defaults.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ describe('addMfEnvVarToTargetDefaults', () => {
2525
expect(nxJson.targetDefaults).toMatchInlineSnapshot(`
2626
{
2727
"@nx/angular:webpack-browser": {
28+
"dependsOn": [
29+
"^build",
30+
],
2831
"inputs": [
2932
"production",
3033
"^production",
@@ -106,6 +109,9 @@ describe('addMfEnvVarToTargetDefaults', () => {
106109
expect(nxJson.targetDefaults).toMatchInlineSnapshot(`
107110
{
108111
"@nx/angular:webpack-browser": {
112+
"dependsOn": [
113+
"^build",
114+
],
109115
"inputs": [
110116
"^build",
111117
{
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
2+
import {
3+
addProjectConfiguration,
4+
readNxJson,
5+
updateNxJson,
6+
type Tree,
7+
} from '@nx/devkit';
8+
import ensureDependsOnForMf from './ensure-depends-on-for-mf';
9+
10+
describe('ensure-depends-on-for-mf', () => {
11+
it('should ensure targetDefault is added correctly if not exists', async () => {
12+
// ARRANGE
13+
const tree = createTreeWithEmptyWorkspace();
14+
const nxJson = readNxJson(tree);
15+
nxJson.targetDefaults ??= {};
16+
nxJson.targetDefaults['@nx/angular:webpack-browser'] = {
17+
inputs: ['production', '^production'],
18+
};
19+
updateNxJson(tree, nxJson);
20+
addProject(tree);
21+
22+
// ACT
23+
await ensureDependsOnForMf(tree);
24+
25+
// ASSERT
26+
expect(readNxJson(tree).targetDefaults['@nx/angular:webpack-browser'])
27+
.toMatchInlineSnapshot(`
28+
{
29+
"dependsOn": [
30+
"^build",
31+
],
32+
"inputs": [
33+
"production",
34+
"^production",
35+
{
36+
"env": "NX_MF_DEV_REMOTES",
37+
},
38+
],
39+
}
40+
`);
41+
});
42+
43+
it('should ensure targetDefault is added correctly if there are no targetDefaults', async () => {
44+
// ARRANGE
45+
const tree = createTreeWithEmptyWorkspace();
46+
const nxJson = readNxJson(tree);
47+
nxJson.targetDefaults = {};
48+
updateNxJson(tree, nxJson);
49+
addProject(tree);
50+
51+
// ACT
52+
await ensureDependsOnForMf(tree);
53+
54+
// ASSERT
55+
expect(readNxJson(tree).targetDefaults['@nx/angular:webpack-browser'])
56+
.toMatchInlineSnapshot(`
57+
{
58+
"cache": true,
59+
"dependsOn": [
60+
"^build",
61+
],
62+
"inputs": [
63+
"production",
64+
"^production",
65+
{
66+
"env": "NX_MF_DEV_REMOTES",
67+
},
68+
],
69+
}
70+
`);
71+
});
72+
73+
it('should ensure targetDefault is updated correctly if missing ^build', async () => {
74+
// ARRANGE
75+
const tree = createTreeWithEmptyWorkspace();
76+
const nxJson = readNxJson(tree);
77+
nxJson.targetDefaults ??= {};
78+
nxJson.targetDefaults['@nx/angular:webpack-browser'] = {
79+
inputs: ['production', '^production'],
80+
dependsOn: ['some-task'],
81+
};
82+
updateNxJson(tree, nxJson);
83+
addProject(tree);
84+
85+
// ACT
86+
await ensureDependsOnForMf(tree);
87+
88+
// ASSERT
89+
expect(readNxJson(tree).targetDefaults['@nx/angular:webpack-browser'])
90+
.toMatchInlineSnapshot(`
91+
{
92+
"dependsOn": [
93+
"some-task",
94+
"^build",
95+
],
96+
"inputs": [
97+
"production",
98+
"^production",
99+
{
100+
"env": "NX_MF_DEV_REMOTES",
101+
},
102+
],
103+
}
104+
`);
105+
});
106+
107+
it('should do nothing if targetDefault is set up correctly', async () => {
108+
// ARRANGE
109+
const tree = createTreeWithEmptyWorkspace();
110+
const nxJson = readNxJson(tree);
111+
nxJson.targetDefaults ??= {};
112+
nxJson.targetDefaults['@nx/angular:webpack-browser'] = {
113+
inputs: ['production', '^production', { env: 'NX_MF_DEV_REMOTES' }],
114+
dependsOn: ['^build'],
115+
};
116+
updateNxJson(tree, nxJson);
117+
addProject(tree);
118+
119+
// ACT
120+
await ensureDependsOnForMf(tree);
121+
122+
// ASSERT
123+
expect(readNxJson(tree).targetDefaults['@nx/angular:webpack-browser'])
124+
.toMatchInlineSnapshot(`
125+
{
126+
"dependsOn": [
127+
"^build",
128+
],
129+
"inputs": [
130+
"production",
131+
"^production",
132+
{
133+
"env": "NX_MF_DEV_REMOTES",
134+
},
135+
],
136+
}
137+
`);
138+
});
139+
});
140+
141+
function addProject(tree: Tree) {
142+
tree.write('app/webpack.config.ts', `withModuleFederation`);
143+
addProjectConfiguration(tree, 'app', {
144+
name: 'app',
145+
root: 'app',
146+
projectType: 'application',
147+
targets: {
148+
build: {
149+
executor: '@nx/angular:webpack-browser',
150+
options: { webpackConfig: 'app/webpack.config.ts' },
151+
},
152+
},
153+
});
154+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { formatFiles, readNxJson, type Tree, updateNxJson } from '@nx/devkit';
2+
import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils';
3+
import type { WebpackExecutorOptions } from '@nx/webpack';
4+
5+
export default async function (tree: Tree) {
6+
let usesModuleFederation = false;
7+
forEachExecutorOptions<WebpackExecutorOptions>(
8+
tree,
9+
'@nx/angular:webpack-browser',
10+
(options, projectName, targetName) => {
11+
const webpackConfig: string = options.webpackConfig;
12+
if (!webpackConfig) {
13+
return;
14+
}
15+
16+
const webpackContents = tree.read(webpackConfig, 'utf-8');
17+
if (
18+
['withModuleFederation', 'withModuleFederationForSSR'].some((p) =>
19+
webpackContents.includes(p)
20+
)
21+
) {
22+
usesModuleFederation = true;
23+
}
24+
}
25+
);
26+
27+
if (!usesModuleFederation) {
28+
return;
29+
}
30+
31+
const nxJson = readNxJson(tree);
32+
const nxMFDevRemotesEnvVar = 'NX_MF_DEV_REMOTES';
33+
if (
34+
!nxJson.targetDefaults ||
35+
!nxJson.targetDefaults?.['@nx/angular:webpack-browser']
36+
) {
37+
nxJson.targetDefaults ??= {};
38+
nxJson.targetDefaults['@nx/angular:webpack-browser'] = {
39+
cache: true,
40+
inputs: ['production', '^production', { env: nxMFDevRemotesEnvVar }],
41+
dependsOn: ['^build'],
42+
};
43+
} else {
44+
nxJson.targetDefaults['@nx/angular:webpack-browser'].dependsOn ??= [];
45+
if (
46+
!nxJson.targetDefaults['@nx/angular:webpack-browser'].dependsOn.includes(
47+
'^build'
48+
)
49+
) {
50+
nxJson.targetDefaults['@nx/angular:webpack-browser'].dependsOn.push(
51+
'^build'
52+
);
53+
}
54+
55+
nxJson.targetDefaults['@nx/angular:webpack-browser'].inputs ??= [];
56+
if (
57+
!nxJson.targetDefaults['@nx/angular:webpack-browser'].inputs.find((i) =>
58+
typeof i === 'string' ? false : i['env'] === nxMFDevRemotesEnvVar
59+
)
60+
) {
61+
nxJson.targetDefaults['@nx/angular:webpack-browser'].inputs.push({
62+
env: nxMFDevRemotesEnvVar,
63+
});
64+
}
65+
}
66+
updateNxJson(tree, nxJson);
67+
await formatFiles(tree);
68+
}

packages/react/migrations.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353
"version": "19.6.0-beta.4",
5454
"description": "Update the server file for Module Federation SSR port value to be the same as the 'serve' target port value.",
5555
"factory": "./src/migrations/update-19-6-0/update-ssr-server-port"
56+
},
57+
"update-19-6-1-ensure-module-federation-target-defaults": {
58+
"cli": "nx",
59+
"version": "19.6.1-beta.0",
60+
"description": "Ensure Target Defaults are set correctly for Module Federation.",
61+
"factory": "./src/migrations/update-19-6-1/ensure-depends-on-for-mf"
5662
}
5763
},
5864
"packageJsonUpdates": {

packages/react/src/migrations/update-18-0-0/add-mf-env-var-to-target-defaults.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ describe('addMfEnvVarToTargetDefaults', () => {
2424
expect(nxJson.targetDefaults).toMatchInlineSnapshot(`
2525
{
2626
"@nx/webpack:webpack": {
27+
"dependsOn": [
28+
"^build",
29+
],
2730
"inputs": [
2831
"production",
2932
"^production",
@@ -106,6 +109,9 @@ describe('addMfEnvVarToTargetDefaults', () => {
106109
expect(nxJson.targetDefaults).toMatchInlineSnapshot(`
107110
{
108111
"@nx/webpack:webpack": {
112+
"dependsOn": [
113+
"^build",
114+
],
109115
"inputs": [
110116
"^build",
111117
{

0 commit comments

Comments
 (0)