Skip to content

Commit

Permalink
fix: call initializeSharing before load remote while shareStrategy is…
Browse files Browse the repository at this point in the history
… version first (#3212)
  • Loading branch information
2heal1 authored Nov 13, 2024
1 parent 168ce96 commit c2b1efa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/nervous-tomatoes-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@module-federation/webpack-bundler-runtime': patch
'@module-federation/runtime': patch
---

fix: initializeSharing before load remote while shareStrategy is version first
4 changes: 3 additions & 1 deletion packages/runtime/src/remote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ export class RemoteHandler {
): Promise<T | null> {
const { host } = this;
try {
const { loadFactory = true } = options || { loadFactory: true };
const { loadFactory = true } = options || {
loadFactory: true,
};
// 1. Validate the parameters of the retrieved module. There are two module request methods: pkgName + expose and alias + expose.
// 2. Request the snapshot information of the current host and globally store the obtained snapshot information. The retrieved module information is partially offline and partially online. The online module information will retrieve the modules used online.
// 3. Retrieve the detailed information of the current module from global (remoteEntry address, expose resource address)
Expand Down
20 changes: 16 additions & 4 deletions packages/webpack-bundler-runtime/src/remotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,22 @@ export function remotes(options: RemotesOptions) {
);

const remoteModuleName = remoteName + data[1].slice(1);
return webpackRequire.federation.instance!.loadRemote(
remoteModuleName,
{ loadFactory: false, from: 'build' },
);
const instance = webpackRequire.federation.instance!;
const loadRemote = () =>
webpackRequire.federation.instance!.loadRemote(remoteModuleName, {
loadFactory: false,
from: 'build',
});

if (instance.options.shareStrategy === 'version-first') {
return Promise.all(
instance.sharedHandler.initializeSharing(data[0]),
).then(() => {
return loadRemote();
});
}

return loadRemote();
} catch (error) {
onError(error as Error);
}
Expand Down

0 comments on commit c2b1efa

Please sign in to comment.