Skip to content

Commit bf90604

Browse files
authored
fix(module-federation): nested projects should be ordered first when reading from tsconfig paths #20284 (#23212)
<!-- 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` --> ## Current Behavior <!-- This is the behavior we have today --> When using nested projects (note: not secondary entry points), the `shareWorkspaceLibraries` needs to order the nested projects first for webpack to resolve the import path aliases correctly. ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> Ensure nested projects are ordered first when reading tsconfig path aliases ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #20284
1 parent e8041bb commit bf90604

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

packages/webpack/src/utils/module-federation/share.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,30 @@ describe('MF Share Utils', () => {
5858
});
5959
});
6060

61+
it('should order nested projects first', () => {
62+
// ARRANGE
63+
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
64+
jest.spyOn(tsUtils, 'readTsPathMappings').mockReturnValue({
65+
'@myorg/shared': ['/libs/shared/src/index.ts'],
66+
'@myorg/shared/components': ['/libs/shared/components/src/index.ts'],
67+
});
68+
69+
// ACT
70+
const sharedLibraries = shareWorkspaceLibraries([
71+
{ name: 'shared', root: 'libs/shared', importKey: '@myorg/shared' },
72+
{
73+
name: 'shared-components',
74+
root: 'libs/shared/components',
75+
importKey: '@myorg/shared/components',
76+
},
77+
]);
78+
79+
// ASSERT
80+
expect(Object.keys(sharedLibraries.getAliases())[0]).toEqual(
81+
'@myorg/shared/components'
82+
);
83+
});
84+
6185
it('should handle path mappings with wildcards correctly in non-buildable libraries', () => {
6286
// ARRANGE
6387
jest.spyOn(fs, 'existsSync').mockImplementation((file: string) => true);

packages/webpack/src/utils/module-federation/share.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,18 @@ export function shareWorkspaceLibraries(
4141
return getEmptySharedLibrariesConfig();
4242
}
4343

44+
// Nested projects must come first, sort them as such
45+
const sortedTsConfigPathAliases = {};
46+
Object.keys(tsconfigPathAliases)
47+
.sort((a, b) => {
48+
return b.split('/').length - a.split('/').length;
49+
})
50+
.forEach(
51+
(key) => (sortedTsConfigPathAliases[key] = tsconfigPathAliases[key])
52+
);
53+
4454
const pathMappings: { name: string; path: string }[] = [];
45-
for (const [key, paths] of Object.entries(tsconfigPathAliases)) {
55+
for (const [key, paths] of Object.entries(sortedTsConfigPathAliases)) {
4656
const library = workspaceLibs.find((lib) => lib.importKey === key);
4757
if (!library) {
4858
continue;
@@ -52,7 +62,7 @@ export function shareWorkspaceLibraries(
5262
// It will do nothing for React Projects
5363
collectWorkspaceLibrarySecondaryEntryPoints(
5464
library,
55-
tsconfigPathAliases
65+
sortedTsConfigPathAliases
5666
).forEach(({ name, path }) =>
5767
pathMappings.push({
5868
name,

0 commit comments

Comments
 (0)