Tencent / hel

A module federation SDK which is unrelated to tool chain for module consumer. 工具链无关的运行时模块联邦sdk.
https://tencent.github.io/hel/
Other
931 stars 79 forks source link

在wujie微前端框中使用hel-micro加载远程组件会导致动态加载的js报错 #82

Closed bitjjj closed 8 months ago

bitjjj commented 8 months ago

如果页面是嵌入到wujie的iframe中,动态加载的所有js都不能正常加载。 问题在hel-micro-core中的patchAppendChild方法:

// 兼容一些第三方库对 Element.prototype.appendChild 打了补丁的情况
  const getAppend = function getAppend(nativeAppend, bindTarget) {
    const el = gs.Element;
    return el ? el.prototype.appendChild.bind(bindTarget) : nativeAppend;
  };

由于wujie没有拦截Element的appendChild方法,导致使用这个方法动态加载js,会把js加载到shadowDom里面去,而不是正确加载到iframe里面。

希望能兼容一下

fantasticsoul commented 8 months ago

好的,我想确认下,是否无界加载时,window上可以获取到一个__POWERED_BY_WUJIE__标识

fantasticsoul commented 8 months ago

可人工把项目里的 hel-micro-core 升级到最新版 4.8.7 试试,如果还是不行,人工调用下 hel-micro-coreinitGlobalConfig 方法设定下append 句柄

export interface IGlobalConfigFull {
  /**
   * 某些容器型微前端框架会对append打补丁,且打的目标不一样,有的是 Element.appendChild,有的是 head 和 body 上那个
   * 会导致 hel-micro 加载js资源报错,遇到此情况需要用户人工设定 append 句柄
   */
  headAppend: null,
  bodyAppend: null,
}

export type IGlobalConfig = Partial<IGlobalConfigFull>;

/**
 * 设定一些全局配置,如 headAppend, bodyAppend
 * @see https://github.com/tnfe/hel/issues/82
 */
export function initGlobalConfig(config: IGlobalConfig): void;
fantasticsoul commented 8 months ago

你好,看到你已关闭issue,请问此问题解决了么

fantasticsoul commented 8 months ago

window.document.head.appendChildwindow.document.body.appendChild 转义给hel的 headAppend 和 bodyAppend 即可

initGlobalConfig({
   headAppend: window.document.head.appendChild,
   bodyAppend: window.document.body.appendChild,
})