@blueking/bk-weweb 0.0.35 → 0.0.36-beta.16

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/dist/index.d.mts CHANGED
@@ -1,27 +1,34 @@
1
+ type ExecuteResult = Comment | HTMLScriptElement | undefined;
2
+ /** Script脚本实例类 */
1
3
  declare class Script {
2
4
  async: boolean;
3
5
  code: string;
4
6
  defer: boolean;
5
- exportInstance?: any;
7
+ exportInstance?: unknown;
6
8
  fromHtml: boolean;
9
+ blobUrl?: string;
7
10
  initial: boolean;
8
11
  isModule: boolean;
9
12
  scoped: boolean;
10
13
  url: string | undefined;
11
14
  constructor({ async, code, defer, fromHtml, initial, isModule, url }: IScriptOption);
12
- /**
13
- * @param app 应用
14
- * @param needReplaceScriptElement 是否需要替换script标签
15
- * @returns 返回执行后的script标签或注释
16
- */
17
- executeCode(app: BaseModel, needReplaceScriptElement?: boolean): Promise<Comment | HTMLScriptElement | undefined>;
15
+ /** 执行脚本代码 */
16
+ executeCode(app: BaseModel, needReplaceScriptElement?: boolean): Promise<ExecuteResult>;
17
+ /** 内存脚本执行 */
18
18
  executeMemoryScript(app: BaseModel, scopedCode: string): void;
19
+ /** 脚本标签执行 */
19
20
  executeSourceScript(scriptElement: HTMLScriptElement, scopedCode: string): void;
21
+ /** 获取脚本内容 */
20
22
  getCode(app?: BaseModel): Promise<string>;
21
23
  setCode(code: string): void;
24
+ /** 转换脚本内容 */
22
25
  transformCode(app: BaseModel): string;
23
26
  }
24
27
 
28
+ type PackRuleType = CSSMediaRule | CSSSupportsRule;
29
+ /**
30
+ * 样式处理类
31
+ */
25
32
  declare class Style {
26
33
  code: string;
27
34
  fromHtml: boolean;
@@ -33,90 +40,321 @@ declare class Style {
33
40
  url: string | undefined;
34
41
  constructor({ code, fromHtml, initial, prefetch, preload, url }: IStyleOption);
35
42
  /**
36
- * @param styleElement 样式node
43
+ * 通用样式作用域处理
44
+ * @param styleElement 样式元素
37
45
  * @param app 应用实例
38
46
  */
39
47
  commonScoped(styleElement: HTMLStyleElement, app: BaseModel): void;
48
+ /**
49
+ * 创建样式元素
50
+ */
40
51
  createStyleElement(): HTMLStyleElement;
41
52
  /**
53
+ * 执行样式代码
42
54
  * @param app 应用实例
43
55
  * @returns 返回执行后的style标签
44
56
  */
45
57
  executeCode(app: BaseModel): Promise<HTMLStyleElement>;
58
+ /**
59
+ * 获取样式代码
60
+ */
46
61
  getCode(app?: BaseModel): Promise<string>;
62
+ /**
63
+ * 检查并链接基础应用样式
64
+ * @param styleElement 样式元素
65
+ * @param app 应用实例
66
+ * @returns 是否已链接基础样式
67
+ */
47
68
  linkedBaseStyle(styleElement: HTMLStyleElement, app: BaseModel): boolean;
48
- resetPackRule(rule: CSSMediaRule | CSSSupportsRule, prefix: string, packName: string): string;
69
+ /**
70
+ * 重置包装规则
71
+ */
72
+ resetPackRule(rule: PackRuleType, prefix: string, packName: string): string;
73
+ /**
74
+ * 重置URL地址
75
+ */
49
76
  resetUrlHost(cssText: string, uri: string, linkPath?: string): string;
77
+ /**
78
+ * css rule 处理
79
+ */
50
80
  scopeRule(rules: CSSRule[], cssPrefix: string): string;
81
+ /**
82
+ * style rule 处理
83
+ */
51
84
  scopeStyleRule(rule: CSSStyleRule, prefix: string): string;
85
+ /**
86
+ * link style 处理
87
+ */
52
88
  scopedLinkCSS(app: BaseModel, linkElement: HTMLLinkElement): HTMLStyleElement;
89
+ /**
90
+ * 隔离 style
91
+ */
53
92
  scopedStyleCSS(app: BaseModel, styleElement: HTMLStyleElement): HTMLStyleElement;
93
+ /**
94
+ * 应用隔离 style
95
+ */
96
+ private applyScopedCSS;
97
+ private applyUnscopedCSS;
98
+ /**
99
+ * 处理ShadowRoot中的字体
100
+ */
101
+ private handleShadowRootFonts;
102
+ private getCodeFromAppSource;
103
+ private getCodeFromCache;
104
+ private fetchCodeFromRemote;
105
+ private clearStyleElement;
106
+ private buildBaseURI;
107
+ private handleExistingCode;
108
+ /**
109
+ * 处理href属性
110
+ */
111
+ private handleHrefAttribute;
112
+ /**
113
+ * 处理缺失的href
114
+ */
115
+ private handleMissingHref;
116
+ private processExistingContent;
117
+ private observeContentChanges;
54
118
  }
55
119
 
120
+ /**
121
+ * 应用缓存管理器
122
+ * @description 负责管理所有微应用实例的缓存、资源共享和状态跟踪
123
+ */
56
124
  declare class AppCache {
57
- private baseSource;
58
- private cache;
125
+ /** 基础资源源,用于主应用共享资源包 */
126
+ private readonly baseSource;
127
+ /** 应用实例缓存映射表 */
128
+ private readonly cache;
129
+ /**
130
+ * 构造函数
131
+ * @description 初始化应用缓存管理器
132
+ */
59
133
  constructor();
60
- deleteApp(url: string): void;
134
+ /**
135
+ * 设置应用实例到缓存
136
+ * @description 将应用实例添加到缓存中,使用应用的缓存键作为标识
137
+ * @param app - 要缓存的应用实例
138
+ */
139
+ setApp(app: BaseModel): void;
140
+ /**
141
+ * 获取缓存的应用实例
142
+ * @description 根据名称或ID获取已缓存的应用实例
143
+ * @param name - 应用名称或ID,为空时返回 undefined
144
+ * @returns BaseModel | undefined - 应用实例或 undefined
145
+ */
61
146
  getApp(name?: null | string): BaseModel | undefined;
62
- getBaseAppStyle(urlOrCode: string): Style | undefined;
147
+ /**
148
+ * 删除缓存的应用实例
149
+ * @description 从缓存中移除指定URL的应用实例
150
+ * @param url - 要删除的应用URL标识
151
+ */
152
+ deleteApp(url: string): void;
153
+ /**
154
+ * 获取缓存的HTML内容
155
+ * @description 根据URL获取已缓存的HTML内容
156
+ * @param url - 应用的URL
157
+ * @returns string - HTML内容,未找到时返回空字符串
158
+ */
63
159
  getCacheHtml(url: string): string;
64
- getCacheScript(url: string): Script | undefined;
65
- getCacheStyle(url: string): Style | undefined;
66
- setApp(app: BaseModel): void;
160
+ /**
161
+ * 设置基础应用脚本
162
+ * @description 将脚本添加到基础资源源中,供多个应用共享
163
+ * @param url - 脚本的URL
164
+ * @param script - 脚本实例
165
+ */
67
166
  setBaseAppScript(url: string, script: Script): void;
167
+ /**
168
+ * 获取缓存的脚本资源
169
+ * @description 从基础资源源或应用缓存中获取脚本资源
170
+ * @param url - 脚本的URL
171
+ * @returns Script | undefined - 脚本实例或 undefined
172
+ */
173
+ getCacheScript(url: string): Script | undefined;
174
+ /**
175
+ * 设置基础应用样式
176
+ * @description 将样式添加到基础资源源中,供多个应用共享
177
+ * @param url - 样式的URL
178
+ * @param style - 样式实例
179
+ */
68
180
  setBaseAppStyle(url: string, style: Style): void;
181
+ /**
182
+ * 获取基础应用样式
183
+ * @description 从基础资源源中获取样式资源
184
+ * @param urlOrCode - 样式的URL或代码
185
+ * @returns Style | undefined - 样式实例或 undefined
186
+ */
187
+ getBaseAppStyle(urlOrCode: string): Style | undefined;
188
+ /**
189
+ * 获取缓存的样式资源
190
+ * @description 从基础资源源或应用缓存中获取样式资源
191
+ * @param url - 样式的URL
192
+ * @returns Style | undefined - 样式实例或 undefined
193
+ */
194
+ getCacheStyle(url: string): Style | undefined;
195
+ /**
196
+ * 检查是否存在活跃的应用
197
+ * @description 判断当前是否有处于非卸载状态的应用实例
198
+ * @returns boolean - 存在活跃应用时返回 true
199
+ */
69
200
  get hasActiveApp(): boolean;
70
201
  }
71
202
 
72
- declare enum AppState {
73
- ACTIVATED = "ACTIVATED",// 激活
74
- DEACTIVATED = "DEACTIVATED",// 未激活
75
- ERROR = "ERROR",// Error
76
- LOADED = "LOADED",// 加载完成
77
- LOADING = "LOADING",// 加载中
78
- MOUNTED = "MOUNTED",// 挂载完成
79
- MOUNTING = "MOUNTING",// 挂载中
80
- UNMOUNT = "UNMOUNT",// 卸载
81
- UNSET = "UNSET"
82
- }
203
+ /**
204
+ * 应用状态常量
205
+ * 定义微前端应用的生命周期状态
206
+ */
207
+ declare const AppState: {
208
+ /** 未设置状态 */
209
+ readonly UNSET: "UNSET";
210
+ /** 加载中 */
211
+ readonly LOADING: "LOADING";
212
+ /** 加载完成 */
213
+ readonly LOADED: "LOADED";
214
+ /** 挂载中 */
215
+ readonly MOUNTING: "MOUNTING";
216
+ /** 挂载完成 */
217
+ readonly MOUNTED: "MOUNTED";
218
+ /** 激活状态 */
219
+ readonly ACTIVATED: "ACTIVATED";
220
+ /** 未激活状态 */
221
+ readonly DEACTIVATED: "DEACTIVATED";
222
+ /** 卸载状态 */
223
+ readonly UNMOUNT: "UNMOUNT";
224
+ /** 错误状态 */
225
+ readonly ERROR: "ERROR";
226
+ };
227
+ type KeyOfAppState = keyof typeof AppState;
228
+ type ValueOfAppState = (typeof AppState)[KeyOfAppState];
83
229
 
230
+ /**
231
+ * BK WEWEB 支持的自定义属性枚举
232
+ * @description 定义了 bk-weweb 自定义元素支持的所有属性配置
233
+ */
84
234
  declare enum WewebCustomAttrs {
85
- data = "data",// 传递给子应用的数据
86
- id = "id",// 应用id
87
- keepAlive = "keepAlive",// 是否缓存
88
- mode = "mode",// 模式
89
- scopeCss = "scopeCss",// 是否开启css隔离
90
- scopeJs = "scopeJs",// 是否开启js隔离
91
- scopeLocation = "scopeLocation",// 是否开启location隔离
92
- setShodowDom = "setShodowDom",// 是否开启shadowDom
93
- showSourceCode = "showSourceCode",// 是否显示源码
235
+ /** 传递给子应用的数据 */
236
+ data = "data",
237
+ /** 应用唯一标识符 */
238
+ id = "id",
239
+ /** 是否启用缓存模式 */
240
+ keepAlive = "keepAlive",
241
+ /** 应用运行模式 */
242
+ mode = "mode",
243
+ /** 是否开启 CSS 样式隔离 */
244
+ scopeCss = "scopeCss",
245
+ /** 是否开启 JavaScript 隔离 */
246
+ scopeJs = "scopeJs",
247
+ /** 是否开启 location 路由隔离 */
248
+ scopeLocation = "scopeLocation",
249
+ /** 是否启用 Shadow DOM */
250
+ setShadowDom = "setShadowDom",
251
+ /** @deprecated 请使用 setShadowDom 代替 */
252
+ setShodowDom = "setShodowDom",
253
+ /** 是否显示源码 */
254
+ showSourceCode = "showSourceCode",
255
+ /** 应用资源 URL */
94
256
  url = "url"
95
257
  }
96
258
 
259
+ type FakeWindow = Window & Record<string, unknown>;
260
+
261
+ /**
262
+ * 微前端沙箱
263
+ * @description 提供应用间的环境隔离,防止全局变量污染,支持多应用并存运行
264
+ */
97
265
  declare class SandBox {
98
- app: BaseModel;
266
+ readonly app: BaseModel;
267
+ /** 沙箱激活状态标识 */
99
268
  private active;
100
- private inRawWindowKeySet;
269
+ /** 记录在原始 window 上新增的属性键集合 */
270
+ private readonly inRawWindowKeySet;
271
+ /** 重置文档和 body 事件的函数 */
101
272
  private resetDocumentAndBodyEvent?;
102
- private resetWindowFunction;
103
- private sameRawWindowKeySet;
104
- fakeWindow: Window & IInjectWindowAttrs;
273
+ /** 重置 window 函数的方法 */
274
+ private readonly resetWindowFunction;
275
+ /** 记录与原始 window 相同的属性键集合 */
276
+ private readonly sameRawWindowKeySet;
277
+ /** 伪造的 window 对象 */
278
+ readonly fakeWindow: FakeWindow & IInjectWindowAttrs;
279
+ /** 代理的 document 对象 */
105
280
  proxyDocument: any;
106
- proxyWindow: WindowProxy & IInjectWindowAttrs;
281
+ /** 代理的 window 对象 */
282
+ readonly proxyWindow: WindowProxy & IInjectWindowAttrs;
283
+ /** 原始 document 对象 */
107
284
  rawDocument: Record<string, any>;
108
- rawWindow: Window;
109
- windowSymbolKey: keyof Window;
285
+ /** 原始 window 对象 */
286
+ readonly rawWindow: Window;
287
+ /** 在 window 上的唯一标识键 */
288
+ readonly windowSymbolKey: keyof Window;
289
+ /** 初始化沙箱环境 */
110
290
  constructor(app: BaseModel);
291
+ /** 处理代理对象的 get 操作 */
292
+ private handleProxyGet;
293
+ /**
294
+ * 处理代理对象的 set 操作
295
+ * @description 统一处理代理对象属性设置的复杂逻辑
296
+ * @param target - 目标对象
297
+ * @param key - 属性键
298
+ * @param value - 属性值
299
+ * @param rawWindow - 原始 window 对象
300
+ * @returns boolean - 设置是否成功
301
+ * @private
302
+ */
303
+ private handleProxySet;
304
+ /**
305
+ * 判断是否应该使用 iframe 的 location
306
+ * @description 检查是否在 iframe 模式下访问 location 相关属性
307
+ * @param key - 属性键
308
+ * @returns boolean - 是否使用 iframe location
309
+ * @private
310
+ */
311
+ private shouldUseIframeLocation;
312
+ /**
313
+ * 创建 getComputedStyle 方法的代理
314
+ * @description 为 getComputedStyle 方法创建安全的代理实现
315
+ * @param rawWindow - 原始 window 对象
316
+ * @returns Function - 代理后的 getComputedStyle 方法
317
+ * @private
318
+ */
319
+ private createGetComputedStyleProxy;
320
+ /**
321
+ * 判断是否应该在目标对象上设置属性
322
+ * @description 检查属性设置的逻辑条件
323
+ * @param target - 目标对象
324
+ * @param key - 属性键
325
+ * @param rawWindow - 原始 window 对象
326
+ * @returns boolean - 是否在目标对象上设置
327
+ * @private
328
+ */
329
+ private shouldSetOnTarget;
330
+ /**
331
+ * 在目标对象上设置属性
332
+ * @description 安全地在目标对象上设置属性,保持描述符特性
333
+ * @param target - 目标对象
334
+ * @param key - 属性键
335
+ * @param value - 属性值
336
+ * @param rawWindow - 原始 window 对象
337
+ * @private
338
+ */
339
+ private setPropertyOnTarget;
111
340
  /**
112
- *
113
- * @param data data for sandbox
114
- * @description active hook for sandbox
341
+ * 处理白名单属性
342
+ * @description 处理需要在原始 window 上设置的白名单属性
343
+ * @param key - 属性键
344
+ * @param value - 属性值
345
+ * @param rawWindow - 原始 window 对象
346
+ * @private
347
+ */
348
+ private handleWhiteListProperty;
349
+ /**
350
+ * 激活沙箱
351
+ * @description 启动沙箱环境,初始化代理对象和事件处理
352
+ * @param data - 传递给沙箱的数据(可选)
115
353
  */
116
354
  activated(data?: Record<string, unknown>): void;
117
355
  /**
118
- *
119
- * @description decativated hook for sandbox
356
+ * 停用沙箱
357
+ * @description 关闭沙箱环境,清理所有副作用和修改
120
358
  */
121
359
  deactivated(): void;
122
360
  }
@@ -124,9 +362,10 @@ declare class SandBox {
124
362
  type SourceFuncType = () => Promise<string[]>;
125
363
  type SourceType = SourceFuncType | string[];
126
364
 
365
+ /** BK-WEWEB 微应用模式类 */
127
366
  declare class MicroAppModel implements BaseModel {
128
367
  private state;
129
- container?: HTMLElement | ShadowRoot;
368
+ container?: ContainerType;
130
369
  data: Record<string, unknown>;
131
370
  iframe: HTMLIFrameElement | null;
132
371
  initSource: SourceType;
@@ -142,22 +381,57 @@ declare class MicroAppModel implements BaseModel {
142
381
  showSourceCode: boolean;
143
382
  source?: EntrySource;
144
383
  url: string;
145
- constructor(props: IAppModleProps);
146
- activated(container: HTMLElement | ShadowRoot, callback?: (app: BaseModel) => void): void;
384
+ constructor(props: IAppModelProps);
385
+ /** 激活微应用 */
386
+ activated<T = unknown>(container: ContainerType, callback?: CallbackFunction<T>): void;
387
+ /** 创建隔离iframe */
147
388
  createIframe(): Promise<HTMLIFrameElement>;
389
+ /** 停用微应用 */
148
390
  deactivated(): void;
391
+ /** 初始化ShadowRoot容器 */
149
392
  initShadowRootContainer(): void;
150
- mount(container?: HTMLElement | ShadowRoot, callback?: (app: BaseModel) => void): void;
393
+ /** 挂载微应用 */
394
+ mount<T = unknown>(container?: ContainerType, callback?: CallbackFunction<T>): void;
395
+ /** 错误处理 */
151
396
  onError(): void;
397
+ /** 挂载处理 */
152
398
  onMount(): void;
399
+ /** 注册运行中的微应用 */
153
400
  registerRunningApp(): void;
401
+ /** 启动微应用 */
154
402
  start(): Promise<void>;
403
+ /** 卸载微应用 */
155
404
  unmount(needDestroy?: boolean): void;
156
405
  get appCacheKey(): string;
157
- get status(): AppState;
158
- set status(v: AppState);
406
+ get status(): ValueOfAppState;
407
+ set status(value: ValueOfAppState);
408
+ /** 初始化沙盒 */
409
+ private initializeSandBox;
410
+ /** 设置容器属性 */
411
+ private setContainerAttribute;
412
+ /** 转移节点到新容器 */
413
+ private transferNodes;
414
+ /** 设置节点属性 */
415
+ private setupNodeProperties;
416
+ /** 创建iframe元素 */
417
+ private createIframeElement;
418
+ /** 构建iframe源地址 */
419
+ private buildIframeSrc;
420
+ /** 检查是否为Chrome浏览器 */
421
+ private isChromeUserAgent;
422
+ /** 处理非Chrome浏览器iframe */
423
+ private handleNonChromeIframe;
424
+ /** 渲染应用内容 */
425
+ private renderAppContent;
426
+ /** 检查是否需要重新加载 */
427
+ private needsReload;
159
428
  }
160
429
 
430
+ interface CollectResult {
431
+ replace: Comment | Element;
432
+ style?: Style;
433
+ script?: Script;
434
+ }
161
435
  declare class EntrySource {
162
436
  url: string;
163
437
  html: HTMLElement | null;
@@ -165,82 +439,210 @@ declare class EntrySource {
165
439
  scripts: Map<string, Script>;
166
440
  styles: Map<string, Style>;
167
441
  constructor(url: string);
168
- collectLink(link: HTMLLinkElement, parent: Node, needReplaceELement?: boolean): {
442
+ /** 收集链接元素 */
443
+ collectLink: (link: HTMLLinkElement, parent: Node, needReplaceElement?: boolean) => {
169
444
  replace: Comment | Element;
170
445
  style?: Style;
171
446
  };
172
- collectScript(script: HTMLScriptElement, parent: Node, needReplaceELement?: boolean): {
173
- replace: Comment | Element;
174
- script?: Script;
175
- } | undefined;
176
- collectScriptAndStyle(parent: HTMLElement): void;
177
- getScript(url: string): Script | undefined;
178
- getStyle(urlOrCode: string): Style | undefined;
447
+ /** 收集脚本元素 */
448
+ collectScript: (script: HTMLScriptElement, parent: Node, needReplaceElement?: boolean) => CollectResult | undefined;
449
+ /** 收集样式和脚本 */
450
+ collectScriptAndStyle: (parent: HTMLElement) => void;
451
+ getScript: (url: string) => Script | undefined;
452
+ getStyle: (urlOrCode: string) => Style | undefined;
453
+ /** html entry */
179
454
  importEntry(app: BaseModel): Promise<void>;
455
+ /** 微应用入口 */
180
456
  importHtmlEntry(app: MicroAppModel): Promise<void>;
457
+ /** 微模块入口 */
181
458
  importInstanceEntry(app: BaseModel): Promise<void>;
182
- setScript(url: string, script: IScriptOption | Script): void;
183
- setStyle(url: string, style: Style): void;
459
+ setScript: (url: string, script: IScriptOption | Script) => void;
460
+ setStyle: (url: string, style: Style) => void;
461
+ /** 处理样式表链接 */
462
+ private handleStylesheetLink;
463
+ /**
464
+ * 处理图标链接
465
+ */
466
+ private handleIconLink;
467
+ /**
468
+ * 检查是否应该忽略脚本
469
+ */
470
+ private shouldIgnoreScript;
471
+ /**
472
+ * 处理被排除的脚本
473
+ */
474
+ private handleExcludedScript;
475
+ /**
476
+ * 处理外部脚本
477
+ */
478
+ private handleExternalScript;
479
+ /**
480
+ * 处理内联脚本
481
+ */
482
+ private handleInlineScript;
483
+ /**
484
+ * 处理链接元素
485
+ */
486
+ private processLinks;
487
+ /**
488
+ * 处理样式元素
489
+ */
490
+ private processStyles;
491
+ /**
492
+ * 处理脚本元素
493
+ */
494
+ private processScripts;
495
+ /**
496
+ * 处理Meta元素
497
+ */
498
+ private processMetas;
499
+ /**
500
+ * 处理图片元素
501
+ */
502
+ private processImages;
503
+ /**
504
+ * 加载初始资源
505
+ */
506
+ private loadInitialSources;
507
+ /**
508
+ * 获取HTML content
509
+ */
510
+ private fetchHtmlContent;
511
+ /**
512
+ * 创建顶层 root 元素
513
+ */
514
+ private createWrapElement;
515
+ /**
516
+ * 获取JS content
517
+ */
518
+ private fetchJsContent;
184
519
  }
185
520
 
186
- declare function fetchSource(url: string, options?: {}, app?: BaseModel): Promise<string>;
521
+ type FetchOptions = Record<string, unknown>;
522
+ /**
523
+ * 统一的资源获取方法,支持应用级别和全局级别的自定义fetch
524
+ * 优先级:应用级fetchSource > 全局fetchSource > 原生fetch
525
+ * @param url 要获取的资源URL
526
+ * @param options fetch请求选项,默认为空对象
527
+ * @param app 可选的应用实例,如果提供且有自定义fetchSource则优先使用
528
+ * @returns Promise<string> 返回资源内容的Promise,失败时返回空字符串
529
+ */
530
+ declare const fetchSource: (url: string, options?: FetchOptions, app?: BaseModel) => Promise<string>;
187
531
 
532
+ /** WEWEB运行模式 */
533
+ declare enum WewebMode {
534
+ /** 微应用模式 */
535
+ APP = "app",
536
+ /** 配置模式 */
537
+ CONFIG = "config",
538
+ /** 微模块模式 */
539
+ INSTANCE = "js"
540
+ }
541
+ /** 容器类型 */
542
+ type ContainerType = HTMLElement | ShadowRoot;
543
+ /** 通用回调函数类型 */
544
+ type CallbackFunction<T = unknown> = (instance: BaseModel, exportInstance?: T) => void;
545
+ /** 基础模型属性接口 */
188
546
  interface IBaseModelProps {
547
+ /** 运行模式 */
189
548
  [WewebCustomAttrs.mode]?: WewebMode;
549
+ /** 应用URL */
190
550
  [WewebCustomAttrs.url]: string;
191
- id?: null | string;
551
+ /** 应用ID */
552
+ id?: string | null;
553
+ /** 是否预加载 */
192
554
  isPreLoad?: boolean;
193
555
  }
194
- interface IAppModleProps extends IBaseModelProps {
556
+ /** 微应用模式属性配置 */
557
+ interface IAppModelProps extends IBaseModelProps {
558
+ /** 传递给子应用的数据 */
195
559
  [WewebCustomAttrs.data]?: Record<string, unknown>;
560
+ /** 是否缓存DOM */
196
561
  [WewebCustomAttrs.keepAlive]?: boolean;
562
+ /** 是否启用样式隔离 */
197
563
  [WewebCustomAttrs.scopeCss]?: boolean;
564
+ /** 是否使用沙盒隔离 */
198
565
  [WewebCustomAttrs.scopeJs]?: boolean;
566
+ /** 是否共享主应用路由 */
199
567
  [WewebCustomAttrs.scopeLocation]?: boolean;
200
- [WewebCustomAttrs.setShodowDom]?: boolean;
568
+ /** 是否使用 ShadowDOM */
569
+ [WewebCustomAttrs.setShadowDom]?: boolean;
570
+ /** 是否显示源码 */
201
571
  [WewebCustomAttrs.showSourceCode]?: boolean;
202
- container?: HTMLElement | ShadowRoot | null;
572
+ /** 容器元素 */
573
+ container?: ContainerType | null;
574
+ /** 初始化资源 */
203
575
  initSource?: SourceType;
204
576
  }
577
+ /** 微模块模式属性配置 */
205
578
  interface IJsModelProps extends IBaseModelProps {
579
+ /** 传递给模块的数据 */
206
580
  [WewebCustomAttrs.data]?: Record<string, unknown>;
581
+ /** 是否显示源码 */
207
582
  [WewebCustomAttrs.showSourceCode]?: boolean;
208
- container?: HTMLElement | ShadowRoot | null;
583
+ /** 容器元素 */
584
+ container?: ContainerType | null;
585
+ /** 初始化资源 */
209
586
  initSource?: SourceType;
587
+ /** 是否缓存DOM */
210
588
  keepAlive?: boolean;
589
+ /** 是否启用样式隔离 */
211
590
  scopeCss?: boolean;
591
+ /** 是否使用沙盒隔离 */
212
592
  scopeJs?: boolean;
213
593
  }
594
+ /** 基础模型接口 */
214
595
  interface BaseModel {
215
- activated<T>(container: HTMLElement | ShadowRoot, callback?: (instance: BaseModel, exportInstance?: T) => void): void;
216
- container?: HTMLElement | ShadowRoot;
217
- deactivated(): void;
218
- get appCacheKey(): string;
219
- get status(): AppState;
596
+ /** 应用缓存键 */
597
+ readonly appCacheKey: string;
598
+ /** 容器元素 */
599
+ container?: ContainerType;
600
+ /** 初始化资源 */
220
601
  initSource?: SourceType;
602
+ /** 是否为模块应用 */
221
603
  isModuleApp?: boolean;
604
+ /** 是否预加载 */
222
605
  isPreLoad: boolean;
606
+ /** 是否保持活跃 */
223
607
  keepAlive?: boolean;
224
- mount<T>(container?: HTMLElement | ShadowRoot, callback?: (instance: BaseModel, exportInstance?: T) => void): void;
608
+ /** 应用名称 */
225
609
  name: string;
226
- onError(): void;
227
- onMount(): void;
228
- registerRunningApp(): void;
610
+ /** 沙盒实例 */
229
611
  sandBox?: SandBox;
612
+ /** 是否启用样式隔离 */
230
613
  scopeCss?: boolean;
614
+ /** 是否使用JS隔离 */
231
615
  scopeJs: boolean;
232
- set status(v: AppState);
616
+ /** 是否显示源码 */
233
617
  showSourceCode?: boolean;
618
+ /** 入口资源 */
234
619
  source?: EntrySource;
235
- start(): Promise<void>;
236
- unmount(needDestroy?: boolean): void;
620
+ /** 应用URL */
237
621
  url: string;
622
+ /** 获取资源的函数 */
238
623
  fetchSource?: typeof fetchSource;
239
- }
240
- declare enum WewebMode {
241
- APP = "app",
242
- CONFIG = "config",
243
- INSTANCE = "js"
624
+ /** 传递给应用的数据 */
625
+ data: Record<string, unknown>;
626
+ /** 激活应用 */
627
+ activated<T = unknown>(container: ContainerType, callback?: CallbackFunction<T>): void;
628
+ /** 停用应用 */
629
+ deactivated(): void;
630
+ /** 挂载应用 */
631
+ mount<T = unknown>(container?: ContainerType, callback?: CallbackFunction<T>): void;
632
+ /** 错误处理 */
633
+ onError(): void;
634
+ /** 挂载处理 */
635
+ onMount(): void;
636
+ /** 注册运行中的应用 */
637
+ registerRunningApp(): void;
638
+ /** 启动应用 */
639
+ start(): Promise<void>;
640
+ /** 卸载应用 */
641
+ unmount(needDestroy?: boolean): void;
642
+ /** 获取应用状态 */
643
+ get status(): ValueOfAppState;
644
+ /** 设置应用状态 */
645
+ set status(value: ValueOfAppState);
244
646
  }
245
647
 
246
648
  declare global {
@@ -252,39 +654,70 @@ declare global {
252
654
  interface Node {
253
655
  __BK_WEWEB_APP_KEY__?: string;
254
656
  __KEEP_ALIVE__?: string;
255
- data?: any;
657
+ data?: unknown;
256
658
  }
257
659
  }
258
660
 
259
661
  /**
260
- * 接口定义:样式选项
662
+ * 资源加载相关类型定义
663
+ * @description 定义了样式、脚本等资源的配置选项和相关类型
664
+ */
665
+ /**
666
+ * 样式资源配置选项接口
667
+ * @description 定义样式文件的加载配置和属性信息
261
668
  */
262
669
  interface IStyleOption {
670
+ /** 样式代码内容 */
263
671
  code: string;
672
+ /** 是否来自 HTML 页面内联样式 */
264
673
  fromHtml: boolean;
674
+ /** 是否为初始样式 */
265
675
  initial?: boolean;
676
+ /** 是否预取样式资源 */
266
677
  prefetch?: boolean;
678
+ /** 是否预加载样式资源 */
267
679
  preload?: boolean;
680
+ /** 样式文件的 URL 地址 */
268
681
  url?: string;
269
682
  }
683
+ /**
684
+ * 脚本资源配置选项接口
685
+ * @description 定义脚本文件的加载配置和属性信息
686
+ */
270
687
  interface IScriptOption {
688
+ /** 是否为异步加载脚本 */
271
689
  async: boolean;
690
+ /** 脚本代码内容 */
272
691
  code: string;
692
+ /** 是否为延迟执行脚本 */
273
693
  defer: boolean;
694
+ /** 是否从 HTML 页面中提取的脚本 */
274
695
  fromHtml: boolean;
696
+ /** 是否为初始脚本 */
275
697
  initial?: boolean;
698
+ /** 是否为 ES6 模块类型脚本 */
276
699
  isModule: boolean;
700
+ /** 脚本文件的 URL 地址 */
277
701
  url?: string;
278
702
  }
279
703
 
280
704
  /**
281
- * Interface for injecting window attributes.
705
+ * 沙箱环境配置类型定义
706
+ */
707
+ /**
708
+ * 注入到子应用 window 对象的属性接口
709
+ * @description 定义了 BK WEWEB 平台注入到子应用环境中的必要属性
282
710
  */
283
711
  interface IInjectWindowAttrs {
712
+ /** BK WEWEB平台的应用唯一标识 */
284
713
  __BK_WEWEB_APP_KEY__: string;
714
+ /** BK WEWEB的附加数据 */
285
715
  __BK_WEWEB_DATA__: Record<string, unknown>;
716
+ /** 标识页面是否由BK WEWEB驱动 */
286
717
  __POWERED_BY_BK_WEWEB__: boolean;
718
+ /** 原始 document 对象的引用 */
287
719
  rawDocument: Document;
720
+ /** 原始 window 对象的引用 */
288
721
  rawWindow: Window;
289
722
  }
290
723
 
@@ -292,18 +725,32 @@ type FetchSourceType = (url: string, options: Record<string, unknown>) => Promis
292
725
  interface IStartOption {
293
726
  collectBaseSource?: boolean;
294
727
  fetchSource?: FetchSourceType;
295
- webcomponentTag?: string;
728
+ webComponentTag?: string;
296
729
  }
297
730
 
731
+ /**
732
+ * 激活指定应用
733
+ * @description 激活已失活的应用或挂载新应用,支持 keep-alive 模式的应用状态恢复
734
+ * @template T - 导出实例的类型
735
+ * @param appKey - 应用的唯一标识符
736
+ * @param container - 挂载容器,HTMLElement 或 ShadowRoot
737
+ * @param callback - 激活完成后的回调函数(可选)
738
+ */
298
739
  declare function activated<T>(appKey: string, container: HTMLElement | ShadowRoot, callback?: <M extends BaseModel>(instance: M, exportInstance?: T) => void): void;
299
740
 
741
+ /**
742
+ * deactivated 指定应用
743
+ * @description 将指定应用设置为失活状态,根据 keep-alive 配置决定
744
+ */
300
745
  declare function deactivated(appKey: string): void;
301
746
 
747
+ /** BK-WEWEB 微模块模式类 */
302
748
  declare class MicroInstanceModel implements BaseModel {
303
749
  private state;
304
750
  appCacheKey: string;
305
- container?: HTMLElement | ShadowRoot;
751
+ container?: ContainerType;
306
752
  data: Record<string, unknown>;
753
+ fetchSource?: typeof fetchSource;
307
754
  initSource: SourceType;
308
755
  isPreLoad: boolean;
309
756
  keepAlive: boolean;
@@ -314,53 +761,148 @@ declare class MicroInstanceModel implements BaseModel {
314
761
  showSourceCode: boolean;
315
762
  source?: EntrySource;
316
763
  url: string;
317
- fetchSource?: typeof fetchSource;
318
764
  constructor(props: IJsModelProps & {
319
765
  fetchSource?: typeof fetchSource;
320
766
  });
321
- activated<T>(container: HTMLElement | ShadowRoot, callback?: (instance: BaseModel, exportInstance?: T) => void): void;
767
+ /** 激活微模块 */
768
+ activated<T = unknown>(container: ContainerType, callback?: CallbackFunction<T>): void;
769
+ /** 停用微模块 */
322
770
  deactivated(): void;
323
- mount<T>(container?: HTMLElement | ShadowRoot, callback?: (instance: MicroInstanceModel, exportInstance: T) => void): void;
771
+ /** 挂载微模块 */
772
+ mount<T = unknown>(container?: ContainerType, callback?: CallbackFunction<T>): void;
773
+ /** 错误处理 */
324
774
  onError(): void;
775
+ /** 挂载处理 */
325
776
  onMount(): void;
777
+ /** 注册运行中的应用 */
326
778
  registerRunningApp(): void;
779
+ /** 启动微模块 */
327
780
  start(): Promise<void>;
781
+ /** 卸载微模块 */
328
782
  unmount(needDestroy?: boolean): void;
329
- set status(v: AppState);
330
- get status(): AppState;
783
+ /** 初始化ShadowRoot容器 */
784
+ initShadowRootContainer(): void;
785
+ set status(value: ValueOfAppState);
786
+ get status(): ValueOfAppState;
787
+ /** 初始化沙盒 */
788
+ private initializeSandBox;
789
+ /** 设置容器属性 */
790
+ private setContainerAttribute;
791
+ /** 转移节点到新容器 */
792
+ private transferNodes;
793
+ /** 设置容器 */
794
+ private setupContainer;
795
+ /** 执行样式 */
796
+ private executeStyles;
797
+ /** 创建实例包装器 */
798
+ private createInstanceWrapper;
799
+ /** 渲染实例 */
800
+ private renderInstance;
801
+ /** 获取脚本信息 */
802
+ private getScriptInfo;
803
+ /** 检查是否需要重新加载 */
804
+ private needsReload;
331
805
  }
332
806
 
807
+ /**
808
+ * 根据配置模式加载相应的应用或模块实例
809
+ * @description 统一的加载入口,根据模式参数决定加载应用还是模块实例
810
+ * @param props - 基础模型配置参数
811
+ * @returns Promise<BaseModel> - 返回加载的模型实例
812
+ */
333
813
  declare function load(props: IBaseModelProps): Promise<BaseModel>;
334
- declare function loadApp(props: IAppModleProps): Promise<MicroAppModel>;
814
+ /**
815
+ * 加载微应用
816
+ * @description 加载或获取已存在的微应用实例,并启动应用
817
+ * @param props - 应用模型配置参数
818
+ * @returns Promise<MicroAppModel> - 返回微应用模型实例
819
+ */
820
+ declare function loadApp(props: IAppModelProps): Promise<MicroAppModel>;
821
+ /**
822
+ * 加载模块实例
823
+ * @description 加载或获取已存在的模块实例,支持状态监听和异步等待
824
+ * @param props - 模块实例配置参数
825
+ * @returns Promise<MicroInstanceModel> - 返回模块实例模型
826
+ */
335
827
  declare function loadInstance(props: IJsModelProps): Promise<MicroInstanceModel>;
336
828
 
829
+ /**
830
+ * 挂载应用到指定容器
831
+ * @description 将已加载的应用挂载到指定的 DOM 容器或 ShadowRoot 中
832
+ * @template T - 导出实例的类型
833
+ * @param appKey - 应用的唯一标识符
834
+ * @param container - 挂载容器,可以是 HTMLElement 或 ShadowRoot(可选)
835
+ * @param callback - 挂载完成后的回调函数(可选)
836
+ */
337
837
  declare function mount<T>(appKey: string, container?: HTMLElement | ShadowRoot, callback?: <M extends BaseModel>(instance: M, exportInstance?: T) => void): void;
338
838
 
839
+ /**
840
+ * 卸载并删除指定应用
841
+ * @description 从缓存中完全删除指定的应用实例,释放相关资源
842
+ * @param url - 应用的 URL 标识符,用于定位要删除的应用
843
+ */
339
844
  declare function unload(url: string): void;
340
845
 
846
+ /**
847
+ * 卸载指定应用
848
+ * @description 卸载指定的应用实例,并在没有活跃应用时重置全局方法
849
+ * @param appKey - 应用的唯一标识符
850
+ */
341
851
  declare function unmount(appKey: string): void;
342
852
 
343
853
  /**
344
- * @param options 加载模块的参数
345
- * @description 预加载实例
854
+ * 预加载模块实例
855
+ * @description 在浏览器空闲时预加载指定的模块实例,提前准备资源以提升后续加载性能
856
+ * @param options - 模块加载配置参数
857
+ * @param options.isPreLoad - 将被自动设置为 true,标识这是预加载操作
858
+ * @example
859
+ * ```typescript
860
+ * preLoadInstance({
861
+ * name: 'my-module',
862
+ * url: 'https://example.com/module.js'
863
+ * });
864
+ * ```
346
865
  */
347
866
  declare function preLoadInstance(options: IJsModelProps): void;
348
867
  /**
349
- * @param options 加载应用的参数
350
- * @description 预加载应用
868
+ * 预加载微应用
869
+ * @description 在浏览器空闲时预加载指定的微应用,提前准备应用资源以提升后续加载性能
870
+ * @param options - 应用加载配置参数
871
+ * @param options.isPreLoad - 将被自动设置为 true,标识这是预加载操作
872
+ * @example
873
+ * ```typescript
874
+ * preLoadApp({
875
+ * name: 'my-app',
876
+ * entry: 'https://example.com/app/'
877
+ * });
878
+ * ```
351
879
  */
352
- declare function preLoadApp(options: IAppModleProps): void;
880
+ declare function preLoadApp(options: IAppModelProps): void;
353
881
  /**
354
- * @param sourceList 要预加载的资源列表
355
- * @description 预加载资源
882
+ * 预加载全局资源
883
+ * @description 在浏览器空闲时预加载指定的全局资源列表,包括样式文件、脚本文件等
884
+ * @param sourceList - 要预加载的资源列表,可以是单个资源或资源数组
885
+ * @example
886
+ * ```typescript
887
+ * // 预加载单个资源
888
+ * preLoadSource('https://example.com/style.css');
889
+ *
890
+ * // 预加载多个资源
891
+ * preLoadSource([
892
+ * 'https://example.com/style.css',
893
+ * 'https://example.com/script.js'
894
+ * ]);
895
+ * ```
356
896
  */
357
897
  declare function preLoadSource(sourceList: SourceType): void;
358
898
 
359
899
  declare class WeWeb {
360
900
  fetchSource?: FetchSourceType;
361
- webcomponentTag: string;
901
+ webComponentTag: string;
362
902
  constructor();
903
+ /** 设置自定义DOM标签名 */
363
904
  setWebComponentTag(): void;
905
+ /** 启动WeWeb */
364
906
  start(option?: IStartOption): void;
365
907
  }
366
908
  declare const weWeb: WeWeb;