module-federation / core

Module Federation is a concept that allows developers to share code and resources across multiple JavaScript applications
https://module-federation.io/
MIT License
1.55k stars 242 forks source link

Question for shared function instance between host app and remote app. #3278

Open jwq403926021 opened 4 hours ago

jwq403926021 commented 4 hours ago

Describe the bug

Why is the mitt() instance not singleton ??

Here is the shared function instance code. This code is shared by host app. image

Here is the host federation config. image

Here is the usage from host app. Using the instance directly. image

Here is the usage from remote app. Using the instance by shared module. image

Reproduction

reproduction

Used Package Manager

pnpm

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (16) x64 12th Gen Intel(R) Core(TM) i5-1240P
    Memory: 886.59 MB / 15.68 GB
  Binaries:
    Node: 18.20.4 - C:\Program Files\nodejs\node.EXE
    npm: 10.7.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.14.2 - C:\Program Files\nodejs\pnpm.CMD
  Browsers:
    Edge: Chromium (130.0.2849.56)
    Internet Explorer: 11.0.22621.3527

Validations

ScriptedAlchemy commented 49 minutes ago

exposed modules are not guarenteed to be singleton, the host and remote have seperate runtime containers and module graphs. only mitt would be singleton, the exposed item would not be in this case, youd need a runtime chunk single but that may cause other issues unless you have an additional ContainerPlugin with runtime: false set on it so that you have one for when the hosts own remote is loaded into the page, and another for if the host is not the same app as a remote being loaded. the exposed thing is not singleton, but its import of mitt is.

if you shared the thing you expose, it would probbably work