@micro-zoe/micro-app 0.4.1 → 0.5.1

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
@@ -32,20 +32,20 @@ micro-app is a micro front-end framework launched by JD Retail. It renders based
32
32
 
33
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, nor is it tied to the business, 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,20 @@ 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
60
  <div>
82
61
  <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>
62
+ <!-- 👇 name is the application name, url is the html address -->
63
+ <micro-app name='app1' url='http://localhost:3000/'></micro-app>
85
64
  </div>
86
65
  </template>
87
66
  ```
88
67
 
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
68
  ## 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.
69
+ **Set cross-domain support in the headers of webpack-dev-server**
113
70
  ```js
114
71
  devServer: {
115
72
  headers: {
@@ -118,12 +75,10 @@ devServer: {
118
75
  },
119
76
  ```
120
77
 
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
78
+ The above micro front-end rendering is completed, and the effect is as follows:
122
79
 
123
80
  ![image](https://img10.360buyimg.com/imagetools/jfs/t1/188373/14/17696/41854/6111f4a0E532736ba/4b86f4f8e2044519.png)
124
81
 
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
82
  More detailed configuration can be viewed [Documentation](https://micro-zoe.github.io/micro-app/docs.html#/zh-cn/start).
128
83
 
129
84
  # 🤝 Contribution
package/README.zh-cn.md CHANGED
@@ -30,20 +30,21 @@
30
30
  # 📖简介
31
31
  micro-app是京东零售推出的一款微前端框架,它基于类WebComponent进行渲染,从组件化的思维实现微前端,旨在降低上手难度、提升工作效率。它是目前接入微前端成本最低的框架,并且提供了JS沙箱、样式隔离、元素隔离、预加载、资源地址补全、插件系统、数据通信等一系列完善的功能。
32
32
 
33
- micro-app与技术栈无关,也不和业务绑定,可以用于任何前端框架。
33
+ micro-app与技术栈无关,对前端框架没有限制,任何框架都可以作为基座应用嵌入任何类型的子应用。
34
34
 
35
- # 🔧开始使用
36
- 微前端分为基座应用和子应用,我们分别列出基座应用和子应用需要进行的修改,具体介绍micro-app的使用方式。
35
+ # 如何使用
36
+ 微前端分为基座应用(也可以叫做主应用)和子应用。
37
+
38
+ 这里以一种比较常见的情况举例:基座应用使用vue框架,采用history路由,子应用使用react框架,采用hash路由,我们分别列出基座应用和子应用需要进行的修改,具体介绍micro-app的使用方式。
37
39
 
38
40
  ## 基座应用
39
- > 基座应用以vue框架为例
40
41
 
41
- 1、安装依赖
42
+ **1、安装依赖**
42
43
  ```bash
43
44
  yarn add @micro-zoe/micro-app
44
45
  ```
45
46
 
46
- 2、在入口处引入依赖
47
+ **2、在入口处引入依赖**
47
48
  ```js
48
49
  // main.js
49
50
  import microApp from '@micro-zoe/micro-app'
@@ -51,63 +52,21 @@ import microApp from '@micro-zoe/micro-app'
51
52
  microApp.start()
52
53
  ```
53
54
 
54
- 3、分配一个路由给子应用
55
- ```js
56
- // router.js
57
- import Vue from 'vue'
58
- import VueRouter from 'vue-router' // vue-router@3.x
59
- import MyPage from './my-page.vue'
60
-
61
- Vue.use(VueRouter)
62
-
63
- const routes = [
64
- {
65
- // 👇 非严格匹配,/my-page/xxx 都将匹配到 MyPage 页面
66
- path: '/my-page/*',
67
- name: 'my-page',
68
- component: MyPage,
69
- },
70
- ]
71
-
72
- export default routes
73
- ```
74
-
75
- 4、在`MyPage`页面中嵌入微前端应用
55
+ **3、在页面中嵌入微前端应用**
76
56
  ```html
77
57
  <!-- my-page.vue -->
78
58
  <template>
79
59
  <div>
80
60
  <h1>子应用</h1>
81
61
  <!-- 👇 name为应用名称,url为html地址 -->
82
- <micro-app name='app1' url='http://localhost:3000/' baseroute='/my-page'></micro-app>
62
+ <micro-app name='app1' url='http://localhost:3000/'></micro-app>
83
63
  </div>
84
64
  </template>
85
65
  ```
86
66
 
87
- > url和子应用路由的关系请查看[路由一章](https://micro-zoe.github.io/micro-app/docs.html#/zh-cn/route)
88
-
89
67
  ## 子应用
90
- > 子应用以react框架为例
91
-
92
- 1、设置基础路由(如果基座应用是history路由,子应用是hash路由,不需要设置基础路由,这一步可以省略)
93
-
94
- ```js
95
- // router.js
96
- import { BrowserRouter, Switch, Route } from 'react-router-dom'
97
-
98
- export default function AppRoute () {
99
- return (
100
- // 👇 设置基础路由,子应用可以通过window.__MICRO_APP_BASE_ROUTE__获取基座应用下发的baseroute
101
- <BrowserRouter basename={window.__MICRO_APP_BASE_ROUTE__ || '/'}>
102
- <Switch>
103
- ...
104
- </Switch>
105
- </BrowserRouter>
106
- )
107
- }
108
- ```
109
68
 
110
- 2、在webpack-dev-server的headers中设置跨域支持。
69
+ **在webpack-dev-server的headers中设置跨域支持。**
111
70
  ```js
112
71
  devServer: {
113
72
  headers: {
@@ -116,12 +75,10 @@ devServer: {
116
75
  },
117
76
  ```
118
77
 
119
- 以上微前端就可以正常渲染,实现了在vue基座应用中嵌入react子应用,效果如下:
78
+ 以上微前端基本渲染完成,效果如下:
120
79
 
121
80
  <img src="https://img12.360buyimg.com/imagetools/jfs/t1/196940/34/1541/38365/610a14fcE46c21374/c321b9f8fa50a8fc.png" alt="result" width='900'/>
122
81
 
123
- 上面列出了react和vue框架的使用方式,它们是可以自由组合的,比如基座应用是react,子应用是vue,或者基座应用是vue,子应用是react,或者基座应用和子应用都是react、vue。 micro-app对前端框架没有限制,任何框架都可以作为基座应用嵌入任何类型框架的子应用。
124
-
125
82
  更多详细配置可以查看[官网文档](https://micro-zoe.github.io/micro-app/docs.html#/zh-cn/start)
126
83
 
127
84
  # 🤝 参与共建
package/lib/index.d.ts CHANGED
@@ -4,9 +4,34 @@
4
4
  // ../../@micro-app/types
5
5
 
6
6
  declare module '@micro-zoe/micro-app' {
7
- export { default as preFetch } from '@micro-zoe/micro-app/prefetch';
8
7
  export { default } from '@micro-zoe/micro-app/micro_app';
9
- export { removeDomScope, pureCreateElement, version, setCurrentAppName } from '@micro-zoe/micro-app/libs/utils';
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, // version >= 0.5.1
12
+ getAllApps, } from './create_app';
13
+ }
14
+
15
+ declare module '@micro-zoe/micro-app/micro_app' {
16
+ import type { OptionsType, MicroAppConfigType, lifeCyclesType, plugins, fetchType } from '@micro-app/types';
17
+ import preFetch from '@micro-zoe/micro-app/prefetch';
18
+ import { EventCenterForBaseApp } from '@micro-zoe/micro-app/interact';
19
+ class MicroApp extends EventCenterForBaseApp implements MicroAppConfigType {
20
+ tagName: string;
21
+ shadowDOM?: boolean;
22
+ destroy?: boolean;
23
+ inline?: boolean;
24
+ disableScopecss?: boolean;
25
+ disableSandbox?: boolean;
26
+ macro?: boolean;
27
+ lifeCycles?: lifeCyclesType;
28
+ plugins?: plugins;
29
+ fetch?: fetchType;
30
+ preFetch: typeof preFetch;
31
+ start(options?: OptionsType): void;
32
+ }
33
+ const _default: MicroApp;
34
+ export default _default;
10
35
  }
11
36
 
12
37
  declare module '@micro-zoe/micro-app/prefetch' {
@@ -35,33 +60,21 @@ declare module '@micro-zoe/micro-app/prefetch' {
35
60
  export function getGlobalAssets(assets: globalAssetsType): void;
36
61
  }
37
62
 
38
- declare module '@micro-zoe/micro-app/micro_app' {
39
- import type { OptionsType, MicroAppConfigType, lifeCyclesType, plugins, fetchType } from '@micro-app/types';
40
- import preFetch from '@micro-zoe/micro-app/prefetch';
41
- import { EventCenterForBaseApp } from '@micro-zoe/micro-app/interact';
42
- class MicroApp extends EventCenterForBaseApp implements MicroAppConfigType {
43
- tagName: string;
44
- shadowDOM?: boolean;
45
- destory?: boolean;
46
- inline?: boolean;
47
- disableScopecss?: boolean;
48
- disableSandbox?: boolean;
49
- macro?: boolean;
50
- lifeCycles?: lifeCyclesType;
51
- plugins?: plugins;
52
- fetch?: fetchType;
53
- preFetch: typeof preFetch;
54
- start(options?: OptionsType): void;
55
- }
56
- const _default: MicroApp;
57
- export default _default;
58
- }
59
-
60
63
  declare module '@micro-zoe/micro-app/libs/utils' {
61
64
  import type { Func } from '@micro-app/types';
62
65
  export const version = "__VERSION__";
63
66
  export const isBrowser: boolean;
64
67
  export const globalThis: any;
68
+ export function isUndefined(target: unknown): target is undefined;
69
+ export function isNull(target: unknown): target is null;
70
+ export function isString(target: unknown): target is string;
71
+ export function isBoolean(target: unknown): target is boolean;
72
+ export function isFunction(target: unknown): boolean;
73
+ export const isArray: (arg: any) => arg is any[];
74
+ export function isPlainObject(target: unknown): boolean;
75
+ export function isPromise(target: unknown): boolean;
76
+ export function isBoundFunction(target: any): boolean;
77
+ export function isShadowRoot(target: unknown): boolean;
65
78
  /**
66
79
  * format error log
67
80
  * @param msg message
@@ -86,10 +99,22 @@ declare module '@micro-zoe/micro-app/libs/utils' {
86
99
  */
87
100
  export function addProtocol(url: string): string;
88
101
  /**
89
- * Format URL address
90
- * @param url address
102
+ * format URL address
103
+ * note the scenes:
104
+ * 1. micro-app -> attributeChangedCallback
105
+ * 2. preFetch
106
+ */
107
+ export function formatAppURL(url: string | null, appName?: string | null): string;
108
+ /**
109
+ * format name
110
+ * note the scenes:
111
+ * 1. micro-app -> attributeChangedCallback
112
+ * 2. event_center -> EventCenterForMicroApp -> constructor
113
+ * 3. event_center -> EventCenterForBaseApp -> all methods
114
+ * 4. preFetch
115
+ * 5. plugins
91
116
  */
92
- export function formatURL(url: string | null, appName?: string | null): string;
117
+ export function formatAppName(name: string | null): string;
93
118
  /**
94
119
  * Get valid address, such as https://xxx/xx/xx.html to https://xxx/xx/
95
120
  * @param url app.url
@@ -110,11 +135,11 @@ declare module '@micro-zoe/micro-app/libs/utils' {
110
135
  /**
111
136
  * promise stream
112
137
  * @param promiseList promise list
113
- * @param successsCb success callback
138
+ * @param successCb success callback
114
139
  * @param errorCb failed callback
115
140
  * @param finallyCb finally callback
116
141
  */
117
- export function promiseStream<T>(promiseList: Array<Promise<T> | T>, successsCb: CallableFunction, errorCb: CallableFunction, finallyCb?: CallableFunction): void;
142
+ export function promiseStream<T>(promiseList: Array<Promise<T> | T>, successCb: CallableFunction, errorCb: CallableFunction, finallyCb?: CallableFunction): void;
118
143
  export function isSupportModuleScript(): boolean;
119
144
  export function createNonceSrc(): string;
120
145
  export function unique(array: any[]): any[];
@@ -123,7 +148,6 @@ declare module '@micro-zoe/micro-app/libs/utils' {
123
148
  export function getCurrentAppName(): string | null;
124
149
  export function removeDomScope(): void;
125
150
  export function isSafari(): boolean;
126
- export function isFunction(target: unknown): boolean;
127
151
  /**
128
152
  * Create pure elements
129
153
  */
@@ -134,7 +158,14 @@ declare module '@micro-zoe/micro-app/libs/utils' {
134
158
  * @param target Accept cloned elements
135
159
  * @param deep deep clone or transfer dom
136
160
  */
137
- export function cloneNode<T extends Element, Q extends Element>(origin: T, target: Q, deep: boolean): void;
161
+ export function cloneContainer<T extends Element, Q extends Element>(origin: T, target: Q, deep: boolean): void;
162
+ export function isInvalidQuerySelectorKey(key: string): boolean;
163
+ export function isUniqueElement(key: string): boolean;
164
+ /**
165
+ * get micro-app element
166
+ * @param target app container
167
+ */
168
+ export function getRootContainer(target: HTMLElement | ShadowRoot): HTMLElement;
138
169
  }
139
170
 
140
171
  declare module '@micro-zoe/micro-app/interact' {
@@ -156,6 +187,10 @@ declare module '@micro-zoe/micro-app/interact' {
156
187
  * @param data data
157
188
  */
158
189
  setGlobalData(data: Record<PropertyKey, unknown>): void;
190
+ /**
191
+ * get global data
192
+ */
193
+ getGlobalData(): Record<PropertyKey, unknown> | null;
159
194
  /**
160
195
  * clear all listener of global data
161
196
  * if appName exists, only the specified functions is cleared