Skip to content

Commit d133a08

Browse files
committed
3.5.3
1 parent ce9b9c5 commit d133a08

File tree

6 files changed

+32
-19
lines changed

6 files changed

+32
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-module-loader",
3-
"version": "3.5.1",
3+
"version": "3.5.3",
44
"description": "Let you use the micro front-end architecture to build Vue applications.",
55
"author": "mqhe2007 <[email protected]>",
66
"homepage": "https://mengqinghe.com",

src/boot-module.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ export default async function(
2020
moduleData: ModuleOptions,
2121
moduleHostUrl?: string
2222
): Promise<void> {
23-
const context: Context = window[Symbol.for("___VML_CONTEXT___")];
2423
let installReturn;
2524
try {
2625
console.log(`[vue-module-loader]: 模块「${moduleData.name}」开始加载...`);
2726
if (moduleHostUrl) {
2827
await loadStyle(moduleData, moduleHostUrl);
2928
}
30-
installReturn = await moduleData.install(context);
29+
installReturn = await moduleData.install(window[Symbol.for("___VML_CONTEXT___")]);
3130
console.log(`[vue-module-loader]: 模块「${moduleData.name}」加载完成。`);
3231
// 缓存模块卸载方法,记录已加载模块清单
3332
uninstallCache(moduleData.name, moduleData.uninstall);

src/create-context.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { Context } from "./interfaces";
22
export function createContext(ctx: Context = {}): Context {
3-
// 创建全局VML上下文对象
4-
window[Symbol.for("___VML_CONTEXT___")] = ctx;
5-
return window[Symbol.for("___VML_CONTEXT___")]
3+
if (!window[Symbol.for("___VML_CONTEXT___")]) {
4+
// 创建上下文对象
5+
window[Symbol.for("___VML_CONTEXT___")] = ctx;
6+
} else {
7+
// 合并上下文对象
8+
window[Symbol.for("___VML_CONTEXT___")] = Object.assign(
9+
window[Symbol.for("___VML_CONTEXT___")],
10+
ctx
11+
);
12+
}
13+
return window[Symbol.for("___VML_CONTEXT___")];
614
}

src/interfaces.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
import { App, CreateAppFunction } from "vue";
2+
13
export interface Context {
4+
app?: App;
5+
createApp?: CreateAppFunction<Element>;
6+
http?: any;
7+
moduleExpose?: {
8+
[propName: string]: any;
9+
};
210
[propName: string]: any;
311
}
412
export interface ModuleUninstallerMap {

src/use-context.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,20 @@ import { createContext } from "./create-context";
66
* @returns 根据key获取的上下文对象中的值
77
*/
88
function useContext(key: string, instance: any): any {
9-
if (!window[Symbol.for("___VML_CONTEXT___")]) {
10-
createContext();
11-
}
9+
const context = createContext();
1210
if (instance) {
13-
window[Symbol.for("___VML_CONTEXT___")][key] = instance;
11+
context[key] = instance;
1412
}
1513
if (!key) {
16-
return window[Symbol.for("___VML_CONTEXT___")];
14+
return context;
1715
} else {
18-
if (window[Symbol.for("___VML_CONTEXT___")][key] === undefined) {
19-
window[Symbol.for("___VML_CONTEXT___")][key] = null;
16+
if (context[key] === undefined) {
17+
context[key] = null;
2018
console.warn(
2119
`[vue-module-loader]:上下文中不存在“${key}”,既然你要用,我先帮你占个坑(初始化为null)。`
2220
);
2321
}
24-
return window[Symbol.for("___VML_CONTEXT___")][key];
22+
return context[key];
2523
}
2624
}
2725
export { useContext };

src/use-module.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ async function useModule(
1212
): Promise<void>;
1313
async function useModule(moduleData: string, ctx?: Context): Promise<void>;
1414
async function useModule(moduleData: any, ctx?: Context): Promise<void> {
15-
const existingContext =
16-
window[Symbol.for("___VML_CONTEXT___")] || createContext(ctx);
15+
const context = createContext(ctx);
16+
// console.log('vml-debug2:', context);
1717
let bootResult;
1818
if (typeof moduleData === "object") {
1919
bootResult = await bootModule(moduleData);
2020
} else if (typeof moduleData === "string") {
21-
if (!existingContext.Vue)
21+
if (!context.Vue)
2222
throw new Error("[vue-module-loader]: 上下文对象缺少Vue对象");
2323
const res = await fetch(moduleData);
2424
const moduleString = await res.text();
@@ -27,13 +27,13 @@ async function useModule(moduleData: any, ctx?: Context): Promise<void> {
2727
"return function"
2828
);
2929
const moduleStringFun = Function(`return function(vue){${moduleCode}}`)();
30-
const moduleDataFromUrl = moduleStringFun(existingContext.Vue);
30+
const moduleDataFromUrl = moduleStringFun(context.Vue);
3131
bootResult = await bootModule(
3232
moduleDataFromUrl,
3333
moduleData.match(/\S*\//)[0]
3434
);
3535
}
36-
return bootResult
36+
return bootResult;
3737
}
3838

3939
export { useModule };

0 commit comments

Comments
 (0)