@lemon-fe/vite-plugin-micro-frontend 1.0.8 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/runtime.d.ts CHANGED
@@ -16,7 +16,14 @@ interface QiankunWindow {
16
16
  [x: string]: any;
17
17
  }
18
18
  declare const qiankunWindow: QiankunWindow;
19
- declare const renderWithQiankun: (qiankunLifeCycle: QiankunLifeCycle) => void;
19
+ /**
20
+ * 渲染乾坤微应用
21
+ * 自动从 index.html 中查找第一个带 id 的 div 作为挂载容器
22
+ * @param qiankunLifeCycle 生命周期钩子
23
+ */
24
+ declare const renderWithQiankun: (
25
+ /** 生命周期钩子 */
26
+ qiankunLifeCycle: QiankunLifeCycle) => void;
20
27
 
21
28
  interface DynamicOptions<P = Record<string, unknown>> {
22
29
  /** 动态加载函数,返回一个包含 default 导出的 Promise */
@@ -28,7 +35,6 @@ interface DynamicOptions<P = Record<string, unknown>> {
28
35
  }
29
36
  /**
30
37
  * 封装动态导入函数,兼容 Umi 的 dynamic
31
- *
32
38
  */
33
39
  declare function dynamic<P = Record<string, unknown>>(options: DynamicOptions<P>): ComponentType<P>;
34
40
 
package/dist/runtime.js CHANGED
@@ -1,4 +1,4 @@
1
- import require$$0, { createElement, Suspense, lazy, Component } from 'react';
1
+ import require$$0, { lazy, createElement, Suspense, Component } from 'react';
2
2
  import { qiankunWindow as qiankunWindow$1, renderWithQiankun as renderWithQiankun$1 } from 'vite-plugin-qiankun/es/helper';
3
3
 
4
4
  function getDefaultExportFromCjs (x) {
@@ -27761,7 +27761,26 @@ var ReactDOM = /*@__PURE__*/getDefaultExportFromCjs(reactDomExports);
27761
27761
  * 这个文件不应该包含任何 Node.js 模块
27762
27762
  */
27763
27763
  const qiankunWindow = qiankunWindow$1;
27764
- const renderWithQiankun = (qiankunLifeCycle) => {
27764
+ /**
27765
+ * 查找当前微应用的挂载容器
27766
+ * @param container 乾坤传入的容器(可选)
27767
+ */
27768
+ function findAppRoot(container) {
27769
+ // 乾坤环境:只从当前微应用的沙箱容器中查找,避免找到其他微应用的容器
27770
+ if (container) {
27771
+ return container.querySelector("div[id]");
27772
+ }
27773
+ // 独立运行(非乾坤环境):从 document.body 中查找
27774
+ return document.body.querySelector("div[id]");
27775
+ }
27776
+ /**
27777
+ * 渲染乾坤微应用
27778
+ * 自动从 index.html 中查找第一个带 id 的 div 作为挂载容器
27779
+ * @param qiankunLifeCycle 生命周期钩子
27780
+ */
27781
+ const renderWithQiankun = (
27782
+ /** 生命周期钩子 */
27783
+ qiankunLifeCycle) => {
27765
27784
  renderWithQiankun$1({
27766
27785
  mount(props) {
27767
27786
  return new Promise((resolve, reject) => {
@@ -27784,8 +27803,11 @@ const renderWithQiankun = (qiankunLifeCycle) => {
27784
27803
  return new Promise((resolve, reject) => {
27785
27804
  try {
27786
27805
  const { container } = props;
27787
- const mountRoot = container?.querySelector("#wms");
27788
- ReactDOM.unmountComponentAtNode(mountRoot || document.querySelector("#wms"));
27806
+ const mountElement = findAppRoot(container);
27807
+ if (!mountElement) {
27808
+ throw new Error("No div with id found in index.html");
27809
+ }
27810
+ ReactDOM.unmountComponentAtNode(mountElement);
27789
27811
  resolve();
27790
27812
  }
27791
27813
  catch (error) {
@@ -27855,15 +27877,16 @@ class DynamicErrorBoundary extends Component {
27855
27877
  }
27856
27878
  /**
27857
27879
  * 封装动态导入函数,兼容 Umi 的 dynamic
27858
- *
27859
27880
  */
27860
27881
  function dynamic(options) {
27882
+ // lazy 在外层调用,只创建一次,保证组件引用稳定(keepAlive 依赖此特性)
27883
+ const LazyComponent = lazy(options.loader);
27861
27884
  const LoadingComponent = options.loading;
27862
27885
  return function DynamicComponent(props) {
27863
27886
  const loadingElement = LoadingComponent
27864
27887
  ? createElement(LoadingComponent)
27865
27888
  : null;
27866
- return createElement(DynamicErrorBoundary, { LoadingComponent }, createElement(Suspense, { fallback: loadingElement }, createElement(lazy(options.loader), props)));
27889
+ return createElement(DynamicErrorBoundary, { LoadingComponent }, createElement(Suspense, { fallback: loadingElement }, createElement(LazyComponent, props)));
27867
27890
  };
27868
27891
  }
27869
27892