nx icon indicating copy to clipboard operation
nx copied to clipboard

refactor(module-federation): consolidate getModuleFederationConfig implementations

Open adwait1290 opened this issue 2 weeks ago โ€ข 2 comments

Current Behavior

The getModuleFederationConfig function is implemented separately in 4 places with 70-80% duplicate code:

  • with-module-federation/webpack/utils.ts (161 lines)
  • with-module-federation/rspack/utils.ts (150 lines)
  • with-module-federation/angular/utils.ts - async version (~80 lines)
  • with-module-federation/angular/utils.ts - sync version (~80 lines)

Each implementation repeats the same core logic:

  1. Get project from graph
  2. Get and filter dependencies
  3. Share workspace libraries and npm packages
  4. Apply eager packages
  5. Map remotes

Expected Behavior

A single shared implementation with framework-specific configuration via a FrameworkConfig interface. Each bundler utility becomes a thin wrapper that provides its specific configuration.

Changes

File Before After Change
webpack/utils.ts 161 lines 54 lines -107 lines
rspack/utils.ts 150 lines 51 lines -99 lines
angular/utils.ts 273 lines 119 lines -154 lines
NEW module-federation-config.ts - 289 lines +289 lines

Net reduction: 41 lines, with significantly improved maintainability

New Shared Utility

Created packages/module-federation/src/utils/module-federation-config.ts with:

  • FrameworkConfig interface for bundler-specific customization
  • ModuleFederationConfigResult interface for type-safe return values
  • getModuleFederationConfigAsync() - for webpack/angular async configs
  • getModuleFederationConfigSync() - for rspack/angular sync configs
  • createDefaultRemoteUrlResolver() - shared remote URL generation
  • Caching for NX_MF_DEV_SERVER_STATIC_REMOTES env variable parsing (performance)

Benefits

  1. Single source of truth: Bug fixes and improvements only need to be made once
  2. Better maintainability: Framework-specific behavior is clearly separated via config
  3. Performance: Added caching for env variable parsing
  4. Type safety: New interfaces provide better IntelliSense and compile-time checks
  5. Backward compatible: All existing exports and behavior preserved

Related Issue(s)

N/A - This is a refactoring for improved code maintainability and performance.

Merge Dependencies

Must be merged AFTER: #33734


adwait1290 avatar Dec 08 '25 03:12 adwait1290

Deploy request for nx-docs pending review.

Visit the deploys page to approve it

Name Link
Latest commit 9f32ad4f21af58cc403bd70fa2c02eb92d134f6a

netlify[bot] avatar Dec 08 '25 03:12 netlify[bot]

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Updated (UTC)
nx-dev Ready Ready Preview Dec 8, 2025 3:45am

vercel[bot] avatar Dec 08 '25 03:12 vercel[bot]

View your CI Pipeline Execution โ†— for commit 9f32ad4f21af58cc403bd70fa2c02eb92d134f6a

Command Status Duration Result
nx affected --targets=lint,test,test-kt,build,e... โœ… Succeeded 23m 6s View โ†—
nx run-many -t check-imports check-lock-files c... โœ… Succeeded 2m 38s View โ†—
nx-cloud record -- nx-cloud conformance:check โœ… Succeeded 11s View โ†—
nx-cloud record -- nx format:check โœ… Succeeded 2s View โ†—
nx-cloud record -- nx sync:check โœ… Succeeded <1s View โ†—

โ˜๏ธ Nx Cloud last updated this comment at 2025-12-12 12:47:25 UTC

nx-cloud[bot] avatar Dec 12 '25 10:12 nx-cloud[bot]

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

github-actions[bot] avatar Dec 18 '25 00:12 github-actions[bot]