originjs / vite-plugin-federation

Module Federation for vite & rollup
Other
2.3k stars 236 forks source link

Zustand in remote app causes issues #478

Open wijionejs opened 1 year ago

wijionejs commented 1 year ago

Versions

Reproduction

There is an issue if you install zustand as a state manager in a remote app.

I did some research and noticed that zustand has use-sync-external-store as a dependency (here), which is bundled in cjs and in its turn imports react as a commonjs module: require("react").

It causes two issues:

However, this reactExports is undefined which results in an error: Error: Cannot read properties of undefined (useRef).

Here is a github repo that you can clone and reproduce the issue there.

Extra import in the remote app output:

Screenshot 2023-08-10 at 14 44 58

Commonjs import of "react" in "use-sync-external-store" package:

Screenshot 2023-08-10 at 15 01 57

Steps to reproduce

What is Expected?

nguyenbatranvan commented 1 year ago

I have cloned your repo to check it out, and I will give the following workaround:

  1. In host , please install more zustand
  2. In both the host and remote vite.config.ts directories, add the configuration:
federation({

 ...
 shared:{
  zustand:{}
 }
})
  1. In Remote, at the counter.jsx directory change the import of zustand:
change: import  {create} from "zustand";
to: import  create from "zustand";
  1. build & start

If you want a reference on how to share state between host and remote using zustand, you can refer to my repo Checkout

wijionejs commented 1 year ago

@nguyenbatranvan thanks for the response! Eventually, I've come to this workaround as well, that said if we I add a few more libraries that use commonjs imports I'll have to add them to the host app too (even if host app doesn't need them at all).

AsuraKev commented 1 year ago

have you tried downgrade vite-plugin-federation to v1.1.14?

tpreusse commented 1 year ago

Same issue happens with swr. Downgrading to 1.1.14 seems to have solved the problem.

wijionejs commented 1 year ago

@AsuraKev, @tpreusse Thanks guys, downgrading the version did help me.

kabaehr commented 8 months ago

Hey all, is there a recent solution or a plan for resolving this? The version 1.1.14 is now already rather old

The workaround of adding the package also to the host works, but is still just a workaround imho

oddie13 commented 8 months ago

I still got error even downgrading to 1.1.14 (for host and remote)