umijs 接入 icestark

umijs接入文档 https://micro-frontends.ice.work/docs/guide/use-child/others/#umi-%E5%BA%94%E7%94%A8
umijs接入案例 https://github.com/maoxiaoke/icestark-umi-child

  1. pnpm add @ice/stark-app

app.tsx

src/app.tsx

  1. import ReactDOM from 'react-dom';
  2. import {
  3. isInIcestark,
  4. getMountNode,
  5. registerAppEnter,
  6. registerAppLeave
  7. } from '@ice/stark-app';
  8. // 在 icestark 中修改渲染的节点
  9. export function modifyClientRenderOpts(memo: any) {
  10. return {
  11. ...memo,
  12. rootElement: isInIcestark() ? getMountNode() : memo.rootElement
  13. };
  14. }
  15. // 为 icestark 注册生命周期
  16. export function render (oldRender: any) {
  17. if (isInIcestark()) {
  18. registerAppEnter(() => {
  19. oldRender();
  20. });
  21. registerAppLeave(() => {
  22. ReactDOM.unmountComponentAtNode(getMountNode());
  23. });
  24. } else {
  25. oldRender();
  26. }
  27. }

修改在 icestark 中修改渲染的根节点
umijs midifyConfig https://umijs.org/docs/api/plugin-api#modifyconfig

render

覆写 umijs默认的 render,为 icestark 注册生命周期
https://umijs.org/docs/api/runtime-config#renderoldrender-function

.umirc.ts

修改 umi 子应用 .umirc.ts,通过 base 属性添加路由前缀

app.tsx icestart

完整的子应用接入 icestart配置

  1. /**
  2. * 运行时配置
  3. * https://umijs.org/docs/api/runtime-config#dva
  4. *
  5. * icestark 微前端接入
  6. * https://micro-frontends.ice.work/docs/guide/use-child/others
  7. */
  8. import { useState } from 'react'
  9. import ReactDOM from 'react-dom'
  10. import dayjs from 'dayjs'
  11. import 'dayjs/locale/zh-cn'
  12. import { MenuFoldOutlined, MenuUnfoldOutlined } from '@ant-design/icons'
  13. import {
  14. isInIcestark,
  15. getMountNode,
  16. registerAppEnter,
  17. registerAppLeave,
  18. } from '@ice/stark-app'
  19. import { DEFAULT_NAME } from '@/constants'
  20. import Settings from '../config/defaultSettings'
  21. dayjs.locale('zh-cn')
  22. const purePath = [
  23. '/designer',
  24. '/publish',
  25. ]
  26. // 在 icestark 中修改渲染的节点
  27. export function modifyClientRenderOpts(memo: any) {
  28. return {
  29. ...memo,
  30. rootElement: isInIcestark() ? getMountNode() : memo.rootElement,
  31. }
  32. }
  33. // 为 icestark 注册生命周期
  34. export function render(oldRender: any) {
  35. if (isInIcestark()) {
  36. registerAppEnter(() => {
  37. oldRender()
  38. })
  39. registerAppLeave(() => {
  40. ReactDOM.unmountComponentAtNode(getMountNode())
  41. })
  42. } else {
  43. oldRender()
  44. }
  45. }
  46. // export function rootContainer(container, args) {
  47. // return React.createElement(ThemeProvider, null, container);
  48. // }
  49. /**
  50. * 全局初始化数据配置,用于 Layout 用户信息和权限初始化
  51. * layout 文档:https://next.umijs.org/docs/api/runtime-config#getinitialstate
  52. */
  53. export async function getInitialState(): Promise<{ name: string }> {
  54. return { name: DEFAULT_NAME }
  55. }
  56. export const layout = () => {
  57. // eslint-disable-next-line react-hooks/rules-of-hooks
  58. const [collapsed, setCollapsed] = useState(false)
  59. return {
  60. ...Settings,
  61. pure: !!purePath.find((path) => location.pathname.includes(path)),
  62. logo: 'https://img.alicdn.com/tfs/TB1YHEpwUT1gK0jSZFhXXaAtVXa-28-27.svg',
  63. menu: {
  64. locale: false,
  65. type: 'group',
  66. },
  67. avatarProps: {
  68. src: 'https://gw.alipayobjects.com/zos/antfincdn/efFD%24IOql2/weixintupian_20170331104822.jpg',
  69. size: 'small',
  70. title: '管理员',
  71. },
  72. breakpoint: false,
  73. // 关闭默认的折叠按钮
  74. collapsedButtonRender: false,
  75. collapsed,
  76. onCollapse: (flag: boolean) => setCollapsed(flag),
  77. headerContentRender: () => {
  78. return (
  79. <div className="header-render">
  80. {collapsed ? (
  81. <MenuUnfoldOutlined onClick={() => setCollapsed(!collapsed)} />
  82. ) : (
  83. <MenuFoldOutlined onClick={() => setCollapsed(!collapsed)} />
  84. )}
  85. </div>
  86. )
  87. },
  88. menuFooterRender: (props: any) => {
  89. if (props?.collapsed) return undefined
  90. return (
  91. <p
  92. style={{
  93. textAlign: 'center',
  94. paddingBlockStart: 12,
  95. }}
  96. >
  97. Power by {DEFAULT_NAME}
  98. </p>
  99. )
  100. },
  101. bgLayoutImgList: [
  102. {
  103. src: 'https://img.alicdn.com/imgextra/i2/O1CN01O4etvp1DvpFLKfuWq_!!6000000000279-2-tps-609-606.png',
  104. left: 85,
  105. bottom: 100,
  106. height: '303px',
  107. },
  108. {
  109. src: 'https://img.alicdn.com/imgextra/i2/O1CN01O4etvp1DvpFLKfuWq_!!6000000000279-2-tps-609-606.png',
  110. bottom: -68,
  111. right: -45,
  112. height: '303px',
  113. },
  114. {
  115. src: 'https://img.alicdn.com/imgextra/i3/O1CN018NxReL1shX85Yz6Cx_!!6000000005798-2-tps-884-496.png',
  116. bottom: 0,
  117. left: 0,
  118. width: '331px',
  119. },
  120. ],
  121. // dva: {
  122. // hmr: true,
  123. // },
  124. }
  125. }
  126. // export function onRouteChange({ location, clientRoutes, routes, action, basename }: any) {
  127. // console.log(800, location, clientRoutes, routes, action, basename);
  128. // }