简体   繁体   English

模块联合如何选择使用哪个依赖版本?

[英]How does module federation choose which dependency version to use?

I've been playing around with this module federation example , where the setup is relatively straightforward - a host is consuming a module from a remote with a shared react dependency.我一直在玩这个模块联合示例,其中设置相对简单 - 主机正在使用具有共享反应依赖关系的远程模块。 When running this locally, I noticed that despite both host and remote having the same react/react-dom versions, the remote's version is always the one downloaded.在本地运行时,我注意到尽管主机和远程具有相同的 react/react-dom 版本,但远程的版本始终是下载的版本。

Based on my research it seems that module federation will pick the "best" version of shared dependencies, but I'm surprised that the remote one will be chosen in a case where both have the same version.根据我的研究,模块联合似乎会选择共享依赖项的“最佳”版本,但我很惊讶在两者具有相同版本的情况下会选择远程版本。 How is this decision made?这个决定是如何做出的? Is there a way to force the host's version to be used in this case?有没有办法强制在这种情况下使用主机的版本?

Basically, when your host starts up it will register all the versions it has into the shared scope.基本上,当您的主机启动时,它会将它拥有的所有版本注册到共享的 scope 中。 Every time you load a remoteEntry.js from a remote, the remote will also add their versions to this same scope, but only if that exact version does not exist already.每次从远程加载remoteEntry.js时,远程也会将它们的版本添加到同一个 scope,但前提是该确切版本不存在。

So for example, if the host shares module-a at version 1.0.0 .例如,如果主机在version 1.0.0共享module-a -a。 When the host loads it will put module-a:1.0.0 into the shared context.当主机加载时,它会将module-a:1.0.0放入共享上下文中。 If the remote also shares module-a:1.0.0 it will not put it in the context, because it is already there.如果远程也共享module-a:1.0.0它不会把它放在上下文中,因为它已经在那里了。 If the host was sharing module-a:1.0.1 then the context will now have two versions: the module-a:1.0.0 from the host and module-a:1.0.1 from the remote.如果主机正在共享module-a:1.0.1 ,那么上下文现在将有两个版本:来自主机的module-a:1.0.0和来自远程module-a:1.0.1

At this point we are just talking about registration... we haven't chosen which version to use, but we are registering all unique versions shared from all remotes and hosts.在这一点上,我们只是在谈论注册......我们还没有选择使用哪个版本,但我们正在注册从所有遥控器和主机共享的所有唯一版本。 And basically the first to register it wins.基本上第一个注册它的人就赢了。

Now when the version resolution algorithm runs... it will figure out based on all the requirements which version to use.现在,当版本解析算法运行时......它将根据所有要求确定要使用哪个版本。 If the algorithm chooses version 1.0.0 of the module, then it will go to the scope and use whatever module is assigned to version 1.0.0 which in this case would be the one from the host, because the host ran first and was able to register it first.如果算法选择模块的1.0.0版本,那么它将 go 到 scope 并使用分配给版本1.0.0的任何模块,在这种情况下将是来自主机的模块,因为主机首先运行并且能够先注册它。 If the algorithm picked 1.0.1 it would use the module from the remote.如果算法选择1.0.1 ,它将使用远程模块。 If multiple remotes provided 1.0.1 then it will use the one from the remote that first registered it into the scope.如果多个遥控器提供1.0.1 ,那么它将使用首先将其注册到 scope 的遥控器中的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM