zhangHongEn / module-federation-vite

vite plugin for module federation
8 stars 2 forks source link

support loading modules dynamically #1

Open husayt opened 1 month ago

husayt commented 1 month ago

Thank you for getting this effort ahead. I was wondering if loading modules dynamically is supported, since all the examples I have seen define remotes statically in vite config. Dynamic remotes would be really cool

zhangHongEn commented 1 month ago

Just use runtime https://module-federation.io/practice/frameworks/next/dynamic-remotes.html

husayt commented 1 month ago

@zhangHongEn I have tried with this vite.config

export default defineConfig(({ command }) => ({
  plugins: [
  federation({
      name: "host",
      filename: "remoteEntry.js",
      remotes: {},
      shared: {
        react: {
          singleton: true,
          eager: true,
          requiredVersion: "<=18.2.0 >15.0.0",
        },
      "react-dom": {
          singleton: true,
          requiredVersion: "<=18.2.0 >15.0.0",
        },
]

and i get this error with 1.0.8, when it is trying to run loadShare("react",{...}):

@module-federation_runtime.js:29 Uncaught (in promise) Error: [ Federation Runtime ]: Please call init first
    at error2 (@module-federation_runtime.js:29:13)
    at Object.assert2 [as assert] (@module-federation_runtime.js:21:9)
    at loadShare (@module-federation_runtime.js:3589:13)
    at __overrideModule__@mono_api.js:3:27

seems it is running loadshare before running init

zhangHongEn commented 1 month ago

Try this configuration first, it should be the problem. I will solve it in the plugin

build: {
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.indexOf("/@module-federation/runtime") > -1) {
            return "mfruntime";
          }
        },
      },
    },
  },