模块联邦实践

早期

早期业务,线进行开发的时候,我复杂的业务,和在一个主应用中,发布,测试部署,严重耦合,多个业务线复杂人,迭代,一旦有bug 就会立刻回滚,最终导致上线失败率很低 并且只是改变其中的子页面,模块,导致整个应用,全部都进行拆分 而且业务形态,非常集中以报表,图表为主

所有结合当时的业务形态,我们这里,主要是用模块联邦来处理,进行拆分

首先,划分主子应用边界 左侧栏,以及顶部栏,以及图表,下拉框,表格,都是划到主应用 作为子应用,主要去负责

最后结合具体实践,和坑的整理

模块联邦

  1. 作为webpack 承担的功能,解决的是单体复杂应用,如难以单独部署,协作问题,以及

拆分一个巨型应用

先去配置一个最基本的模块联邦微应用: 以下以一些

js 复制代码
// 主应用
plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html',
    }),
    new ModuleFederationPlugin({
      name: 'hostApp',
      filename: 'remoteEntry.js',
      exposes: {
        './RouteGuard': './src/components/RouteGuard',
        './GlobalConfigContext': './src/context/GlobalConfigContext',
        './ThemeContext': './src/context/ThemeContext',
        './ActionToolbar': './src/components/ActionToolbar',
        './SmartTable': './src/components/SmartTable/SmartTable',
        './BaseChart': './src/components/BaseChart/BaseChart',
        './DateProcessor': './src/components/DateProcessor',
        './useBusinessData': './src/hooks/useBusinessData',
        './biz-utils': './src/utils/biz-utils',
        ...
      },
      remotes: {
        remoteApp: 'remoteApp@http://localhost:3001/remoteEntry.js',
      },
      shared: {
        react: { singleton: true, requiredVersion: false, eager: true },
        'react-dom': { singleton: true, requiredVersion: false, eager: true },
        antd: { singleton: true, eager: true },
        axios: { singleton: true },
        'react-router-dom': { singleton: true },
        'react-redux': { singleton: true },
      },
    }),
    //

//子应用
plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html',
    }),
    new ModuleFederationPlugin({
      name: 'remoteApp',
      // 这里的也必须用这个remoteEntr.js吗
      filename: 'remoteEntry.js',
      // 这里暴露的是业务模块,不是共用的组件
      exposes: {
        './ReportModule': './src/modules/ReportModule/ReportModule',
        './OptionChainModule': './src/modules/OptionChainModule/OptionChainModule'
      },
      remotes: {
        hostApp: 'hostApp@http://localhost:3000/remoteEntry.js',
      }
    })

实践中常见坑

  1. 初期的useTheme context 上下文,共享缺失的问题
  2. 主应用,有context的访问,子应用也有类似的访问
  3. 主子应用和主应用,最好是共用一套,全局状态管理

css 隔离

非常常见的css 污染,对于模块联邦,没有内置的隔离

一种方案为,我自己通过建立xxx.module.css 文件,来进行模块化处理

js 复制代码
<div className="report-module-container>
    <div className={styles.reportModuleContainer}

由于本公司技术栈,以及金融报表业务,不允许,有太多的自定义样式, 因此没有采用更灵活的 css-in-js模式.

js 复制代码
const Button = styled.button`
  background: blue;
  color: white;
`;

通过这种模式来进行处理,那么其他模块,主应用自然样式是不会干扰的.

模块联邦为何不自己提供呢?

相关推荐
uzong5 分钟前
AI Agent 是什么,如何理解它,未来挑战和思考
人工智能·后端·架构
爱学习的程序媛1 小时前
【Web前端】JavaScript设计模式全解析
前端·javascript·设计模式·web
小码哥_常1 小时前
从SharedPreferences到DataStore:Android存储进化之路
前端
老黑1 小时前
开源工具 AIDA:给 AI 辅助开发加一个数据采集层,让 AI 从错误中自动学习(Glama 3A 认证)
前端·react.js·ai·nodejs·cursor·vibe coding·claude code
jessecyj1 小时前
Spring boot整合quartz方法
java·前端·spring boot
苦瓜小生2 小时前
【前端】|【js手撕】经典高频面试题:手写实现function.call、apply、bind
java·前端·javascript
天若有情6732 小时前
前端HTML精讲03:页面性能优化+懒加载,搞定首屏加速
前端·性能优化·html
踩着两条虫2 小时前
AI驱动的Vue3应用开发平台深入探究(十):物料系统之内置组件库
android·前端·vue.js·人工智能·低代码·系统架构·rxjava
swipe2 小时前
AI 应用里的 Memory,不是“保存聊天记录”,而是管理上下文预算
前端·llm·agent