feat: 重构插件安装方式,统一使用 install 方法并移除不必要的 setup 函数
All checks were successful
/ playwright (push) Successful in 2m19s
/ build-and-deploy-to-vercel (push) Successful in 1m9s
/ depcheck (push) Successful in 1m5s

This commit is contained in:
严浩
2024-12-24 23:13:09 +08:00
parent e9f5aa56a6
commit 9d421c5bfe
7 changed files with 39 additions and 40 deletions

View File

@ -1,35 +1,18 @@
import './assets/styles';
import { createHead } from '@unhead/vue';
import { setupEruda } from './plugins/eruda';
import { setupPinia } from './plugins/pinia';
import { setupPrimeVue } from './plugins/primevue';
import { setupRouter } from './plugins/router';
import { setupVueI18n } from './plugins/vue-i18n';
import App from './App.vue';
async function init() {
if ((import.meta.env.MODE === 'development' || 1 === 1) && 0) {
await setupEruda();
const app = createApp(App);
{
// https://github.com/antfu-collective/vitesse/blob/47618e72dfba76c77b9b85b94784d739e35c492b/src/modules/README.md
type UserPluginContext = { app: import('vue').App<Element> };
type UserPlugin = (ctx: UserPluginContext) => void;
Object.values(import.meta.glob<{ install: UserPlugin }>('./plugins/**.ts', { eager: true })).forEach((i) =>
i.install?.({ app }),
);
}
const app = createApp(App).use(createHead());
app.config.errorHandler = (error, instance, info) => {
console.error('Global error:', error);
console.error('Component:', instance);
console.error('Error Info:', info);
// 这里你可以:
// 1. 发送错误到日志服务
// 2. 显示全局错误提示
// 3. 进行错误分析和处理
};
setupPinia(app);
setupVueI18n(app);
setupPrimeVue(app);
setupRouter(app);
app.config.globalProperties.$__DEV__ = $__DEV__;
app.mount('#app');

14
src/plugins/_/_.ts Normal file
View File

@ -0,0 +1,14 @@
import { createHead } from '@unhead/vue';
export function install({ app }: { app: import('vue').App<Element> }) {
app.use(createHead());
app.config.errorHandler = (error, instance, info) => {
console.error('Global error:', error);
console.error('Component:', instance);
console.error('Error Info:', info);
// 这里你可以:
// 1. 发送错误到日志服务
// 2. 显示全局错误提示
// 3. 进行错误分析和处理
};
}

View File

@ -1,12 +1,14 @@
export async function setupEruda() {
// TODO: https://github.com/hu3dao/vite-plugin-debug/
// https://eruda.liriliri.io/zh/docs/#快速上手
await import('eruda').then(({ default: eruda }) => {
eruda.init({
defaults: {
transparency: 0.9,
},
export function install() {
if ((import.meta.env.MODE === 'development' || 1 === 1) && 0) {
// TODO: https://github.com/hu3dao/vite-plugin-debug/
// https://eruda.liriliri.io/zh/docs/#快速上手
import('eruda').then(({ default: eruda }) => {
eruda.init({
defaults: {
transparency: 0.9,
},
});
/* eruda.show(); */
});
// eruda.show();
});
}
}

View File

@ -1,7 +1,7 @@
import { PiniaColada } from '@pinia/colada';
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate';
export function setupPinia(app: import('vue').App) {
export function install({ app }: { app: import('vue').App<Element> }) {
app.use(createPinia().use(piniaPluginPersistedstate));
app.use(PiniaColada, {});
}

View File

@ -6,7 +6,7 @@ import Aura from '@primevue/themes/aura';
import zhCN from 'primelocale/zh-CN.json';
import PrimeVue from 'primevue/config';
export function setupPrimeVue(app: import('vue').App) {
export function install({ app }: { app: import('vue').App<Element> }) {
app.use(PrimeVue, {
locale: {
...zhCN['zh-CN'],

View File

@ -14,14 +14,14 @@ router.onError((error) => {
console.debug('🚨 [router error]: ', error);
});
function setupRouter(app: import('vue').App) {
export { router };
export function install({ app }: { app: import('vue').App<Element> }) {
app
// Register the plugin before the router
.use(DataLoaderPlugin, { router })
// adding the router will trigger the initial navigation
.use(router);
}
export { router, setupRouter };
// ========================================================================
// =========================== Router Guards ==============================
// ========================================================================

View File

@ -7,7 +7,7 @@ import { createI18n } from 'vue-i18n';
import messages from '@intlify/unplugin-vue-i18n/messages';
console.debug(`messages :>> `, messages);
export function setupVueI18n(app: import('vue').App) {
export function install({ app }: { app: import('vue').App<Element> }) {
app.use(
// https://vue-i18n.intlify.dev/guide/essentials/started.html#registering-the-i18n-plugin
createI18n({