Skip to content

Commit

Permalink
重构调整
Browse files Browse the repository at this point in the history
  • Loading branch information
mqhe2007 committed Jul 12, 2022
1 parent 62a8dc7 commit 5d7b7c4
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-module-loader",
"version": "3.1.0-bate1",
"version": "3.1.0-bate.2",
"description": "Let you use the micro front-end architecture to build Vue applications.",
"author": "mqhe2007 <[email protected]>",
"homepage": "https://mengqinghe.com",
Expand Down
40 changes: 20 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/create-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Context } from "./interfaces";
export function createContext(ctx: Context = {}): void {
// 创建全局VML上下文对象
window[Symbol.for("___VML_CONTEXT___")] = ctx;
}
6 changes: 3 additions & 3 deletions src/install.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { App } from "vue";
import { createContext } from "./create-Context";
import { Context } from "./interfaces";
export function install(app: App, ctx: Context) {
if (!app.version?.startsWith("3")) {
throw new Error(`[vue-module-loader]: 仅适用于vue3`);
} else {
// 创建全局VML上下文对象
window[Symbol.for("___VML_CONTEXT___")] = {
createContext({
app,
...ctx,
}
});
}
}
3 changes: 0 additions & 3 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { App } from "vue";
export interface Context {
// Vue应用实例
app: App;
[propName: string]: any;
}
export interface ModuleUninstallerMap {
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {install} from './install';
import { install } from "./install";
export { useModule } from "./use-module";
export { uninstall, clear, listUnistaller } from "./uninstaller";
export { useContext } from "./use-context";
export default install
export default install;
24 changes: 14 additions & 10 deletions src/use-module.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import { createContext } from "./create-Context";
import fireModule from "./fire-module";
import { ModuleOptions } from "./interfaces";
import { Context, ModuleOptions } from "./interfaces";
/**
* 使用模块
* @param moduleData 模块数据,可以是模块定义对象或者是模块资源url.
*/
async function useModule(moduleData: ModuleOptions): Promise<void>;
async function useModule(moduleData: string): Promise<void>;
async function useModule(moduleData: any): Promise<void> {
if (!window[Symbol.for("___VML_CONTEXT___")]) {
throw new Error("[vue-module-loader]: 请先使用use方法注册插件。");
async function useModule(
moduleData: ModuleOptions,
ctx: Context
): Promise<void>;
async function useModule(moduleData: string, ctx: Context): Promise<void>;
async function useModule(moduleData: any, ctx: Context): Promise<void> {
const existingContext = window[Symbol.for("___VML_CONTEXT___")];
if (!existingContext) {
createContext(ctx);
}
if (typeof moduleData === "object") {
return await fireModule(moduleData);
} else if (typeof moduleData === "string") {
if (!existingContext.Vue) throw new Error("[vue-module-loader]: 上下文对象缺少Vue对象");
const res = await fetch(moduleData);
const moduleString = await res.text();
const moduleCode = moduleString.replace("var", "return");
const moduleCode = moduleString.replace("const", "return");
const moduleStringFun = Function(`return function(vue){${moduleCode}}`)();
const moduleDataFromUrl = moduleStringFun(
window[Symbol.for("___VML_CONTEXT___")].Vue
);
const moduleDataFromUrl = moduleStringFun(existingContext.Vue);
return await fireModule(moduleDataFromUrl, moduleData.match(/\S*\//)[0]);
}
}
Expand Down

0 comments on commit 5d7b7c4

Please sign in to comment.