-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathboot-module.ts
42 lines (41 loc) · 1.3 KB
/
boot-module.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
import { Context, ModuleOptions } from "./interfaces";
import uninstallCache from "./uninstaller";
function loadStyle(moduleData, moduleHostUrl): Promise<void> {
return new Promise((resolve, reject) => {
const style = document.createElement("link");
style.id = moduleData.name;
style.rel = "stylesheet";
style.crossOrigin = "anonymous";
style.href = moduleHostUrl + "style.css";
style.onload = () => {
resolve();
};
style.onerror = () => {
resolve();
};
document.head.append(style);
});
}
export default async function(
moduleData: ModuleOptions,
moduleHostUrl?: string
): Promise<void> {
let installReturn;
try {
console.log(`[vue-module-loader]: 模块「${moduleData.name}」开始加载...`);
if (moduleHostUrl) {
await loadStyle(moduleData, moduleHostUrl);
}
installReturn = await moduleData.install(window[Symbol.for("___VML_CONTEXT___")]);
console.log(`[vue-module-loader]: 模块「${moduleData.name}」加载完成。`);
// 缓存模块卸载方法,记录已加载模块清单
uninstallCache(moduleData.name, moduleData.uninstall);
} catch (error) {
console.error(
`[vue-module-loader]: 模块「${moduleData.name}」加载错误!`,
error
);
installReturn = error;
}
return installReturn;
}