Skip to content

Conversation

@y-okt
Copy link

@y-okt y-okt commented Nov 22, 2025

Description

When using Module Federation with both remotes AND shared modules, .mjs files that import a default export from a shared module receive the module namespace object instead of the default export value.

See #4238 more in detail.

The solution to this problem is:

  1. object/function default exports -> Unwrap the default and make it the primary export, with named exports attached as properties and a circular .default reference
  2. primitive default exports -> Keep the original ESM namespace object to preserve named exports

Testing with the reproduction codes,
スクリーンショット 2025-11-23 2 28 43

Related Issue

#4238

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • [] I have updated the documentation.

@changeset-bot
Copy link

changeset-bot bot commented Nov 22, 2025

🦋 Changeset detected

Latest commit: eb21d4c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 37 packages
Name Type
@module-federation/webpack-bundler-runtime Patch
@module-federation/enhanced Patch
@module-federation/nextjs-mf Patch
@module-federation/runtime-tools Patch
@module-federation/rspack Patch
@module-federation/inject-external-runtime-core-plugin Patch
@module-federation/modern-js Patch
@module-federation/node Patch
@module-federation/rsbuild-plugin Patch
@module-federation/rspress-plugin Patch
@module-federation/storybook-addon Patch
@module-federation/modernjsapp Patch
remote5 Patch
remote6 Patch
website-new Patch
@module-federation/runtime Patch
@module-federation/sdk Patch
@module-federation/managers Patch
@module-federation/manifest Patch
@module-federation/dts-plugin Patch
@module-federation/third-party-dts-extractor Patch
@module-federation/devtools Patch
@module-federation/bridge-react Patch
@module-federation/bridge-vue3 Patch
@module-federation/bridge-shared Patch
@module-federation/bridge-react-webpack-plugin Patch
@module-federation/retry-plugin Patch
@module-federation/data-prefetch Patch
@module-federation/error-codes Patch
@module-federation/runtime-core Patch
create-module-federation Patch
@module-federation/cli Patch
@module-federation/metro Patch
@module-federation/metro-plugin-rnef Patch
@module-federation/metro-plugin-rnc-cli Patch
@module-federation/esbuild Patch
@module-federation/utilities Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify
Copy link

netlify bot commented Nov 22, 2025

Deploy Preview for module-federation-docs ready!

Name Link
🔨 Latest commit eb21d4c
🔍 Latest deploy log https://app.netlify.com/projects/module-federation-docs/deploys/6921f05fce95c60008b4e96a
😎 Deploy Preview https://deploy-preview-4239--module-federation-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 65 to 69
shareInfo?.shareConfig?.layer &&
result &&
typeof result === 'object'
moduleExports &&
typeof moduleExports === 'object'
) {
try {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Skip layer tagging for function defaults after unwrapping

When a shared module is configured with shareConfig.layer and its default export is a function/object, the new unwrapping logic sets moduleExports to that function but the layer attachment block now requires typeof moduleExports === 'object', so the layer property is never added. Previously the layer metadata was applied to the namespace object and was available to consumers. With this change, layered shares backed by a function default lose their layer tag entirely, breaking layer-aware consumers for exactly the remotes/shared combination this commit targets.

Useful? React with 👍 / 👎.

Comment on lines 86 to 90
if (
shareInfo?.shareConfig?.layer &&
result &&
typeof result === 'object'
moduleExports &&
typeof moduleExports === 'object'
) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Eager consumes drop layer metadata when default export is a function

The eager path mirrors the same typeof moduleExports === 'object' guard before assigning shareConfig.layer. After unwrapping an ESM namespace to its default function, this condition is false, so layered shares no longer receive the layer property. Earlier versions always set the layer on the returned namespace object, so this is a regression for any layered shared module whose default export is a function/object.

Useful? React with 👍 / 👎.

@y-okt y-okt force-pushed the fix-resolve-mjs-on-federation-remote branch from a7b3521 to ef448c7 Compare November 22, 2025 17:15
… mjs files

Fix ESM interop issue where .mjs files received module namespace objects
instead of default exports when using Module Federation with remotes.

The runtime now intelligently unwraps ESM namespace objects for
object/function default exports while preserving the namespace for
primitive defaults to maintain named export accessibility.
@y-okt y-okt force-pushed the fix-resolve-mjs-on-federation-remote branch from ef448c7 to eb21d4c Compare November 22, 2025 17:18
@y-okt y-okt changed the title fix(webpack-bundler-runtime): fix the bug in which mjs is wrongly handled fix(webpack-bundler-runtime): fix the bug that mjs is wrongly handled Nov 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant