feat: 优化插件设置逻辑,支持自动安装模块并增强调试信息
This commit is contained in:
@ -3,4 +3,5 @@ import './styles';
|
|||||||
import App from './App.vue';
|
import App from './App.vue';
|
||||||
import { setupPlugins } from './plugins';
|
import { setupPlugins } from './plugins';
|
||||||
|
|
||||||
setupPlugins(createApp(App)).mount('#app');
|
const autoInstallModules = import.meta.glob('./plugins/*.ts', { eager: true });
|
||||||
|
setupPlugins(createApp(App), autoInstallModules).mount('#app');
|
||||||
|
@ -1,15 +1,18 @@
|
|||||||
// https://github.com/antfu-collective/vitesse/blob/47618e72dfba76c77b9b85b94784d739e35c492b/src/modules/README.md
|
// https://github.com/antfu-collective/vitesse/blob/47618e72dfba76c77b9b85b94784d739e35c492b/src/modules/README.md
|
||||||
type UserPluginContext = { app: import('vue').App<Element> };
|
type UserPluginContext = { app: import('vue').App<Element> };
|
||||||
type UserPlugin = (ctx: UserPluginContext) => void;
|
type UserPlugin = (ctx: UserPluginContext) => void;
|
||||||
|
type AutoInstallModule = { install?: UserPlugin; [K: string]: unknown };
|
||||||
|
|
||||||
export function setupPlugins(app: import('vue').App) {
|
export function setupPlugins(app: import('vue').App, modules: AutoInstallModule | Record<string, unknown>) {
|
||||||
console.group('Setup Plugins');
|
console.group('🔌 Plugins');
|
||||||
const modules = import.meta.glob<{ install: UserPlugin }>('./*.ts', { eager: true });
|
|
||||||
for (const path in modules) {
|
for (const path in modules) {
|
||||||
modules[path].install?.({ app });
|
const module = modules[path] as AutoInstallModule;
|
||||||
if (modules[path].install !== undefined) {
|
if (module.install) {
|
||||||
const moduleName = path.replace(/\.\/(.*)\.ts/, '$1');
|
module.install({ app });
|
||||||
console.debug(`%c✔ ${moduleName}`, 'color: #07a');
|
console.debug(`%c✔ ${path}`, 'color: #07a');
|
||||||
|
} else {
|
||||||
|
if (typeof module.setupPlugins === 'function') continue;
|
||||||
|
console.warn(`%c✘ ${path} has no install function`, 'color: #f50');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.groupEnd();
|
console.groupEnd();
|
||||||
|
Reference in New Issue
Block a user