originjs / vite-plugin-federation

Module Federation for vite & rollup

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Virtual federation creates own wrapShareModule and not use the globalThis.__federation_shared__

bustEXZ opened this issue · comments

Its easy to access with globalThis.federation_shared.

Virtual generation creates own wrapShareModule, and use globalThis.federation_shared and its dependencies. And same dependencies loading multiple times

i fixed that in file remote-production.ts

Add scoped constant

const shareScope = options.shareScope || "default";

And change wrapShareModule:

function merge(obj1, obj2) {
  const mergedObj = Object.assign(obj1, obj2);
  for (const key of Object.keys(mergedObj)) {
    if (typeof mergedObj[key] === 'object' && typeof obj2[key] === 'object') {
      mergedObj[key] = merge(mergedObj[key], obj2[key]);
    }
  }
  return mergedObj;
}

const wrapShareModule = ${REMOTE_FROM_PARAMETER} => {
  return merge({
    ${getModuleMarker("shareScope")}
  }, (globalThis.__federation_shared__ || {})['${shareScope}'] || {}); // this will get our already loaded shared modules
}

And now all works good