Code Monkey home page Code Monkey logo

versea's Introduction

versea


PRs Welcome


背景

  • 随着业务的发展,微前端需求越来越复杂,仅仅提供隔离性的微前端框架不再满足业务需求,微前端需要一种 业务协作 + 隔离 的方案。

  • 随着业务的发展,每个业务团队可能都需要拥有自己特性的微前端框架,可以自己研发,也可以基于已有的微前端框架修改,一种可以批量生产自己特性的微前端框架的引擎亟待提出。

特性

  • 多框架兼容
  • 独立部署
  • 懒加载
  • 应用嵌套,单主应用
  • 强大的插件能力
  • 扩展能力
  • 微前端引擎

插件

  • 沙箱,自定义沙箱
  • 自定义应用匹配
  • html 入口

贡献者

LICENSE

MIT

versea's People

Contributors

chhu1 avatar kejinzhu avatar s-elo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

versea's Issues

[Core] 路由嵌套匹配之后应用加载问题

考虑这个一个问题,主应用使用 versea,目前接入两个子应用 A 和 B,假设 A 应用可以嵌套 B。

  • 主应用 versea 路由配置如下:
const routes = [{
  path: 'a',
  apps: [A],
  children: [{
    path: 'a1',
    apps: [A],
  }, {
    path: 'b',
    apps: [B],
  }],
}]
  • A 应用是 vue,使用 vue-router,配置如下:
const routes = [{
  path: 'a',
  component: Index,
  children: [{
    path: 'a1',
    component: ComponentA,
  }, {
    path: 'b',
    component: () => import('ComponentB'),
  }],
}]
  • A 应用 ComponentA 内容如下:
<template>
  <div>ComponentA</div>
</template>
  • A 应用 ComponentB 内容如下:
<template>
  <div>ComponentB</div>
  <!-- 用于嵌套 B 应用 -->
  <div id="inner_slot"></div>
</template>

复线步骤:

  1. 当前路由是 /a/a1,A 和 B 应用已经加载过。
  2. 现在由于用户点击浏览器前进或者后退,触发 popstate 事件。
  3. popstate 优先走 reroute 内部逻辑,匹配需要 mount A 和 B 应用,A 应用已经是 mounted,那么会 mount B,可是此时页面上显示的 ComponentA 的内容,无法 mount B。

因此这里存在两个问题:

我们拦截了 popstate 事件,可是此时无法 mount B,需要应用 A 先完成 vue-router 路由匹配逻辑,然后渲染出 ComponentB,才会展示<div id="inner_slot"></div> 在页面上,有了id 是 inner_slot 的 div 才能交给 versea mount B 应用。

  1. 可是我们已经拦截了 popstate 事件,应用 A vue-router 的路由匹配逻辑并不会执行。
  2. 即使应用 A vue-router 完成路由匹配,我们看到加载渲染 ComponentB 是异步的,versea 怎么知道 应用 A 内部什么时刻渲染完成 ComponentB

可添加超时时间的方法

并不是每个方法都可以添加超时设置,有些方法执行已经产生了副作用,而这些副作用在超时之后无法被销毁,因此不可以设置超时。

  1. load 阶段执行的加载逻辑
  2. waitForContainer 阶段

[Core] hooks 和 plugin 使用

除了 versea/core,还有一层 versea/versea 的封装

用于让不理解 inversify 的开发人员使用,不需要理解 inversify 的各种概念就能使用 versea

class Versea {
  constructor() {
    this.container = new Container({ defaultScope: 'Singleton' });
    this.container.load(buildProviderModule());
  }

  registerApp(options: AppOptions) {
    this.container.get(IAppService).registerApp(options);
  }
}

方案一

插件独立于 versea/core,也就是不依赖 versea/core 的依赖注入能力,依赖 versea/versea 的 Versea 类

class Versea {
  use(plugin: Plugin) {
    plugin.apply(this);
  }
}

插件写法如下:

class MyPlugin {
  constructor(options) {
    this.options = options;
  }

  apply(context) {
    context.container.get(IHooks).registerApp.tap('my-plugin', (hookParams) => {
      console.log(hookParams);
    });
  }
}

方案二

插件独立于 versea/core,不依赖 versea/versea 的 Versea 类,但依赖 versea/core 的依赖注入能力。

class Versea {
  use(pluginKey) {
    this.container.get(pluginKey).apply(this)
  }
}

插件写法如下:

@provide(IMyPlugin)
class MyPlugin {
  constructor(@inject(IHooks) hooks) {
    this._hooks = hooks;
  }

  apply(context) {
    this._hooks.registerApp.tap('my-plugin', (hookParams) => {
      console.log(hookParams);
    });
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.