-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(module-federation): ensure targetDefaults for module federation e…
…xecutors are setup correctly (#22282)
- Loading branch information
Showing
10 changed files
with
428 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
packages/angular/src/migrations/update-18-1-1/fix-target-defaults-inputs.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import fixTargetDefaultInputs from './fix-target-defaults-inputs'; | ||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; | ||
import { addProjectConfiguration, readNxJson, updateNxJson } from '@nx/devkit'; | ||
|
||
describe('fixTargetDefaultsInputs', () => { | ||
it('should add the executor and input when it does not exist', async () => { | ||
// ARRANGE | ||
const tree = createTreeWithEmptyWorkspace(); | ||
addProjectConfiguration(tree, 'test', { | ||
root: '', | ||
targets: { | ||
build: { | ||
executor: '@nx/angular:webpack-browser', | ||
}, | ||
}, | ||
}); | ||
|
||
tree.write('module-federation.config.ts', ''); | ||
|
||
// ACT | ||
await fixTargetDefaultInputs(tree); | ||
|
||
// ASSERT | ||
const nxJson = readNxJson(tree); | ||
expect(nxJson.targetDefaults).toMatchInlineSnapshot(` | ||
{ | ||
"@nx/angular:webpack-browser": { | ||
"inputs": [ | ||
"production", | ||
"^production", | ||
{ | ||
"env": "NX_MF_DEV_SERVER_STATIC_REMOTES", | ||
}, | ||
], | ||
}, | ||
"build": { | ||
"cache": true, | ||
}, | ||
"lint": { | ||
"cache": true, | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
it('should not add the executor and input when no project uses it', async () => { | ||
// ARRANGE | ||
const tree = createTreeWithEmptyWorkspace(); | ||
addProjectConfiguration(tree, 'test', { | ||
root: '', | ||
targets: { | ||
build: { | ||
executor: '@nx/angular:module-federation-dev-server', | ||
}, | ||
}, | ||
}); | ||
|
||
// ACT | ||
await fixTargetDefaultInputs(tree); | ||
|
||
// ASSERT | ||
const nxJson = readNxJson(tree); | ||
expect(nxJson.targetDefaults).toMatchInlineSnapshot(` | ||
{ | ||
"build": { | ||
"cache": true, | ||
}, | ||
"lint": { | ||
"cache": true, | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
it('should update the executor and input target default when it already exists', async () => { | ||
// ARRANGE | ||
const tree = createTreeWithEmptyWorkspace(); | ||
addProjectConfiguration(tree, 'test', { | ||
root: '', | ||
targets: { | ||
build: { | ||
executor: '@nx/angular:webpack-browser', | ||
}, | ||
}, | ||
}); | ||
tree.write('module-federation.config.ts', ''); | ||
|
||
let nxJson = readNxJson(tree); | ||
nxJson = { | ||
...nxJson, | ||
targetDefaults: { | ||
...nxJson.targetDefaults, | ||
['@nx/angular:webpack-browser']: { | ||
inputs: ['^build'], | ||
}, | ||
}, | ||
}; | ||
|
||
updateNxJson(tree, nxJson); | ||
|
||
// ACT | ||
await fixTargetDefaultInputs(tree); | ||
|
||
// ASSERT | ||
nxJson = readNxJson(tree); | ||
expect(nxJson.targetDefaults).toMatchInlineSnapshot(` | ||
{ | ||
"@nx/angular:webpack-browser": { | ||
"inputs": [ | ||
"^build", | ||
"production", | ||
"^production", | ||
{ | ||
"env": "NX_MF_DEV_SERVER_STATIC_REMOTES", | ||
}, | ||
], | ||
}, | ||
"build": { | ||
"cache": true, | ||
}, | ||
"lint": { | ||
"cache": true, | ||
}, | ||
} | ||
`); | ||
}); | ||
}); |
75 changes: 75 additions & 0 deletions
75
packages/angular/src/migrations/update-18-1-1/fix-target-defaults-inputs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { | ||
getProjects, | ||
type Tree, | ||
type ProjectConfiguration, | ||
joinPathFragments, | ||
formatFiles, | ||
readNxJson, | ||
updateNxJson, | ||
} from '@nx/devkit'; | ||
|
||
export default async function (tree: Tree) { | ||
if (!isWebpackBrowserUsed(tree)) { | ||
return; | ||
} | ||
ensureTargetDefaultsContainProductionInputs(tree); | ||
|
||
await formatFiles(tree); | ||
} | ||
|
||
function ensureTargetDefaultsContainProductionInputs(tree: Tree) { | ||
const nxJson = readNxJson(tree); | ||
const webpackExecutor = '@nx/angular:webpack-browser'; | ||
const mfEnvVar = 'NX_MF_DEV_SERVER_STATIC_REMOTES'; | ||
|
||
nxJson.targetDefaults[webpackExecutor] ??= {}; | ||
|
||
nxJson.targetDefaults[webpackExecutor].inputs ??= [ | ||
'production', | ||
'^production', | ||
{ env: mfEnvVar }, | ||
]; | ||
|
||
if (!nxJson.targetDefaults[webpackExecutor].inputs.includes('production')) { | ||
nxJson.targetDefaults[webpackExecutor].inputs.push('production'); | ||
} | ||
|
||
if (!nxJson.targetDefaults[webpackExecutor].inputs.includes('^production')) { | ||
nxJson.targetDefaults[webpackExecutor].inputs.push('^production'); | ||
} | ||
|
||
let mfEnvVarExists = false; | ||
for (const input of nxJson.targetDefaults[webpackExecutor].inputs) { | ||
if (typeof input === 'object' && input['env'] === mfEnvVar) { | ||
mfEnvVarExists = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!mfEnvVarExists) { | ||
nxJson.targetDefaults[webpackExecutor].inputs.push({ env: mfEnvVar }); | ||
} | ||
|
||
updateNxJson(tree, nxJson); | ||
} | ||
|
||
function isWebpackBrowserUsed(tree: Tree) { | ||
const projects = getProjects(tree); | ||
for (const project of projects.values()) { | ||
const targets = project.targets || {}; | ||
for (const [_, target] of Object.entries(targets)) { | ||
if ( | ||
target.executor === '@nx/angular:webpack-browser' && | ||
(tree.exists( | ||
joinPathFragments(project.root, 'module-federation.config.ts') | ||
) || | ||
tree.exists( | ||
joinPathFragments(project.root, 'module-federation.config.js') | ||
)) | ||
) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
packages/react/src/migrations/update-18-1-1/fix-target-defaults-inputs.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import fixTargetDefaultsInputs from './fix-target-defaults-inputs'; | ||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; | ||
import { addProjectConfiguration, readNxJson, updateNxJson } from '@nx/devkit'; | ||
|
||
describe('fixTargetDefaultsInputs', () => { | ||
it('should add the executor and input when it does not exist', async () => { | ||
// ARRANGE | ||
const tree = createTreeWithEmptyWorkspace(); | ||
addProjectConfiguration(tree, 'test', { | ||
root: '', | ||
targets: { | ||
build: { | ||
executor: '@nx/webpack:webpack', | ||
}, | ||
}, | ||
}); | ||
tree.write('module-federation.config.ts', ''); | ||
|
||
// ACT | ||
await fixTargetDefaultsInputs(tree); | ||
|
||
// ASSERT | ||
const nxJson = readNxJson(tree); | ||
expect(nxJson.targetDefaults).toMatchInlineSnapshot(` | ||
{ | ||
"@nx/webpack:webpack": { | ||
"inputs": [ | ||
"production", | ||
"^production", | ||
{ | ||
"env": "NX_MF_DEV_SERVER_STATIC_REMOTES", | ||
}, | ||
], | ||
}, | ||
"build": { | ||
"cache": true, | ||
}, | ||
"lint": { | ||
"cache": true, | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
it('should not add the executor and input when no project uses it', async () => { | ||
// ARRANGE | ||
const tree = createTreeWithEmptyWorkspace(); | ||
addProjectConfiguration(tree, 'test', { | ||
root: '', | ||
targets: { | ||
build: { | ||
executor: '@nx/angular:module-federation-dev-server', | ||
}, | ||
}, | ||
}); | ||
|
||
// ACT | ||
await fixTargetDefaultsInputs(tree); | ||
|
||
// ASSERT | ||
const nxJson = readNxJson(tree); | ||
expect(nxJson.targetDefaults).toMatchInlineSnapshot(` | ||
{ | ||
"build": { | ||
"cache": true, | ||
}, | ||
"lint": { | ||
"cache": true, | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
it('should update the executor and input target default when it already exists', async () => { | ||
// ARRANGE | ||
const tree = createTreeWithEmptyWorkspace(); | ||
addProjectConfiguration(tree, 'test', { | ||
root: '', | ||
targets: { | ||
build: { | ||
executor: '@nx/webpack:webpack', | ||
}, | ||
}, | ||
}); | ||
|
||
tree.write('module-federation.config.ts', ''); | ||
|
||
let nxJson = readNxJson(tree); | ||
nxJson = { | ||
...nxJson, | ||
targetDefaults: { | ||
...nxJson.targetDefaults, | ||
['@nx/webpack:webpack']: { | ||
inputs: ['^build'], | ||
}, | ||
}, | ||
}; | ||
|
||
updateNxJson(tree, nxJson); | ||
|
||
// ACT | ||
await fixTargetDefaultsInputs(tree); | ||
|
||
// ASSERT | ||
nxJson = readNxJson(tree); | ||
expect(nxJson.targetDefaults).toMatchInlineSnapshot(` | ||
{ | ||
"@nx/webpack:webpack": { | ||
"inputs": [ | ||
"^build", | ||
"production", | ||
"^production", | ||
{ | ||
"env": "NX_MF_DEV_SERVER_STATIC_REMOTES", | ||
}, | ||
], | ||
}, | ||
"build": { | ||
"cache": true, | ||
}, | ||
"lint": { | ||
"cache": true, | ||
}, | ||
} | ||
`); | ||
}); | ||
}); |
Oops, something went wrong.