-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.ts
133 lines (89 loc) · 2.67 KB
/
bootstrap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// registerMicroApps 注册子应用
import { Config } from "./src/config";
import { TranformatHTML } from "./src/helpers/tranformatHtml";
import { AppsModel, RegisterMicroApps, SafeWindow } from "./src/model";
const apps = [
{
name: "autosheets",
entry: "//localhost:3000",
container: "#subapp",
activeRule: "/subapp/autosheets"
}
]
export class QiankunStarter extends Config implements RegisterMicroApps {
constructor(apps: AppsModel[]) {
super(apps);
}
registerMicroApps() {
}
start() {
this.watchURL();
const app = this.findAppByPathKey().app;
if (!app) {
return void console.log('未找到');
}
}
watchURL() {
// go back forward
window.addEventListener("popstate", () => {
})
// pushstate replaceState需要函数重写劫持
const oldPushState = window.history.pushState;
const oldreplaceState = window.history.replaceState;
window.history.pushState = function (...arg) {
oldPushState.apply(window.history, arg)
}
window.history.replaceState = function (...arg) {
oldreplaceState.apply(window.history, arg)
}
}
/**
* 解析子应用资源 ( css ,js, html )
* @param app
*/
async handleActiveAppSource(app: AppsModel): Promise<void> {
const { entry, container } = app;
const html = await QiankunStarter.fetchSroucce(entry);
// 加载子应用
// 直接插入 html内 不会生效 ( 浏览器安全策略 )
const appInstace = new TranformatHTML(html, entry);
// 先将子节点添加到容器中
const TempContainer = document.querySelector(container);
TempContainer?.appendChild(appInstace.template as unknown as HTMLDivElement)
this.setGloabalEnv();
// 执行所有的js资源
const appExports = await appInstace.execScripts();
// 调用子应用的生命周期
const didmount = {
bootstrap: appExports?.bootstrap,
mount: appExports?.mount,
unmount: appExports?.unmount
}
const updateAppInfo = this.update(didmount);
if (!updateAppInfo) {
return;
}
await updateAppInfo.bootstrap?.();
await updateAppInfo.mount?.({
...updateAppInfo,
container: document.querySelector(updateAppInfo.container) as HTMLDivElement,
});
await updateAppInfo.unmount?.();
}
/**
* 设置微应用全局标记
*/
setGloabalEnv() {
SafeWindow._POWERED_BY_QIANKUN_ = true;
}
static async fetchSroucce(entry: string): Promise<string> {
const res = await fetch(entry);
return await res.text();
}
/**
* 匹配子应用
*/
handleRoute(app: AppsModel) {
}
}
const qiankunStarter = new QiankunStarter(apps);