@micro-zoe/micro-app 0.5.0 → 0.6.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/README.md CHANGED
@@ -30,22 +30,22 @@ English|[简体中文](./README.zh-cn.md)|[Documentation](https://micro-zoe.
30
30
  # 📖Introduction
31
31
  micro-app is a micro front-end framework launched by JD Retail. It renders based on webcomponent-like and realizes the micro front-end from component thinking, it aiming to reduce the difficulty of getting started and improve work efficiency.
32
32
 
33
- It is the lowest cost framework for accessing micro front-end, only one line of code needs to be changed, and even nothing needs to be changed to access the micro front-end, and provides a series of perfect functions such as JS sandbox, style isolation, element isolation, preloading, resource address completion, plugin system, data communication and so on.
33
+ It is the lowest cost framework for accessing micro front-end, and provides a series of perfect functions such as JS sandbox, style isolation, element isolation, preloading, resource address completion, plugin system, data communication and so on.
34
34
 
35
- The micro-app has nothing to do with the technology stack, and can be used in any front-end framework.
35
+ micro-app has no restrictions on the front-end framework, and any framework can be used as a base application to embed any type of micro application of the framework.
36
36
 
37
- # Getting Start
38
- The micro front-end is divided into base application and micro application. We list the modifications required for base application and micro application respectively, and introduce the use of micro-app in detail.
37
+ # How to use
38
+ The micro front end is divided into a base application (also called main application) and a micro application.
39
39
 
40
- ## base application
41
- > The base application takes the vue framework as an example
40
+ Here is a common example: the base application uses the Vue framework, uses history routing, the micro application uses the react framework, and uses hash routing. We list the modifications that need to be made by the base application and the micro application, and introduce the use of micro-app in detail.
42
41
 
43
- 1、Install
42
+ ## base application
43
+ **1、Install**
44
44
  ```bash
45
45
  yarn add @micro-zoe/micro-app
46
46
  ```
47
47
 
48
- 2、import at the entrance
48
+ **2、import at the entrance**
49
49
  ```js
50
50
  // main.js
51
51
  import microApp from '@micro-zoe/micro-app'
@@ -53,63 +53,17 @@ import microApp from '@micro-zoe/micro-app'
53
53
  microApp.start()
54
54
  ```
55
55
 
56
- 3、Assign a route to the micro application
57
- ```js
58
- // router.js
59
- import Vue from 'vue'
60
- import VueRouter from 'vue-router' // vue-router@3.x
61
- import MyPage from './my-page.vue'
62
-
63
- Vue.use(VueRouter)
64
-
65
- const routes = [
66
- {
67
- // 👇 Non-strict matching, /my-page/xxx will be matched to the MyPage component
68
- path: '/my-page/*',
69
- name: 'my-page',
70
- component: MyPage,
71
- },
72
- ]
73
-
74
- export default routes
75
- ```
76
-
77
- 4、Use components in `my-page` pages
56
+ **3、Use components in page**
78
57
  ```html
79
58
  <!-- my-page.vue -->
80
59
  <template>
81
- <div>
82
- <h1>micro application</h1>
83
- <!-- 👇 name is the application name, globally unique, url is the html address -->
84
- <micro-app name='app1' url='http://localhost:3000/' baseroute='/my-page'></micro-app>
85
- </div>
60
+ <!-- 👇 name is the app name, url is the app address -->
61
+ <micro-app name='my-app' url='http://localhost:3000/'></micro-app>
86
62
  </template>
87
63
  ```
88
64
 
89
- > Please refer to [Routing Chapter](https://micro-zoe.github.io/micro-app/docs.html#/zh-cn/route) for the relationship between url and micro application routing
90
-
91
65
  ## micro application
92
- > The micro application takes the react framework as an example
93
-
94
- 1、Add basename for route(If the base application is history route and the micro application is hash route, it is not necessary to set the baseroute, this step can be skipped)
95
-
96
- ```js
97
- // router.js
98
- import { BrowserRouter, Switch, Route } from 'react-router-dom'
99
-
100
- export default function AppRoute () {
101
- return (
102
- // 👇 the micro application can get the baseroute issued by the base application through window.__MICRO_APP_BASE_ROUTE__
103
- <BrowserRouter basename={window.__MICRO_APP_BASE_ROUTE__ || '/'}>
104
- <Switch>
105
- ...
106
- </Switch>
107
- </BrowserRouter>
108
- )
109
- }
110
- ```
111
-
112
- 2、Set cross-domain support in the headers of webpack-dev-server.
66
+ **Set cross-domain support in the headers of webpack-dev-server**
113
67
  ```js
114
68
  devServer: {
115
69
  headers: {
@@ -118,12 +72,10 @@ devServer: {
118
72
  },
119
73
  ```
120
74
 
121
- Then micro front-end can be rendered normally, and the react micro application is embedded in the vue base application. The effect is as follows
75
+ The above micro front-end rendering is completed, and the effect is as follows:
122
76
 
123
77
  ![image](https://img10.360buyimg.com/imagetools/jfs/t1/188373/14/17696/41854/6111f4a0E532736ba/4b86f4f8e2044519.png)
124
78
 
125
- The above lists the usage of react and Vue framework. They can be combined freely. For example, the base application is react, the micro application is Vue, or the base application is Vue, the micro application is react, or both the base application and the micro application are react and Vue. The micro-app has no restrictions on the front-end framework, and any framework can be used as a base application to embed any type of micro application of the framework.
126
-
127
79
  More detailed configuration can be viewed [Documentation](https://micro-zoe.github.io/micro-app/docs.html#/zh-cn/start).
128
80
 
129
81
  # 🤝 Contribution
@@ -190,7 +142,13 @@ For more commands, see [DEVELP](https://github.com/micro-zoe/micro-app/blob/mast
190
142
  <details>
191
143
  <summary>Does it support vite?</summary>
192
144
 
193
- Yes, please see [adapt vite](https://micro-zoe.github.io/micro-app/docs.html#/zh-cn/advanced?id=_2%e3%80%81%e9%80%82%e9%85%8dvite) for details.
145
+ Yes, please see [adapt vite](https://micro-zoe.github.io/micro-app/docs.html#/zh-cn/framework/vite) for details.
146
+ </details>
147
+
148
+ <details>
149
+ <summary>Does it support ssr?</summary>
150
+
151
+ Yes, please see [nextjs](https://micro-zoe.github.io/micro-app/docs.html#/zh-cn/framework/nextjs), [nuxtjs](https://micro-zoe.github.io/micro-app/docs.html#/zh-cn/framework/nuxtjs) for details.
194
152
  </details>
195
153
 
196
154
  # Contributors
package/README.zh-cn.md CHANGED
@@ -28,24 +28,23 @@
28
28
  [English](https://github.com/micro-zoe/micro-app)|简体中文|[官网文档](https://micro-zoe.github.io/micro-app/)|[讨论组](https://github.com/micro-zoe/micro-app/discussions)|[Gitter群聊](https://gitter.im/microzoe/micro-app)
29
29
 
30
30
  # 📖简介
31
- micro-app是京东零售推出的一款微前端框架,它基于类WebComponent进行渲染,从组件化的思维实现微前端,旨在降低上手难度、提升工作效率。
31
+ micro-app是京东零售推出的一款微前端框架,它基于类WebComponent进行渲染,从组件化的思维实现微前端,旨在降低上手难度、提升工作效率。它是目前接入微前端成本最低的框架,并且提供了JS沙箱、样式隔离、元素隔离、预加载、资源地址补全、插件系统、数据通信等一系列完善的功能。
32
32
 
33
- 它是目前接入微前端成本最低的框架,只需要改动一行代码,甚至一行代码都不需要改动即可接入微前端,并且提供了JS沙箱、样式隔离、元素隔离、预加载、资源地址补全、插件系统、数据通信等一系列完善的功能。
33
+ micro-app与技术栈无关,对前端框架没有限制,任何框架都可以作为基座应用嵌入任何类型的子应用。
34
34
 
35
- micro-app与技术栈无关,可以用于任何前端框架。
35
+ # 如何使用
36
+ 微前端分为基座应用(也可以叫做主应用)和子应用。
36
37
 
37
- # 🔧开始使用
38
- 微前端分为基座应用和子应用,我们分别列出基座应用和子应用需要进行的修改,具体介绍micro-app的使用方式。
38
+ 这里以一种比较常见的情况举例:基座应用使用vue框架,采用history路由,子应用使用react框架,采用hash路由,我们分别列出基座应用和子应用需要进行的修改,具体介绍micro-app的使用方式。
39
39
 
40
40
  ## 基座应用
41
- > 基座应用以vue框架为例
42
41
 
43
- 1、安装依赖
42
+ **1、安装依赖**
44
43
  ```bash
45
44
  yarn add @micro-zoe/micro-app
46
45
  ```
47
46
 
48
- 2、在入口处引入依赖
47
+ **2、在入口处引入**
49
48
  ```js
50
49
  // main.js
51
50
  import microApp from '@micro-zoe/micro-app'
@@ -53,63 +52,18 @@ import microApp from '@micro-zoe/micro-app'
53
52
  microApp.start()
54
53
  ```
55
54
 
56
- 3、分配一个路由给子应用
57
- ```js
58
- // router.js
59
- import Vue from 'vue'
60
- import VueRouter from 'vue-router' // vue-router@3.x
61
- import MyPage from './my-page.vue'
62
-
63
- Vue.use(VueRouter)
64
-
65
- const routes = [
66
- {
67
- // 👇 非严格匹配,/my-page/xxx 都将匹配到 MyPage 页面
68
- path: '/my-page/*',
69
- name: 'my-page',
70
- component: MyPage,
71
- },
72
- ]
73
-
74
- export default routes
75
- ```
76
-
77
- 4、在`MyPage`页面中嵌入微前端应用
55
+ **3、在页面中嵌入微前端应用**
78
56
  ```html
79
57
  <!-- my-page.vue -->
80
58
  <template>
81
- <div>
82
- <h1>子应用</h1>
83
- <!-- 👇 name为应用名称,url为html地址 -->
84
- <micro-app name='app1' url='http://localhost:3000/' baseroute='/my-page'></micro-app>
85
- </div>
59
+ <!-- 👇 name为应用名称,url为应用地址 -->
60
+ <micro-app name='my-app' url='http://localhost:3000/'></micro-app>
86
61
  </template>
87
62
  ```
88
63
 
89
- > url和子应用路由的关系请查看[路由一章](https://micro-zoe.github.io/micro-app/docs.html#/zh-cn/route)
90
-
91
64
  ## 子应用
92
- > 子应用以react框架为例
93
-
94
- 1、设置基础路由(如果基座应用是history路由,子应用是hash路由,不需要设置基础路由,这一步可以省略)
95
65
 
96
- ```js
97
- // router.js
98
- import { BrowserRouter, Switch, Route } from 'react-router-dom'
99
-
100
- export default function AppRoute () {
101
- return (
102
- // 👇 设置基础路由,子应用可以通过window.__MICRO_APP_BASE_ROUTE__获取基座应用下发的baseroute
103
- <BrowserRouter basename={window.__MICRO_APP_BASE_ROUTE__ || '/'}>
104
- <Switch>
105
- ...
106
- </Switch>
107
- </BrowserRouter>
108
- )
109
- }
110
- ```
111
-
112
- 2、在webpack-dev-server的headers中设置跨域支持。
66
+ **在webpack-dev-server的headers中设置跨域支持。**
113
67
  ```js
114
68
  devServer: {
115
69
  headers: {
@@ -118,12 +72,10 @@ devServer: {
118
72
  },
119
73
  ```
120
74
 
121
- 以上微前端就可以正常渲染,实现了在vue基座应用中嵌入react子应用,效果如下:
75
+ 以上微前端基本渲染完成,效果如下:
122
76
 
123
77
  <img src="https://img12.360buyimg.com/imagetools/jfs/t1/196940/34/1541/38365/610a14fcE46c21374/c321b9f8fa50a8fc.png" alt="result" width='900'/>
124
78
 
125
- 上面列出了react和vue框架的使用方式,它们是可以自由组合的,比如基座应用是react,子应用是vue,或者基座应用是vue,子应用是react,或者基座应用和子应用都是react、vue。 micro-app对前端框架没有限制,任何框架都可以作为基座应用嵌入任何类型框架的子应用。
126
-
127
79
  更多详细配置可以查看[官网文档](https://micro-zoe.github.io/micro-app/docs.html#/zh-cn/start)
128
80
 
129
81
  # 🤝 参与共建
@@ -191,7 +143,13 @@ yarn start # 访问 http://localhost:3000
191
143
  <details>
192
144
  <summary>支持vite吗?</summary>
193
145
 
194
- 支持,详情请查看[适配vite](https://micro-zoe.github.io/micro-app/docs.html#/zh-cn/advanced?id=_2%e3%80%81%e9%80%82%e9%85%8dvite)
146
+ 支持,详情请查看[适配vite](https://micro-zoe.github.io/micro-app/docs.html#/zh-cn/framework/vite)
147
+ </details>
148
+
149
+ <details>
150
+ <summary>支持ssr吗?</summary>
151
+
152
+ 支持,详情请查看[nextjs](https://micro-zoe.github.io/micro-app/docs.html#/zh-cn/framework/nextjs)、[nuxtjs](https://micro-zoe.github.io/micro-app/docs.html#/zh-cn/framework/nuxtjs)
195
153
  </details>
196
154
 
197
155
  # 贡献者们
package/lib/index.d.ts CHANGED
@@ -5,9 +5,10 @@
5
5
 
6
6
  declare module '@micro-zoe/micro-app' {
7
7
  export { default } from '@micro-zoe/micro-app/micro_app';
8
- export { default as preFetch } from '@micro-zoe/micro-app/prefetch';
9
- export { removeDomScope, pureCreateElement, version } from '@micro-zoe/micro-app/libs/utils';
10
- export { EventCenterForMicroApp } from '@micro-zoe/micro-app/interact';
8
+ export { default as preFetch, } from '@micro-zoe/micro-app/prefetch';
9
+ export { removeDomScope, pureCreateElement, version, } from '@micro-zoe/micro-app/libs/utils';
10
+ export { EventCenterForMicroApp, } from '@micro-zoe/micro-app/interact';
11
+ export { getActiveApps, getAllApps, } from '@micro-zoe/micro-app/create_app';
11
12
  }
12
13
 
13
14
  declare module '@micro-zoe/micro-app/micro_app' {
@@ -22,6 +23,7 @@ declare module '@micro-zoe/micro-app/micro_app' {
22
23
  disableScopecss?: boolean;
23
24
  disableSandbox?: boolean;
24
25
  macro?: boolean;
26
+ ssr?: boolean;
25
27
  lifeCycles?: lifeCyclesType;
26
28
  plugins?: plugins;
27
29
  fetch?: fetchType;
@@ -273,3 +275,70 @@ declare module '@micro-zoe/micro-app/interact' {
273
275
  export {};
274
276
  }
275
277
 
278
+ declare module '@micro-zoe/micro-app/create_app' {
279
+ import type { AppInterface, sourceType, SandBoxInterface } from '@micro-app/types';
280
+ export const appInstanceMap: Map<string, AppInterface>;
281
+ export interface CreateAppParam {
282
+ name: string;
283
+ url: string;
284
+ ssrUrl?: string;
285
+ scopecss: boolean;
286
+ useSandbox: boolean;
287
+ macro?: boolean;
288
+ inline?: boolean;
289
+ baseroute?: string;
290
+ container?: HTMLElement | ShadowRoot;
291
+ }
292
+ export default class CreateApp implements AppInterface {
293
+ umdMode: boolean;
294
+ isPrefetch: boolean;
295
+ name: string;
296
+ url: string;
297
+ ssrUrl: string;
298
+ container: HTMLElement | ShadowRoot | null;
299
+ inline: boolean;
300
+ scopecss: boolean;
301
+ useSandbox: boolean;
302
+ macro: boolean;
303
+ baseroute: string;
304
+ source: sourceType;
305
+ sandBox: SandBoxInterface | null;
306
+ constructor({ name, url, ssrUrl, container, inline, scopecss, useSandbox, macro, baseroute, }: CreateAppParam);
307
+ loadSourceCode(): void;
308
+ /**
309
+ * When resource is loaded, mount app if it is not prefetch or unmount
310
+ */
311
+ onLoad(html: HTMLElement): void;
312
+ /**
313
+ * Error loading HTML
314
+ * @param e Error
315
+ */
316
+ onLoadError(e: Error): void;
317
+ /**
318
+ * mount app
319
+ * @param container app container
320
+ * @param inline js runs in inline mode
321
+ * @param baseroute route prefix, default is ''
322
+ */
323
+ mount(container?: HTMLElement | ShadowRoot, inline?: boolean, baseroute?: string): void;
324
+ /**
325
+ * unmount app
326
+ * @param destroy completely destroy, delete cache resources
327
+ * @param unmountcb callback of unmount
328
+ */
329
+ unmount(destroy: boolean, unmountcb?: CallableFunction): void;
330
+ actionsForCompletelyDestory(): void;
331
+ hiddenKeepAliveApp(): void;
332
+ showKeepAliveApp(container: HTMLElement | ShadowRoot): void;
333
+ /**
334
+ * app rendering error
335
+ * @param e Error
336
+ */
337
+ onerror(e: Error): void;
338
+ getAppState(): string;
339
+ getKeepAliveState(): string | null;
340
+ }
341
+ export function getActiveApps(): string[];
342
+ export function getAllApps(): string[];
343
+ }
344
+