微前端、Web 组件和共享库 [英] Micro-Frontends, Web Components, and Sharing Libraries
问题描述
所以我正在努力将我公司的应用程序迁移到微前端方法.我们遵循 https://micro-frontends.org/ 中描述的标准.虽然在幕后一切目前都在使用 React,但我们正在用 Web 组件包装东西,以便我们在未来拥有独立于框架的自由和灵活性.我们已经建立并运行了一个工作架构,到目前为止它运行良好.我们甚至在 Web 组件规范之上创建了一个漂亮的兼容层,它允许我们将类似 React 的 props 传递给 Web 组件,包括对象、数组,甚至函数.这允许他们之间更好的互动.
so I'm working on migrating my company's app to a micro-frontend approach. We are following the standard described in https://micro-frontends.org/. While under the hood everything is currently using React, we are wrapping things with Web Components so that we will have the freedom and flexibility to be framework-agnostic in the future. We've got a working architecture up and running and so far it is working beautifully. We even created a fancy compatibility layer on top of the Web Component spec which allows us to pass React-like props to the Web Components, including Objects, Arrays, and even Functions. This allows for much better interaction between them.
我们现在主要关注的是库的重复.我们是一家 React 商店,所以即使我们有这种与框架无关的方法,一切都在使用 React.虽然这种新方法使我们能够将应用的各个部分单独升级到更新的 React 版本(最终),但我们仍然不喜欢 React 库有这么多重复的想法.
The main concern we have right now is duplication of libraries. We're a React shop, so even though we have this framework agnostic approach, everything is using React. While this new approach gives us the ability to individually upgrade pieces of our app to a newer React version (finally), we still don't like the idea of so much duplication of the React library.
正确地说,即使是 Gzipped,React/ReactDOM 也超过 40kb.单独来说这是非常小的,但是按比例放大它开始占用越来越多的带宽.RAM 方面问题不大,这些库大约有 130kb,而且考虑到现在大多数设备的 RAM 容量,这不是什么大问题.
To put it in perspective, even Gzipped, React/ReactDOM are over 40kb. That's super tiny individually, but scaled up it starts to take up more and more bandwidth. RAM-wise it's less of an issue, about 130kb for those libraries, and given the RAM capacity of most devices now it's not a huge deal.
但是,当然,我们希望尽可能优化和简化.所以我希望有人可以提出一种方法,让微前端应用程序(包含在 Web 组件中的应用程序)可以从父应用程序获取 React 和其他库.
But, of course, we want things to be as optimized and streamlined as possible. So I'm hoping someone can suggest a way for the micro-frontend apps (the ones wrapped in a Web Component) can get React and other libraries from the parent app.
您应该知道父应用 JavaScript 在微前端之前加载.每个微前端都通过 标签加载.最后,我们目前不使用 Shadow DOM,这是我们为了将现有代码迁移到新的微前端架构而做出的权衡.
You should know that the parent app JavaScript is loaded prior to the micro-frontends. Each micro-frontend is loaded via a <script>
tag. Lastly, we are NOT using the Shadow DOM at the moment, a tradeoff we made to benefit how we are migrating our existing code into the new micro-frontend architecture.
推荐答案
可能的解决方案是使用 Import Map 准备库,但由于它不支持 IE11+ 我建议您使用 SystemJS?
Possible solution is to prepare library using Import Map but as it does not support IE11+ I recommend you using SystemJS?
https://github.com/systemjs/systemjs
尤其是这个似乎与您的情况很接近:
especially this one seems to be close to your case:
https://github.com/systemjs/systemjs-examples/tree/master/loading-code/react-hello-world
在 html 中:
<script type="systemjs-importmap">
{
"imports": {
"react": "https://cdn.jsdelivr.net/npm/react/umd/react.production.min.js",
"react-dom": "https://cdn.jsdelivr.net/npm/react-dom/umd/react-dom.production.min.js"
}
}
</script>
<script src="https://cdn.jsdelivr.net/npm/systemjs/dist/system.min.js"></script>
然后你可以导入 ReactJS,因为浏览器知道从哪里获取它.
Then you could import ReactJS, as browser knows where to take it from.
可能会构建某种库来查看您的微前端 (MFE) package.json
,以了解需要使用哪个库并动态创建导入对象类似于上面的例子.
Potentially there is a possibility to build some kind of library that looks into your micro front end's (MFE's) package.json
, to get know which library needs to be in use and dynamically create imports object similar to example above.
我们需要记住,需要涵盖版本控制.提到的库可能会存储某种映射,该映射将依赖项与版本与可访问的位置连接起来.想象一下,如果提到我们需要在每个 MFE 上处理不同的 React 版本:)
We need to keep in mind that there is a need to cover versioning check. Mentioned library could potentially store some kind of map which connects dependency with version with place where it's accessible. Imagine that in case mentioned we need to deal with different react version on each MFE :).
然后在加载另一个 MFE 库时可以检查是否已经包含了所需的依赖项,如果缺少一些,则下载它,否则使用已经获取的.
Then while loading another MFE library could check if required dependencies has been already included, if some missing, download it, otherwise use what has been already fetched.
另一件事是使用带有模块联合的 webpack 5,我还不推荐它,因为它对于生产来说不够稳定,但有可能开始使用它.难的部分会覆盖版本检查,所以很可能需要另一个抽象层来处理它.
Another thing is use webpack 5, with module federation, which I not recommended yet, as it is not stable enough for production, but there is a possibility to start playing with it. Hard part will be covering version check, so most probably need another abstraction layer to deal with it.
https://indepth.dev/webpack-5-module-federation-a-game-changer-in-javascript-architecture/
这篇关于微前端、Web 组件和共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!