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
pnpm add @ice/stark-app
app.tsx
src/app.tsx
import ReactDOM from 'react-dom';
import {
isInIcestark,
getMountNode,
registerAppEnter,
registerAppLeave
} from '@ice/stark-app';
// 在 icestark 中修改渲染的节点
export function modifyClientRenderOpts(memo: any) {
return {
...memo,
rootElement: isInIcestark() ? getMountNode() : memo.rootElement
};
}
// 为 icestark 注册生命周期
export function render (oldRender: any) {
if (isInIcestark()) {
registerAppEnter(() => {
oldRender();
});
registerAppLeave(() => {
ReactDOM.unmountComponentAtNode(getMountNode());
});
} else {
oldRender();
}
}
修改在 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配置
/**
* 运行时配置
* https://umijs.org/docs/api/runtime-config#dva
*
* icestark 微前端接入
* https://micro-frontends.ice.work/docs/guide/use-child/others
*/
import { useState } from 'react'
import ReactDOM from 'react-dom'
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'
import { MenuFoldOutlined, MenuUnfoldOutlined } from '@ant-design/icons'
import {
isInIcestark,
getMountNode,
registerAppEnter,
registerAppLeave,
} from '@ice/stark-app'
import { DEFAULT_NAME } from '@/constants'
import Settings from '../config/defaultSettings'
dayjs.locale('zh-cn')
const purePath = [
'/designer',
'/publish',
]
// 在 icestark 中修改渲染的节点
export function modifyClientRenderOpts(memo: any) {
return {
...memo,
rootElement: isInIcestark() ? getMountNode() : memo.rootElement,
}
}
// 为 icestark 注册生命周期
export function render(oldRender: any) {
if (isInIcestark()) {
registerAppEnter(() => {
oldRender()
})
registerAppLeave(() => {
ReactDOM.unmountComponentAtNode(getMountNode())
})
} else {
oldRender()
}
}
// export function rootContainer(container, args) {
// return React.createElement(ThemeProvider, null, container);
// }
/**
* 全局初始化数据配置,用于 Layout 用户信息和权限初始化
* layout 文档:https://next.umijs.org/docs/api/runtime-config#getinitialstate
*/
export async function getInitialState(): Promise<{ name: string }> {
return { name: DEFAULT_NAME }
}
export const layout = () => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const [collapsed, setCollapsed] = useState(false)
return {
...Settings,
pure: !!purePath.find((path) => location.pathname.includes(path)),
logo: 'https://img.alicdn.com/tfs/TB1YHEpwUT1gK0jSZFhXXaAtVXa-28-27.svg',
menu: {
locale: false,
type: 'group',
},
avatarProps: {
src: 'https://gw.alipayobjects.com/zos/antfincdn/efFD%24IOql2/weixintupian_20170331104822.jpg',
size: 'small',
title: '管理员',
},
breakpoint: false,
// 关闭默认的折叠按钮
collapsedButtonRender: false,
collapsed,
onCollapse: (flag: boolean) => setCollapsed(flag),
headerContentRender: () => {
return (
<div className="header-render">
{collapsed ? (
<MenuUnfoldOutlined onClick={() => setCollapsed(!collapsed)} />
) : (
<MenuFoldOutlined onClick={() => setCollapsed(!collapsed)} />
)}
</div>
)
},
menuFooterRender: (props: any) => {
if (props?.collapsed) return undefined
return (
<p
style={{
textAlign: 'center',
paddingBlockStart: 12,
}}
>
Power by {DEFAULT_NAME}
</p>
)
},
bgLayoutImgList: [
{
src: 'https://img.alicdn.com/imgextra/i2/O1CN01O4etvp1DvpFLKfuWq_!!6000000000279-2-tps-609-606.png',
left: 85,
bottom: 100,
height: '303px',
},
{
src: 'https://img.alicdn.com/imgextra/i2/O1CN01O4etvp1DvpFLKfuWq_!!6000000000279-2-tps-609-606.png',
bottom: -68,
right: -45,
height: '303px',
},
{
src: 'https://img.alicdn.com/imgextra/i3/O1CN018NxReL1shX85Yz6Cx_!!6000000005798-2-tps-884-496.png',
bottom: 0,
left: 0,
width: '331px',
},
],
// dva: {
// hmr: true,
// },
}
}
// export function onRouteChange({ location, clientRoutes, routes, action, basename }: any) {
// console.log(800, location, clientRoutes, routes, action, basename);
// }