Publish Module Federation packages as ESM + CJS dual format using typeless modules #4012
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
This PR converts all Module Federation packages to publish as both ESM and CommonJS formats using a typeless modules approach, addressing compatibility issues across different JavaScript environments and bundlers.
Problem
Many Module Federation packages were only publishing in a single format (either ESM or CJS), causing compatibility issues for users in different environments:
@nx/js:tsc
compiler and only generated single format outputsimport
andrequire
Solution
Build System Changes
Converted 7 packages from
@nx/js:tsc
to@nx/rollup:rollup
with dual format builds:@module-federation/enhanced
@module-federation/nextjs-mf
@module-federation/node
@module-federation/typescript
@module-federation/storybook-addon
@module-federation/cli
(added ESM support)@module-federation/utilities
(fixed extensions)Fixed existing packages with proper dual format exports:
@module-federation/dts-plugin
(updated to use.cjs
extensions)Typeless Modules Approach
All packages use a typeless modules approach for maximum compatibility:
"type": "module"
declarations to avoid CJS TypeScript implementation issuesmain
/module
fields pointing to.cjs.cjs
/.esm.js
filesexports
field with separate import/require mappings:externalLiveBindings: false
for CJS builds (performance optimization)Rollup Configuration
Created standardized rollup configs that:
.esm.js
and.cjs.cjs
file extensionsBenefits
The typeless modules approach provides:
.js
files as ESMimport
orrequire
syntaxPackages Updated
Converted to dual format:
Already supported dual format (verified working):
Testing
All package.json files have been validated for syntax correctness. The changes ensure maximum compatibility across:
Fixes #3853.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.