refactor(plugins): 重构插件自动加载逻辑
Some checks failed
CI/CD Pipeline / playwright (push) Failing after 7m17s
CI/CD Pipeline / build-and-deploy (push) Has been skipped

This commit is contained in:
严浩
2025-11-03 11:52:54 +08:00
parent 8a3b9e03fd
commit b669889bb0
2 changed files with 11 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
import './styles/index.ts';
import { LogLevels } from 'consola';
import App from './App.vue';
import { setupPlugins } from './plugins';
@@ -6,11 +7,8 @@ import { router } from './plugins/00.router-plugin.ts';
consola.level = LogLevels.verbose;
const autoInstallModules = import.meta.glob('./plugins/!(index).ts', {
eager: true /* true 为同步false 为异步 */,
});
const app = setupPlugins(createApp(App), autoInstallModules);
const app = createApp(App);
setupPlugins(app);
await router.isReady();
await new Promise((resolve) => setTimeout(resolve, 280));
app.mount('#app');

View File

@@ -4,13 +4,15 @@
type UserPlugin = (ctx: UserPluginContext) => void;
type AutoInstallModule = { [K: string]: unknown; install?: UserPlugin };
type UserPluginContext = { app: import('vue').App<Element> };
export function setupPlugins(
app: import('vue').App,
modules: AutoInstallModule | Record<string, unknown>,
) {
const autoInstallModules: AutoInstallModule = import.meta.glob('./!(index).ts', {
eager: true /* true 为同步false 为异步 */,
});
export function setupPlugins(app: import('vue').App) {
console.group('🔌 Plugins');
for (const path in modules) {
const module = modules[path] as AutoInstallModule;
for (const path in autoInstallModules) {
const module = autoInstallModules[path] as AutoInstallModule;
if (module.install) {
module.install({ app });
console.debug(`%c✔ ${path}`, 'color: #07a');