-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 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)
- Loading branch information
1 parent
0c4857d
commit b621454
Showing
10 changed files
with
466 additions
and
0 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
154 changes: 154 additions & 0 deletions
154
packages/angular/src/migrations/update-19-6-1/ensure-depends-on-for-mf.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,154 @@ | ||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; | ||
import { | ||
addProjectConfiguration, | ||
readNxJson, | ||
updateNxJson, | ||
type Tree, | ||
} from '@nx/devkit'; | ||
import ensureDependsOnForMf from './ensure-depends-on-for-mf'; | ||
|
||
describe('ensure-depends-on-for-mf', () => { | ||
it('should ensure targetDefault is added correctly if not exists', async () => { | ||
// ARRANGE | ||
const tree = createTreeWithEmptyWorkspace(); | ||
const nxJson = readNxJson(tree); | ||
nxJson.targetDefaults ??= {}; | ||
nxJson.targetDefaults['@nx/angular:webpack-browser'] = { | ||
inputs: ['production', '^production'], | ||
}; | ||
updateNxJson(tree, nxJson); | ||
addProject(tree); | ||
|
||
// ACT | ||
await ensureDependsOnForMf(tree); | ||
|
||
// ASSERT | ||
expect(readNxJson(tree).targetDefaults['@nx/angular:webpack-browser']) | ||
.toMatchInlineSnapshot(` | ||
{ | ||
"dependsOn": [ | ||
"^build", | ||
], | ||
"inputs": [ | ||
"production", | ||
"^production", | ||
{ | ||
"env": "NX_MF_DEV_REMOTES", | ||
}, | ||
], | ||
} | ||
`); | ||
}); | ||
|
||
it('should ensure targetDefault is added correctly if there are no targetDefaults', async () => { | ||
// ARRANGE | ||
const tree = createTreeWithEmptyWorkspace(); | ||
const nxJson = readNxJson(tree); | ||
nxJson.targetDefaults = {}; | ||
updateNxJson(tree, nxJson); | ||
addProject(tree); | ||
|
||
// ACT | ||
await ensureDependsOnForMf(tree); | ||
|
||
// ASSERT | ||
expect(readNxJson(tree).targetDefaults['@nx/angular:webpack-browser']) | ||
.toMatchInlineSnapshot(` | ||
{ | ||
"cache": true, | ||
"dependsOn": [ | ||
"^build", | ||
], | ||
"inputs": [ | ||
"production", | ||
"^production", | ||
{ | ||
"env": "NX_MF_DEV_REMOTES", | ||
}, | ||
], | ||
} | ||
`); | ||
}); | ||
|
||
it('should ensure targetDefault is updated correctly if missing ^build', async () => { | ||
// ARRANGE | ||
const tree = createTreeWithEmptyWorkspace(); | ||
const nxJson = readNxJson(tree); | ||
nxJson.targetDefaults ??= {}; | ||
nxJson.targetDefaults['@nx/angular:webpack-browser'] = { | ||
inputs: ['production', '^production'], | ||
dependsOn: ['some-task'], | ||
}; | ||
updateNxJson(tree, nxJson); | ||
addProject(tree); | ||
|
||
// ACT | ||
await ensureDependsOnForMf(tree); | ||
|
||
// ASSERT | ||
expect(readNxJson(tree).targetDefaults['@nx/angular:webpack-browser']) | ||
.toMatchInlineSnapshot(` | ||
{ | ||
"dependsOn": [ | ||
"some-task", | ||
"^build", | ||
], | ||
"inputs": [ | ||
"production", | ||
"^production", | ||
{ | ||
"env": "NX_MF_DEV_REMOTES", | ||
}, | ||
], | ||
} | ||
`); | ||
}); | ||
|
||
it('should do nothing if targetDefault is set up correctly', async () => { | ||
// ARRANGE | ||
const tree = createTreeWithEmptyWorkspace(); | ||
const nxJson = readNxJson(tree); | ||
nxJson.targetDefaults ??= {}; | ||
nxJson.targetDefaults['@nx/angular:webpack-browser'] = { | ||
inputs: ['production', '^production', { env: 'NX_MF_DEV_REMOTES' }], | ||
dependsOn: ['^build'], | ||
}; | ||
updateNxJson(tree, nxJson); | ||
addProject(tree); | ||
|
||
// ACT | ||
await ensureDependsOnForMf(tree); | ||
|
||
// ASSERT | ||
expect(readNxJson(tree).targetDefaults['@nx/angular:webpack-browser']) | ||
.toMatchInlineSnapshot(` | ||
{ | ||
"dependsOn": [ | ||
"^build", | ||
], | ||
"inputs": [ | ||
"production", | ||
"^production", | ||
{ | ||
"env": "NX_MF_DEV_REMOTES", | ||
}, | ||
], | ||
} | ||
`); | ||
}); | ||
}); | ||
|
||
function addProject(tree: Tree) { | ||
tree.write('app/webpack.config.ts', `withModuleFederation`); | ||
addProjectConfiguration(tree, 'app', { | ||
name: 'app', | ||
root: 'app', | ||
projectType: 'application', | ||
targets: { | ||
build: { | ||
executor: '@nx/angular:webpack-browser', | ||
options: { webpackConfig: 'app/webpack.config.ts' }, | ||
}, | ||
}, | ||
}); | ||
} |
68 changes: 68 additions & 0 deletions
68
packages/angular/src/migrations/update-19-6-1/ensure-depends-on-for-mf.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,68 @@ | ||
import { formatFiles, readNxJson, type Tree, updateNxJson } from '@nx/devkit'; | ||
import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils'; | ||
import type { WebpackExecutorOptions } from '@nx/webpack'; | ||
|
||
export default async function (tree: Tree) { | ||
let usesModuleFederation = false; | ||
forEachExecutorOptions<WebpackExecutorOptions>( | ||
tree, | ||
'@nx/angular:webpack-browser', | ||
(options, projectName, targetName) => { | ||
const webpackConfig: string = options.webpackConfig; | ||
if (!webpackConfig) { | ||
return; | ||
} | ||
|
||
const webpackContents = tree.read(webpackConfig, 'utf-8'); | ||
if ( | ||
['withModuleFederation', 'withModuleFederationForSSR'].some((p) => | ||
webpackContents.includes(p) | ||
) | ||
) { | ||
usesModuleFederation = true; | ||
} | ||
} | ||
); | ||
|
||
if (!usesModuleFederation) { | ||
return; | ||
} | ||
|
||
const nxJson = readNxJson(tree); | ||
const nxMFDevRemotesEnvVar = 'NX_MF_DEV_REMOTES'; | ||
if ( | ||
!nxJson.targetDefaults || | ||
!nxJson.targetDefaults?.['@nx/angular:webpack-browser'] | ||
) { | ||
nxJson.targetDefaults ??= {}; | ||
nxJson.targetDefaults['@nx/angular:webpack-browser'] = { | ||
cache: true, | ||
inputs: ['production', '^production', { env: nxMFDevRemotesEnvVar }], | ||
dependsOn: ['^build'], | ||
}; | ||
} else { | ||
nxJson.targetDefaults['@nx/angular:webpack-browser'].dependsOn ??= []; | ||
if ( | ||
!nxJson.targetDefaults['@nx/angular:webpack-browser'].dependsOn.includes( | ||
'^build' | ||
) | ||
) { | ||
nxJson.targetDefaults['@nx/angular:webpack-browser'].dependsOn.push( | ||
'^build' | ||
); | ||
} | ||
|
||
nxJson.targetDefaults['@nx/angular:webpack-browser'].inputs ??= []; | ||
if ( | ||
!nxJson.targetDefaults['@nx/angular:webpack-browser'].inputs.find((i) => | ||
typeof i === 'string' ? false : i['env'] === nxMFDevRemotesEnvVar | ||
) | ||
) { | ||
nxJson.targetDefaults['@nx/angular:webpack-browser'].inputs.push({ | ||
env: nxMFDevRemotesEnvVar, | ||
}); | ||
} | ||
} | ||
updateNxJson(tree, nxJson); | ||
await formatFiles(tree); | ||
} |
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
Oops, something went wrong.