refactor(plugins): 重构插件自动加载逻辑
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import './styles/index.ts';
|
import './styles/index.ts';
|
||||||
|
|
||||||
import { LogLevels } from 'consola';
|
import { LogLevels } from 'consola';
|
||||||
import App from './App.vue';
|
import App from './App.vue';
|
||||||
import { setupPlugins } from './plugins';
|
import { setupPlugins } from './plugins';
|
||||||
@@ -6,11 +7,8 @@ import { router } from './plugins/00.router-plugin.ts';
|
|||||||
|
|
||||||
consola.level = LogLevels.verbose;
|
consola.level = LogLevels.verbose;
|
||||||
|
|
||||||
const autoInstallModules = import.meta.glob('./plugins/!(index).ts', {
|
const app = createApp(App);
|
||||||
eager: true /* true 为同步,false 为异步 */,
|
setupPlugins(app);
|
||||||
});
|
|
||||||
|
|
||||||
const app = setupPlugins(createApp(App), autoInstallModules);
|
|
||||||
await router.isReady();
|
await router.isReady();
|
||||||
await new Promise((resolve) => setTimeout(resolve, 280));
|
await new Promise((resolve) => setTimeout(resolve, 280));
|
||||||
app.mount('#app');
|
app.mount('#app');
|
||||||
|
|||||||
@@ -4,13 +4,15 @@
|
|||||||
type UserPlugin = (ctx: UserPluginContext) => void;
|
type UserPlugin = (ctx: UserPluginContext) => void;
|
||||||
type AutoInstallModule = { [K: string]: unknown; install?: UserPlugin };
|
type AutoInstallModule = { [K: string]: unknown; install?: UserPlugin };
|
||||||
type UserPluginContext = { app: import('vue').App<Element> };
|
type UserPluginContext = { app: import('vue').App<Element> };
|
||||||
export function setupPlugins(
|
|
||||||
app: import('vue').App,
|
const autoInstallModules: AutoInstallModule = import.meta.glob('./!(index).ts', {
|
||||||
modules: AutoInstallModule | Record<string, unknown>,
|
eager: true /* true 为同步,false 为异步 */,
|
||||||
) {
|
});
|
||||||
|
|
||||||
|
export function setupPlugins(app: import('vue').App) {
|
||||||
console.group('🔌 Plugins');
|
console.group('🔌 Plugins');
|
||||||
for (const path in modules) {
|
for (const path in autoInstallModules) {
|
||||||
const module = modules[path] as AutoInstallModule;
|
const module = autoInstallModules[path] as AutoInstallModule;
|
||||||
if (module.install) {
|
if (module.install) {
|
||||||
module.install({ app });
|
module.install({ app });
|
||||||
console.debug(`%c✔ ${path}`, 'color: #07a');
|
console.debug(`%c✔ ${path}`, 'color: #07a');
|
||||||
|
|||||||
Reference in New Issue
Block a user