Skip to content

Commit 5d7b7c4

Browse files
committed
重构调整
1 parent 62a8dc7 commit 5d7b7c4

File tree

7 files changed

+45
-39
lines changed

7 files changed

+45
-39
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.1.0-bate1",
3+
"version": "3.1.0-bate.2",
44
"description": "Let you use the micro front-end architecture to build Vue applications.",
55
"author": "mqhe2007 <122274389@qq.com>",
66
"homepage": "https://mengqinghe.com",

pnpm-lock.yaml

Lines changed: 20 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/create-context.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Context } from "./interfaces";
2+
export function createContext(ctx: Context = {}): void {
3+
// 创建全局VML上下文对象
4+
window[Symbol.for("___VML_CONTEXT___")] = ctx;
5+
}

src/install.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { App } from "vue";
2+
import { createContext } from "./create-Context";
23
import { Context } from "./interfaces";
34
export function install(app: App, ctx: Context) {
45
if (!app.version?.startsWith("3")) {
56
throw new Error(`[vue-module-loader]: 仅适用于vue3`);
67
} else {
7-
// 创建全局VML上下文对象
8-
window[Symbol.for("___VML_CONTEXT___")] = {
8+
createContext({
99
app,
1010
...ctx,
11-
}
11+
});
1212
}
1313
}

src/interfaces.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import { App } from "vue";
21
export interface Context {
3-
// Vue应用实例
4-
app: App;
52
[propName: string]: any;
63
}
74
export interface ModuleUninstallerMap {

src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {install} from './install';
1+
import { install } from "./install";
22
export { useModule } from "./use-module";
33
export { uninstall, clear, listUnistaller } from "./uninstaller";
44
export { useContext } from "./use-context";
5-
export default install
5+
export default install;

src/use-module.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1+
import { createContext } from "./create-Context";
12
import fireModule from "./fire-module";
2-
import { ModuleOptions } from "./interfaces";
3+
import { Context, ModuleOptions } from "./interfaces";
34
/**
45
* 使用模块
56
* @param moduleData 模块数据,可以是模块定义对象或者是模块资源url.
67
*/
7-
async function useModule(moduleData: ModuleOptions): Promise<void>;
8-
async function useModule(moduleData: string): Promise<void>;
9-
async function useModule(moduleData: any): Promise<void> {
10-
if (!window[Symbol.for("___VML_CONTEXT___")]) {
11-
throw new Error("[vue-module-loader]: 请先使用use方法注册插件。");
8+
async function useModule(
9+
moduleData: ModuleOptions,
10+
ctx: Context
11+
): Promise<void>;
12+
async function useModule(moduleData: string, ctx: Context): Promise<void>;
13+
async function useModule(moduleData: any, ctx: Context): Promise<void> {
14+
const existingContext = window[Symbol.for("___VML_CONTEXT___")];
15+
if (!existingContext) {
16+
createContext(ctx);
1217
}
1318
if (typeof moduleData === "object") {
1419
return await fireModule(moduleData);
1520
} else if (typeof moduleData === "string") {
21+
if (!existingContext.Vue) throw new Error("[vue-module-loader]: 上下文对象缺少Vue对象");
1622
const res = await fetch(moduleData);
1723
const moduleString = await res.text();
18-
const moduleCode = moduleString.replace("var", "return");
24+
const moduleCode = moduleString.replace("const", "return");
1925
const moduleStringFun = Function(`return function(vue){${moduleCode}}`)();
20-
const moduleDataFromUrl = moduleStringFun(
21-
window[Symbol.for("___VML_CONTEXT___")].Vue
22-
);
26+
const moduleDataFromUrl = moduleStringFun(existingContext.Vue);
2327
return await fireModule(moduleDataFromUrl, moduleData.match(/\S*\//)[0]);
2428
}
2529
}

0 commit comments

Comments
 (0)