refactor(plugins): 重构插件自动加载逻辑
This commit is contained in:
@@ -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');
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user