ice-lab / icestark

:tiger: Micro Frontends solution for large application(面向大型应用的微前端解决方案),站点国内镜像:https://icestark.gitee.io
https://micro-frontends.ice.work
MIT License
2.04k stars 173 forks source link

微模块如何支持沙箱 #668

Closed ultranana closed 1 year ago

ultranana commented 1 year ago

业务上试用了一下微模块@ice/stark-module,但是发现没有对js做沙箱隔离。我也没有找到启用沙箱的对应api,能否提供下

ClarkXia commented 1 year ago

https://github.com/ice-lab/icestark/blob/ec2d44244a2b4ebd7af0e0ff0a10f528b33b8a64/packages/icestark-module/src/MicroModule.tsx#L83

组件传入 sandbox 就行,文档后续补充下这块的说明

ultranana commented 1 year ago

沙箱不生效怎么办

第一个模块组件:

import * as React from 'react';

type ComponentProps = {
  title: string,
};

export default function Ask(props: ComponentProps) {
  const { type, ...others } = props;
  window.a = 1
console.log("ask", a)

  return (
    <div className="Ask" {...others}>Hello Ask</div>
  );
}

第二个模块组件:

import * as React from 'react';

type ComponentProps = {
  title: string,
};

export default function Examle(props: ComponentProps) {
  const { type, ...others } = props;
  console.log(window.a)
  return (
    <div className="Examle" {...others}>Hello Examle</div>
  );
}

使用的代码

import React from 'react'
import { MicroModule, registerModules, getModules } from '@ice/stark-module';

registerModules([
  {
    name: 'Ask',
    url: 'http://localhost:3334/index/index.js',
  },
  {
    url: 'http://localhost:3333/index/index.js',
    name: 'Example',
  },

]);

const App = () => {
  return  <div>
  <MicroModule moduleName="Example" sandbox/>
  <MicroModule moduleName="Ask" sandbox/>
</div>;
}
export default App

image 部分情况下是这样的,加上sanbox了,感觉和js加载顺序有关系