Skip to content

Commit 5d8e2e8

Browse files
committed
rc
1 parent 8b8a677 commit 5d8e2e8

13 files changed

+12160
-1
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
.DS_Store
3+
dist/
4+
dist-ssr
5+
*.local

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"volar.tsPlugin": true
3+
}

index.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html lang="zh-cn">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<script src="https://unpkg.com/vue@next"></script>
8+
<title>develop</title>
9+
</head>
10+
<body>
11+
<script type="module">
12+
import vueModuleLoader from "/src/main.ts";
13+
const app = Vue.createApp({
14+
data() {
15+
return {
16+
msg: "hello",
17+
};
18+
},
19+
}).use(vueModuleLoader);
20+
app.config.globalProperties.$loader([
21+
"http://home.mengqinghe.com:5004/tmp/vue-module-loader-foo.js",
22+
]);
23+
</script>
24+
</body>
25+
</html>

package-lock.json

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

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.0.0",
3+
"version": "3.0.0-rc.1",
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/fire-module.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { App } from "@vue/runtime-core";
2+
import ModuleOptions from "./types/module-type";
3+
import { store } from "./uninstaller";
4+
5+
export default function (
6+
app: App,
7+
moduleOptions: ModuleOptions
8+
): Promise<void> {
9+
const installReturn = moduleOptions.install(app);
10+
if (!(installReturn instanceof Promise)) {
11+
store(moduleOptions.name, moduleOptions.uninstall);
12+
return Promise.resolve();
13+
}
14+
return installReturn.then(function () {
15+
store(moduleOptions.name, moduleOptions.uninstall);
16+
});
17+
}

src/loader.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { App } from "vue";
2+
import fireModule from "./fire-module";
3+
import ModuleType from "./types/module-type";
4+
5+
export default function (app: App) {
6+
function loader(moduleOptions: ModuleType | string[]): Promise<void> {
7+
if ("install" in moduleOptions) {
8+
return fireModule(app, moduleOptions);
9+
} else {
10+
return new Promise((resolve, reject) => {
11+
let promises: Promise<void>[] = [];
12+
moduleOptions.forEach(moduleUrl => {
13+
import(/* @vite-ignore */ moduleUrl).then(module => {
14+
promises.push(fireModule(app, module.default));
15+
});
16+
});
17+
Promise.allSettled(promises).then(() => resolve());
18+
});
19+
}
20+
}
21+
return loader;
22+
}

src/main.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { App } from "vue";
2+
import loader from "./loader";
3+
import { clear, uninstall } from "./uninstaller";
4+
export default function (app: App, options = {}) {
5+
app.config.globalProperties.$loader = {
6+
install: loader(app),
7+
uninstall: uninstall(app),
8+
clear: clear(app),
9+
};
10+
}

src/types/module-type.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { App } from "@vue/runtime-core";
2+
3+
export default interface ModuleOptions {
4+
name: string;
5+
install: (app: App) => any;
6+
uninstall: (app: App) => void;
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { App } from "@vue/runtime-core";
2+
3+
export default interface IModuleUninstallerMap {
4+
[propName: string]: (app: App) => void;
5+
}

src/uninstaller.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 模块卸载器
3+
* 当模块安装时会缓存卸载程序。
4+
*/
5+
import { App } from "@vue/runtime-core";
6+
import IModuleUninstallerMap from "./types/module-uninstaller-map-type";
7+
8+
const moduleUninstallerMap: IModuleUninstallerMap = {};
9+
10+
export function store(moduleName: string, uninstaller: (app: App) => void) {
11+
moduleUninstallerMap[moduleName] = uninstaller;
12+
}
13+
export function uninstall(app: App) {
14+
return function(moduleName: string) {
15+
moduleUninstallerMap[moduleName](app);
16+
};
17+
}
18+
export function clear(app: App) {
19+
return function() {
20+
for (let moduleName in moduleUninstallerMap) {
21+
moduleUninstallerMap[moduleName](app);
22+
}
23+
};
24+
}

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"module": "esnext",
5+
"moduleResolution": "node",
6+
"strict": true,
7+
"jsx": "preserve",
8+
"sourceMap": true,
9+
"resolveJsonModule": true,
10+
"esModuleInterop": true,
11+
"lib": ["esnext", "dom"],
12+
"types": ["vite/client"]
13+
},
14+
"include": ["src/**/*.ts", "src/**/*.d.ts"]
15+
}

vite.config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { resolve } from "path";
2+
import { defineConfig } from "vite";
3+
4+
export default defineConfig({
5+
build: {
6+
lib: {
7+
entry: resolve(__dirname, "src/main.ts"),
8+
name: "vueModuleLoader",
9+
},
10+
},
11+
});

0 commit comments

Comments
 (0)