@micro-zoe/micro-app 1.0.0-alpha.1 → 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.1",
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,21 +7,57 @@ 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, clearEventSource: 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
25
61
  }
26
62
 
27
63
  interface SandBoxAdapter {
@@ -38,68 +74,93 @@ declare module '@micro-app/types' {
38
74
  // injectReactHRMProperty (): void
39
75
  }
40
76
 
41
- type sourceLinkInfo = {
42
- code: string // code
43
- placeholder?: Comment | null // placeholder comment
44
- 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
+ }>
45
85
  }
46
86
 
47
- type sourceScriptInfo = {
48
- code: string // code
49
- isExternal: boolean // external script
50
- isDynamic: boolean // dynamic create script
51
- async: boolean // async script
52
- defer: boolean // defer script
53
- module: boolean // module type script
54
- isGlobal?: boolean // share js to global
55
- code2Function?: Function // code to Function
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
+ }>
56
101
  }
57
102
 
58
- interface sourceType {
59
- html?: HTMLElement
60
- links: Map<string, sourceLinkInfo>
61
- scripts: Map<string, sourceScriptInfo>
103
+ type sourceType = {
104
+ html: HTMLElement | null, // html address
105
+ links: Set<string>, // style/link address list
106
+ scripts: Set<string>, // script address list
107
+ }
108
+
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
119
+ }
120
+
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
62
126
  }
63
127
 
64
128
  // app instance
65
- interface AppInterface {
66
- isPrefetch: boolean // whether prefetch app, default is false
67
- prefetchResolve: (() => void) | null // prefetch callback
129
+ interface AppInterface extends Pick<ParentNode, 'querySelector' | 'querySelectorAll'> {
130
+ source: sourceType // source list
131
+ sandBox: SandBoxInterface | null // sandbox
68
132
  name: string // app name
69
133
  url: string // app url
70
- ssrUrl: string // html path in ssr mode
71
- container: HTMLElement | ShadowRoot | null // container maybe null, micro-app, shadowRoot, DIV(keep-alive)
72
- inline: boolean // whether js runs in inline script mode, default is false
73
134
  scopecss: boolean // whether use css scoped, default is true
74
135
  useSandbox: boolean // whether use js sandbox, default is true
75
- useMemoryRouter: boolean // whether use memoryRouter, default is true
76
- baseroute: string // route prefix, default is ''
77
- keepRouteState: boolean // keep route state when unmount, default is false
78
- source: sourceType // sources of css, js, html
79
- 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)
80
140
  umdMode: boolean // is umd mode
81
- 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
82
149
 
83
150
  // Load resources
84
151
  loadSourceCode (): void
85
152
 
86
153
  // resource is loaded
87
- onLoad (html: HTMLElement): void
154
+ onLoad (html: HTMLElement, defaultPage?: string, disablePatchRequest?: boolean): void
88
155
 
89
156
  // Error loading HTML
90
157
  onLoadError (e: Error): void
91
158
 
92
159
  // mount app
93
- mount (
94
- container?: HTMLElement | ShadowRoot,
95
- inline?: boolean,
96
- baseroute?: string,
97
- keepRouteState?: boolean,
98
- defaultPage?: string,
99
- ): void
160
+ mount (mountParams: MountParam): void
100
161
 
101
162
  // unmount app
102
- unmount (destroy: boolean, unmountcb?: CallableFunction): void
163
+ unmount (unmountParam: UnmountParam): void
103
164
 
104
165
  // app rendering error
105
166
  onerror (e: Error): void
@@ -113,7 +174,7 @@ declare module '@micro-app/types' {
113
174
  actionsForCompletelyDestroy (): void
114
175
 
115
176
  // hidden app when disconnectedCallback with keep-alive
116
- hiddenKeepAliveApp (): void
177
+ hiddenKeepAliveApp (callback?: CallableFunction): void
117
178
 
118
179
  // show app when connectedCallback with keep-alive
119
180
  showKeepAliveApp (container: HTMLElement | ShadowRoot): void
@@ -133,13 +194,20 @@ declare module '@micro-app/types' {
133
194
  attributeChangedCallback (a: 'name' | 'url', o: string, n: string): void
134
195
  }
135
196
 
136
- type prefetchParam = {
197
+ interface prefetchParam {
137
198
  name: string,
138
199
  url: string,
200
+ // old config 👇
139
201
  disableScopecss?: boolean
140
202
  disableSandbox?: boolean
141
- disableMemoryRouter?: boolean
142
- 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
143
211
  }
144
212
 
145
213
  // prefetch params
@@ -147,13 +215,18 @@ declare module '@micro-app/types' {
147
215
 
148
216
  // lifeCycles
149
217
  interface lifeCyclesType {
150
- created?(e?: CustomEvent): void
151
- beforemount?(e?: CustomEvent): void
152
- mounted?(e?: CustomEvent): void
153
- unmount?(e?: CustomEvent): void
154
- 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
155
226
  }
156
227
 
228
+ type AssetsChecker = (url: string) => boolean;
229
+
157
230
  type plugins = {
158
231
  // global plugin
159
232
  global?: Array<{
@@ -161,10 +234,16 @@ declare module '@micro-app/types' {
161
234
  scopeProperties?: Array<PropertyKey>
162
235
  // Properties that can be escape to rawWindow
163
236
  escapeProperties?: Array<PropertyKey>
237
+ // Exclude JS or CSS
238
+ excludeChecker?: AssetsChecker
239
+ // Ignore JS or CSS
240
+ ignoreChecker?: AssetsChecker
164
241
  // options for plugin as the third parameter of loader
165
- options?: unknown
242
+ options?: Record<string, unknown>
166
243
  // handle function
167
- 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
168
247
  }>
169
248
 
170
249
  // plugin for special app
@@ -174,14 +253,25 @@ declare module '@micro-app/types' {
174
253
  scopeProperties?: Array<PropertyKey>
175
254
  // Properties that can be escape to rawWindow
176
255
  escapeProperties?: Array<PropertyKey>
256
+ // Exclude JS or CSS
257
+ excludeChecker?: AssetsChecker
258
+ // Ignore JS or CSS
259
+ ignoreChecker?: AssetsChecker
177
260
  // options for plugin as the third parameter of loader
178
- options?: unknown
261
+ options?: Record<string, unknown>
179
262
  // handle function
180
- 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
181
266
  }>
182
267
  }
183
268
  }
184
269
 
270
+ type GetActiveAppsParam = {
271
+ excludeHiddenApp?: boolean,
272
+ excludePreRender?: boolean,
273
+ }
274
+
185
275
  type fetchType = (url: string, options: Record<string, unknown>, appName: string | null) => Promise<string>
186
276
 
187
277
  type globalAssetsType = {
@@ -189,35 +279,44 @@ declare module '@micro-app/types' {
189
279
  css?: string[],
190
280
  }
191
281
 
192
- type OptionsType = {
193
- tagName?: string
282
+ interface MicroAppConfig {
194
283
  shadowDOM?: boolean
195
284
  destroy?: boolean
285
+ destory?: boolean
196
286
  inline?: boolean
287
+ // old config 👇
197
288
  disableScopecss?: boolean
198
289
  disableSandbox?: boolean
199
- 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
200
300
  ssr?: boolean
301
+ fiber?: boolean
302
+ prefetchLevel?: number
303
+ prefetchDelay?: number
304
+ }
305
+
306
+ interface OptionsType extends MicroAppConfig {
307
+ tagName?: string
201
308
  lifeCycles?: lifeCyclesType
202
309
  preFetchApps?: prefetchParamList
203
310
  plugins?: plugins
204
311
  fetch?: fetchType
205
312
  globalAssets?: globalAssetsType,
313
+ excludeAssetFilter?: (assetUrl: string) => boolean
206
314
  }
207
315
 
208
316
  // MicroApp config
209
- interface MicroAppConfigType {
317
+ interface MicroAppBaseType {
210
318
  tagName: string
211
- shadowDOM?: boolean
212
- destroy?: boolean
213
- inline?: boolean
214
- disableScopecss?: boolean
215
- disableSandbox?: boolean
216
- disableMemoryRouter?: boolean
217
- ssr?: boolean
218
- lifeCycles?: lifeCyclesType
219
- plugins?: plugins
220
- fetch?: fetchType
319
+ options: OptionsType
221
320
  preFetch(apps: prefetchParamList): void
222
321
  router: Router // eslint-disable-line
223
322
  start(options?: OptionsType): void
@@ -283,10 +382,20 @@ declare module '@micro-app/types' {
283
382
  [appName: string]: (to: GuardLocation, from: GuardLocation) => void
284
383
  }
285
384
 
286
- type GlobalNormalGuard = ((appName: string, to: GuardLocation, from: GuardLocation) => void)
385
+ type GlobalNormalGuard = ((to: GuardLocation, from: GuardLocation, appName: string) => void)
287
386
 
288
387
  type RouterGuard = AccurateGuard | GlobalNormalGuard
289
388
 
389
+ type SetDefaultPageOptions = {
390
+ name: string,
391
+ path: string,
392
+ }
393
+
394
+ type AttachAllToURLParam = {
395
+ includeHiddenApp?: boolean,
396
+ includePreRender?: boolean,
397
+ }
398
+
290
399
  // Router API for developer
291
400
  interface Router {
292
401
  // current route of all apps
@@ -339,10 +448,37 @@ declare module '@micro-app/types' {
339
448
  * @param guard global hook for
340
449
  */
341
450
  afterEach(guard: RouterGuard): () => boolean
342
-
343
- 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
+ */
344
459
  removeDefaultPage(appName: string): boolean
345
- 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
346
482
  }
347
483
 
348
484
  // result of add/remove microApp path on browser url