@micro-zoe/micro-app 1.0.0-alpha.0 → 1.0.0-alpha.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micro-zoe/micro-app",
3
- "version": "1.0.0-alpha.0",
3
+ "version": "1.0.0-alpha.10",
4
4
  "description": "A lightweight, efficient and powerful micro front-end framework",
5
5
  "private": false,
6
6
  "main": "lib/index.min.js",
@@ -44,6 +44,8 @@
44
44
  "test": "jest",
45
45
  "test:watch": "jest --watch",
46
46
  "test:coverage": "jest --coverage",
47
+ "test:unit": "jest src/__tests__/unit",
48
+ "test:unit:coverage": "npm run test:unit -- --coverage",
47
49
  "prepublishOnly": "yarn build"
48
50
  },
49
51
  "repository": {
@@ -7,85 +7,160 @@ declare module '@micro-app/types' {
7
7
 
8
8
  type microAppWindowType = Window & any
9
9
 
10
+ type AppName = string
11
+
12
+ type SourceAddress = string
13
+
14
+ type AttrsType = Map<string, string>
15
+
16
+ type RequestIdleCallbackOptions = {
17
+ timeout: number
18
+ }
19
+
20
+ type RequestIdleCallbackInfo = {
21
+ readonly didTimeout: boolean
22
+ timeRemaining: () => number
23
+ }
24
+
25
+ type fiberTasks = Array<() => Promise<void>> | null
26
+
27
+ interface EffectController {
28
+ recordEffect(): void
29
+ rebuildEffect(): void
30
+ releaseEffect(): void
31
+ }
32
+
33
+ interface SandBoxStartParams {
34
+ umdMode: boolean
35
+ baseroute: string
36
+ useMemoryRouter: boolean
37
+ defaultPage: string
38
+ disablePatchRequest: boolean
39
+ }
40
+
41
+ interface SandBoxStopParams {
42
+ umdMode: boolean
43
+ keepRouteState: boolean
44
+ clearEventSource: boolean
45
+ clearData: boolean
46
+ }
47
+
10
48
  interface SandBoxInterface {
11
49
  proxyWindow: WindowProxy
12
50
  microAppWindow: Window // Proxy target
13
- start (
14
- baseRoute: string,
15
- useMemoryRouter: boolean,
16
- defaultPage: string,
17
- ): void
18
- stop (keepRouteState: boolean): void
51
+ start (startParams: SandBoxStartParams): void
52
+ stop (stopParams: SandBoxStopParams): void
53
+ releaseGlobalEffect (clearData?: boolean): void
19
54
  // record umd snapshot before the first execution of umdHookMount
20
- recordUmdSnapshot (): void
55
+ recordEffectSnapshot (): void
21
56
  // rebuild umd snapshot before remount umd app
22
- rebuildUmdSnapshot (): void
57
+ rebuildEffectSnapshot (): void
23
58
  setRouteInfoForKeepAliveApp (): void
24
59
  removeRouteInfoForKeepAliveApp (): void
60
+ setPreRenderState (state: boolean): void
61
+ }
62
+
63
+ interface SandBoxAdapter {
64
+ // Variables that can only assigned to rawWindow
65
+ escapeSetterKeyList: PropertyKey[]
66
+
67
+ // Variables that can escape to rawWindow
68
+ staticEscapeProperties: PropertyKey[]
69
+
70
+ // Variables that scoped in child app
71
+ staticScopeProperties: PropertyKey[]
72
+
73
+ // adapter for react
74
+ // injectReactHRMProperty (): void
25
75
  }
26
76
 
27
- type sourceLinkInfo = {
28
- code: string // code
29
- placeholder?: Comment | null // placeholder comment
30
- isGlobal: boolean // is global asset
77
+ type LinkSourceInfo = {
78
+ code: string, // source code
79
+ appSpace: Record<string, {
80
+ attrs: Map<string, string>, // active element.attributes
81
+ placeholder?: Comment | null, // placeholder comment
82
+ parsedCode?: string, // parsed code
83
+ prefix?: string, // micro-app[name=appName]
84
+ }>
85
+ }
86
+
87
+ type ScriptSourceInfo = {
88
+ code: string, // source code
89
+ isExternal: boolean, // external script
90
+ appSpace: Record<string, {
91
+ async: boolean, // async script
92
+ defer: boolean, // defer script
93
+ module: boolean, // module type script
94
+ inline: boolean, // run js with inline script
95
+ pure: boolean, // pure script
96
+ attrs: Map<string, string>, // element attributes
97
+ parsedCode?: string, // bind code
98
+ parsedFunction?: Function | null, // code to function
99
+ wrapInSandBox?: boolean // use sandbox
100
+ }>
101
+ }
102
+
103
+ type sourceType = {
104
+ html: HTMLElement | null, // html address
105
+ links: Set<string>, // style/link address list
106
+ scripts: Set<string>, // script address list
31
107
  }
32
108
 
33
- type sourceScriptInfo = {
34
- code: string // code
35
- isExternal: boolean // external script
36
- isDynamic: boolean // dynamic create script
37
- async: boolean // async script
38
- defer: boolean // defer script
39
- module: boolean // module type script
40
- isGlobal?: boolean // share js to global
41
- code2Function?: Function // code to Function
109
+ interface MountParam {
110
+ container: HTMLElement | ShadowRoot // app container
111
+ inline: boolean // run js in inline mode
112
+ useMemoryRouter: boolean // use virtual router
113
+ defaultPage: string // default page of virtual router
114
+ baseroute: string // route prefix, default is ''
115
+ disablePatchRequest: boolean // prevent rewrite request method of child app
116
+ fiber: boolean // run js in fiber mode
117
+ esmodule: boolean // support type='module' script
118
+ // hiddenRouter: boolean
42
119
  }
43
120
 
44
- interface sourceType {
45
- html?: HTMLElement
46
- links: Map<string, sourceLinkInfo>
47
- scripts: Map<string, sourceScriptInfo>
121
+ interface UnmountParam {
122
+ destroy: boolean, // completely destroy, delete cache resources
123
+ clearData: boolean // clear data of dateCenter
124
+ keepRouteState: boolean // keep route state when unmount, default is false
125
+ unmountcb?: CallableFunction // callback of unmount
48
126
  }
49
127
 
50
128
  // app instance
51
- interface AppInterface {
52
- isPrefetch: boolean // whether prefetch app, default is false
53
- prefetchResolve: (() => void) | null // prefetch callback
129
+ interface AppInterface extends Pick<ParentNode, 'querySelector' | 'querySelectorAll'> {
130
+ source: sourceType // source list
131
+ sandBox: SandBoxInterface | null // sandbox
54
132
  name: string // app name
55
133
  url: string // app url
56
- ssrUrl: string // html path in ssr mode
57
- container: HTMLElement | ShadowRoot | null // container maybe null, micro-app, shadowRoot, DIV(keep-alive)
58
- inline: boolean // whether js runs in inline script mode, default is false
59
134
  scopecss: boolean // whether use css scoped, default is true
60
135
  useSandbox: boolean // whether use js sandbox, default is true
61
- useMemoryRouter: boolean // whether use memoryRouter, default is true
62
- baseroute: string // route prefix, default is ''
63
- keepRouteState: boolean // keep route state when unmount, default is false
64
- source: sourceType // sources of css, js, html
65
- sandBox: SandBoxInterface | null // sandbox
136
+ inline: boolean // whether js runs in inline script mode, default is false
137
+ esmodule: boolean // support esmodule in script
138
+ ssrUrl: string // html path in ssr mode
139
+ container: HTMLElement | ShadowRoot | null // container maybe null, micro-app, shadowRoot, div(keep-alive)
66
140
  umdMode: boolean // is umd mode
67
- defaultPage: string // default page when mount
141
+ fiber: boolean // fiber mode
142
+ useMemoryRouter: boolean // use virtual router
143
+ isPrefetch: boolean // whether prefetch app, default is false
144
+ isPrerender: boolean
145
+ prefetchLevel?: number
146
+ // defaultPage: string // default page when mount
147
+ // baseroute: string // route prefix, default is ''
148
+ // hiddenRouter: boolean // hide router info of child from browser url
68
149
 
69
150
  // Load resources
70
151
  loadSourceCode (): void
71
152
 
72
153
  // resource is loaded
73
- onLoad (html: HTMLElement): void
154
+ onLoad (html: HTMLElement, defaultPage?: string, disablePatchRequest?: boolean): void
74
155
 
75
156
  // Error loading HTML
76
157
  onLoadError (e: Error): void
77
158
 
78
159
  // mount app
79
- mount (
80
- container?: HTMLElement | ShadowRoot,
81
- inline?: boolean,
82
- baseroute?: string,
83
- keepRouteState?: boolean,
84
- defaultPage?: string,
85
- ): void
160
+ mount (mountParams: MountParam): void
86
161
 
87
162
  // unmount app
88
- unmount (destroy: boolean, unmountcb?: CallableFunction): void
163
+ unmount (unmountParam: UnmountParam): void
89
164
 
90
165
  // app rendering error
91
166
  onerror (e: Error): void
@@ -99,7 +174,7 @@ declare module '@micro-app/types' {
99
174
  actionsForCompletelyDestroy (): void
100
175
 
101
176
  // hidden app when disconnectedCallback with keep-alive
102
- hiddenKeepAliveApp (): void
177
+ hiddenKeepAliveApp (callback?: CallableFunction): void
103
178
 
104
179
  // show app when connectedCallback with keep-alive
105
180
  showKeepAliveApp (container: HTMLElement | ShadowRoot): void
@@ -119,13 +194,20 @@ declare module '@micro-app/types' {
119
194
  attributeChangedCallback (a: 'name' | 'url', o: string, n: string): void
120
195
  }
121
196
 
122
- type prefetchParam = {
197
+ interface prefetchParam {
123
198
  name: string,
124
199
  url: string,
200
+ // old config 👇
125
201
  disableScopecss?: boolean
126
202
  disableSandbox?: boolean
127
- disableMemoryRouter?: boolean
128
- shadowDOM?: boolean
203
+ // old config 👆
204
+ 'disable-scopecss'?: boolean
205
+ 'disable-sandbox'?: boolean
206
+ inline?: boolean
207
+ esmodule?: boolean
208
+ level?: number
209
+ 'default-page'?: string
210
+ 'disable-patch-request'?: boolean
129
211
  }
130
212
 
131
213
  // prefetch params
@@ -133,13 +215,18 @@ declare module '@micro-app/types' {
133
215
 
134
216
  // lifeCycles
135
217
  interface lifeCyclesType {
136
- created?(e?: CustomEvent): void
137
- beforemount?(e?: CustomEvent): void
138
- mounted?(e?: CustomEvent): void
139
- unmount?(e?: CustomEvent): void
140
- error?(e?: CustomEvent): void
218
+ created(e: CustomEvent): void
219
+ beforemount(e: CustomEvent): void
220
+ mounted(e: CustomEvent): void
221
+ unmount(e: CustomEvent): void
222
+ error(e: CustomEvent): void
223
+ beforeshow(e: CustomEvent): void
224
+ aftershow(e: CustomEvent): void
225
+ afterhidden(e: CustomEvent): void
141
226
  }
142
227
 
228
+ type AssetsChecker = (url: string) => boolean;
229
+
143
230
  type plugins = {
144
231
  // global plugin
145
232
  global?: Array<{
@@ -147,10 +234,16 @@ declare module '@micro-app/types' {
147
234
  scopeProperties?: Array<PropertyKey>
148
235
  // Properties that can be escape to rawWindow
149
236
  escapeProperties?: Array<PropertyKey>
237
+ // Exclude JS or CSS
238
+ excludeChecker?: AssetsChecker
239
+ // Ignore JS or CSS
240
+ ignoreChecker?: AssetsChecker
150
241
  // options for plugin as the third parameter of loader
151
- options?: unknown
242
+ options?: Record<string, unknown>
152
243
  // handle function
153
- loader?: (code: string, url: string, options: unknown, info: sourceScriptInfo) => string
244
+ loader?: (code: string, url: string) => string
245
+ // html processor
246
+ processHtml?: (code: string, url: string) => string
154
247
  }>
155
248
 
156
249
  // plugin for special app
@@ -160,14 +253,25 @@ declare module '@micro-app/types' {
160
253
  scopeProperties?: Array<PropertyKey>
161
254
  // Properties that can be escape to rawWindow
162
255
  escapeProperties?: Array<PropertyKey>
256
+ // Exclude JS or CSS
257
+ excludeChecker?: AssetsChecker
258
+ // Ignore JS or CSS
259
+ ignoreChecker?: AssetsChecker
163
260
  // options for plugin as the third parameter of loader
164
- options?: unknown
261
+ options?: Record<string, unknown>
165
262
  // handle function
166
- loader?: (code: string, url: string, options: unknown, info: sourceScriptInfo) => string
263
+ loader?: (code: string, url: string) => string
264
+ // html processor
265
+ processHtml?: (code: string, url: string) => string
167
266
  }>
168
267
  }
169
268
  }
170
269
 
270
+ type GetActiveAppsParam = {
271
+ excludeHiddenApp?: boolean,
272
+ excludePreRender?: boolean,
273
+ }
274
+
171
275
  type fetchType = (url: string, options: Record<string, unknown>, appName: string | null) => Promise<string>
172
276
 
173
277
  type globalAssetsType = {
@@ -175,35 +279,44 @@ declare module '@micro-app/types' {
175
279
  css?: string[],
176
280
  }
177
281
 
178
- type OptionsType = {
179
- tagName?: string
282
+ interface MicroAppConfig {
180
283
  shadowDOM?: boolean
181
284
  destroy?: boolean
285
+ destory?: boolean
182
286
  inline?: boolean
287
+ // old config 👇
183
288
  disableScopecss?: boolean
184
289
  disableSandbox?: boolean
185
- disableMemoryRouter?: boolean
290
+ // old config 👆
291
+ 'disable-scopecss'?: boolean
292
+ 'disable-sandbox'?: boolean
293
+ 'disable-memory-router'?: boolean
294
+ 'disable-patch-request'?: boolean
295
+ 'keep-router-state'?: boolean
296
+ 'hidden-router'?: boolean
297
+ 'keep-alive'?: boolean
298
+ 'clear-data'?: boolean
299
+ esmodule?: boolean
186
300
  ssr?: boolean
301
+ fiber?: boolean
302
+ prefetchLevel?: number
303
+ prefetchDelay?: number
304
+ }
305
+
306
+ interface OptionsType extends MicroAppConfig {
307
+ tagName?: string
187
308
  lifeCycles?: lifeCyclesType
188
309
  preFetchApps?: prefetchParamList
189
310
  plugins?: plugins
190
311
  fetch?: fetchType
191
312
  globalAssets?: globalAssetsType,
313
+ excludeAssetFilter?: (assetUrl: string) => boolean
192
314
  }
193
315
 
194
316
  // MicroApp config
195
- interface MicroAppConfigType {
317
+ interface MicroAppBaseType {
196
318
  tagName: string
197
- shadowDOM?: boolean
198
- destroy?: boolean
199
- inline?: boolean
200
- disableScopecss?: boolean
201
- disableSandbox?: boolean
202
- disableMemoryRouter?: boolean
203
- ssr?: boolean
204
- lifeCycles?: lifeCyclesType
205
- plugins?: plugins
206
- fetch?: fetchType
319
+ options: OptionsType
207
320
  preFetch(apps: prefetchParamList): void
208
321
  router: Router // eslint-disable-line
209
322
  start(options?: OptionsType): void
@@ -269,10 +382,20 @@ declare module '@micro-app/types' {
269
382
  [appName: string]: (to: GuardLocation, from: GuardLocation) => void
270
383
  }
271
384
 
272
- type GlobalNormalGuard = ((appName: string, to: GuardLocation, from: GuardLocation) => void)
385
+ type GlobalNormalGuard = ((to: GuardLocation, from: GuardLocation, appName: string) => void)
273
386
 
274
387
  type RouterGuard = AccurateGuard | GlobalNormalGuard
275
388
 
389
+ type SetDefaultPageOptions = {
390
+ name: string,
391
+ path: string,
392
+ }
393
+
394
+ type AttachAllToURLParam = {
395
+ includeHiddenApp?: boolean,
396
+ includePreRender?: boolean,
397
+ }
398
+
276
399
  // Router API for developer
277
400
  interface Router {
278
401
  // current route of all apps
@@ -325,10 +448,37 @@ declare module '@micro-app/types' {
325
448
  * @param guard global hook for
326
449
  */
327
450
  afterEach(guard: RouterGuard): () => boolean
328
-
329
- setDefaultPage(appName: string, path: string): () => boolean
451
+ /**
452
+ * Add defaultPage to control the first rendered page
453
+ * @param options SetDefaultPageOptions
454
+ */
455
+ setDefaultPage(options: SetDefaultPageOptions): () => boolean
456
+ /**
457
+ * Clear data of defaultPage that set by setDefaultPage
458
+ */
330
459
  removeDefaultPage(appName: string): boolean
331
- getDefaultPage(key: PropertyKey): string | undefined
460
+ /**
461
+ * Get defaultPage that set by setDefaultPage
462
+ */
463
+ getDefaultPage(key: PropertyKey): string | void
464
+ /**
465
+ * Attach specified active app router info to browser url
466
+ */
467
+ attachToURL(appName: string): void
468
+ /**
469
+ * Attach all active app router info to browser url
470
+ */
471
+ attachAllToURL(options: AttachAllToURLParam): void
472
+ /**
473
+ * Record base app router, let child app control base app navigation
474
+ * It is global data
475
+ * @param baseRouter router instance of base app
476
+ */
477
+ setBaseAppRouter(baseRouter: unknown): void
478
+ /**
479
+ * get baseRouter from cache
480
+ */
481
+ getBaseAppRouter(): unknown
332
482
  }
333
483
 
334
484
  // result of add/remove microApp path on browser url