main.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import React from 'react'
  2. import ReactDOM, {Root} from 'react-dom/client'
  3. import './adapter/index.js'
  4. import App from './App.tsx'
  5. import './index.css'
  6. import './styles/index.less'
  7. import renderWithQiankun from "vite-plugin-qiankun/es/helper";
  8. let root: Root | null
  9. const render = (props: any ={}) => {
  10. console.log('[nginx-ui] render', props);
  11. const {container} = props;
  12. const rootContainer = container ? (container as HTMLElement).querySelector('#nginx_ui_root') : document.getElementById('nginx_ui_root')
  13. root = ReactDOM.createRoot(rootContainer as never);
  14. root.render(
  15. <React.StrictMode>
  16. <App />
  17. </React.StrictMode>
  18. )
  19. }
  20. const initQianKun = ()=>{
  21. renderWithQiankun({
  22. bootstrap(){
  23. console.log('bootstrap')
  24. },
  25. mount(props){
  26. console.log('[nginx-ui] mount', props)
  27. render(props)
  28. },
  29. unmount(){
  30. console.log('unmount')
  31. if (!root){
  32. return
  33. }
  34. try {
  35. root.unmount()
  36. root = null
  37. }catch (e) {
  38. console.log('[nginx-ui] unmount fail', e)
  39. }
  40. },
  41. update(props){
  42. console.log('update', props)
  43. }
  44. })
  45. }
  46. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  47. // @ts-ignore
  48. if (window.__POWERED_BY_QIANKUN__){
  49. initQianKun()
  50. } else {
  51. render()
  52. }