Skip to content

Commit

Permalink
fix(module-federation): collect secondary entry points from exports #…
Browse files Browse the repository at this point in the history
…26878 (#27999)

<!-- 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 -->
Some packages that use `exports` in the package.json may point to a
directory such as `esm/<entrypoint>`.
They would still be imported such as `mypackage/secondary-point`.
This needs to be shared still for Module Federation.

However, our current logic for finding secondary entry points for
packages looks at the sub dirs of the package and tries to match it with
the exports fiels.

Therefore if `mypackage/secondary-point` was set up such that

```
"./secondary-point": {
  default: "esm/secondary-point/index.mjs"
}
```

then it would never be found correctly, causing share problems.


## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
exports of secondary entrypoints should be shared correctly


## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #26878
  • Loading branch information
Coly010 authored Sep 19, 2024
1 parent f0f8de8 commit f767bab
Show file tree
Hide file tree
Showing 2 changed files with 256 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,33 @@ export function recursivelyCollectSecondaryEntryPointsFromDirectory(
}
}

function collectPackagesFromExports(
pkgName: string,
pkgVersion: string,
exports: any | undefined,
collectedPackages: {
name: string;
version: string;
}[]
): void {
for (const [relativeEntryPoint, exportOptions] of Object.entries(exports)) {
if (exportOptions?.['default']?.search(/\.(js|mjs|cjs)$/)) {
let entryPointName = joinPathFragments(pkgName, relativeEntryPoint);
if (entryPointName.endsWith('.json')) {
entryPointName = dirname(entryPointName);
}
if (entryPointName === '.') {
continue;
}
if (collectedPackages.find((p) => p.name === entryPointName)) {
continue;
}

collectedPackages.push({ name: entryPointName, version: pkgVersion });
}
}
}

export function collectPackageSecondaryEntryPoints(
pkgName: string,
pkgVersion: string,
Expand All @@ -130,6 +157,9 @@ export function collectPackageSecondaryEntryPoints(
}

const { exports } = packageJson;
if (exports) {
collectPackagesFromExports(pkgName, pkgVersion, exports, collectedPackages);
}
const subDirs = getNonNodeModulesSubDirs(pathToPackage);
recursivelyCollectSecondaryEntryPointsFromDirectory(
pkgName,
Expand Down
227 changes: 226 additions & 1 deletion packages/webpack/src/utils/module-federation/share.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,110 @@ describe('MF Share Utils', () => {
]);
// ASSERT
expect(packages).toEqual({
'@angular/common': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/common/http': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/common/http/testing': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/common/locales/*': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/common/locales/global/*': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/common/testing': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/common/upgrade': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/event-dispatch-contract.min.js': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/common': {
'@angular/core/primitives/event-dispatch': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/primitives/signals': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/rxjs-interop': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/schematics/*': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/testing': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
rxjs: {
requiredVersion: '~7.4.0',
singleton: true,
strictVersion: true,
},
'rxjs/ajax': {
requiredVersion: '~7.4.0',
singleton: true,
strictVersion: true,
},
'rxjs/fetch': {
requiredVersion: '~7.4.0',
singleton: true,
strictVersion: true,
},
'rxjs/internal/*': {
requiredVersion: '~7.4.0',
singleton: true,
strictVersion: true,
},
'rxjs/operators': {
requiredVersion: '~7.4.0',
singleton: true,
strictVersion: true,
},
'rxjs/testing': {
requiredVersion: '~7.4.0',
singleton: true,
strictVersion: true,
},
'rxjs/webSocket': {
requiredVersion: '~7.4.0',
singleton: true,
strictVersion: true,
},
});
});
Expand Down Expand Up @@ -218,6 +308,36 @@ describe('MF Share Utils', () => {
// ASSERT
expect(packages).toEqual({
'@angular/core': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/event-dispatch-contract.min.js': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/primitives/event-dispatch': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/primitives/signals': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/rxjs-interop': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/schematics/*': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/testing': {
singleton: true,
strictVersion: true,
requiredVersion: '~13.2.0',
Expand All @@ -227,12 +347,67 @@ describe('MF Share Utils', () => {
strictVersion: true,
requiredVersion: '~13.2.0',
},
'@angular/common/http': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/common/http/testing': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/common/locales/*': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/common/locales/global/*': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/common/testing': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/common/upgrade': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
rxjs: {
requiredVersion: '~7.4.0',
singleton: true,
strictVersion: true,
},
'rxjs/ajax': {
requiredVersion: '~7.4.0',
singleton: true,
strictVersion: true,
},
'rxjs/fetch': {
requiredVersion: '~7.4.0',
singleton: true,
strictVersion: true,
},
'rxjs/internal/*': {
requiredVersion: '~7.4.0',
singleton: true,
strictVersion: true,
},
'rxjs/operators': {
requiredVersion: '~7.4.0',
singleton: true,
strictVersion: true,
},
'rxjs/testing': {
requiredVersion: '~7.4.0',
singleton: true,
strictVersion: true,
},
'rxjs/webSocket': {
singleton: true,
strictVersion: true,
requiredVersion: '~7.4.0',
Expand Down Expand Up @@ -285,6 +460,31 @@ describe('MF Share Utils', () => {
strictVersion: true,
requiredVersion: '~13.2.0',
},
'@angular/core/event-dispatch-contract.min.js': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/primitives/event-dispatch': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/primitives/signals': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/rxjs-interop': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/schematics/*': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/testing': {
singleton: true,
strictVersion: true,
Expand Down Expand Up @@ -361,6 +561,31 @@ describe('MF Share Utils', () => {
strictVersion: true,
requiredVersion: '~13.2.0',
},
'@angular/core/event-dispatch-contract.min.js': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/primitives/event-dispatch': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/primitives/signals': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/rxjs-interop': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/schematics/*': {
requiredVersion: '~13.2.0',
singleton: true,
strictVersion: true,
},
'@angular/core/testing': {
singleton: true,
strictVersion: true,
Expand Down

0 comments on commit f767bab

Please sign in to comment.