use glob to load plugins.

This commit is contained in:
Miroslav Šedivý 2024-03-17 13:10:50 +01:00
parent 6272e48b67
commit e0366905ad
2 changed files with 23 additions and 29 deletions

3
.gitignore vendored
View File

@ -31,3 +31,6 @@ coverage
test-results/ test-results/
playwright-report/ playwright-report/
/src/page/plugins/*
!/src/page/plugins/.gitkeep

View File

@ -320,22 +320,29 @@
<script lang="ts" setup> <script lang="ts" setup>
// plugins must be available at: // plugins must be available at:
// ./plugins/{name}/main-tabs.vue // ./plugins/{name}/index.ts -> { Components, Tabs }
// ./plugins/{name}/main-components.vue const plugins = import.meta.glob('./plugins/*/index.ts')
let plugins = [] as string[]
const pluginsTabs = shallowRef<Record<string, any>>({})
const pluginsComponents = shallowRef<Record<string, any>>({})
// dynamic plugins loader // dynamic plugins loader
//;(function (r: any) { onMounted(async () => {
// r.keys().forEach((key: string) => { const resolvedPlugins = await Promise.all(
// const found = key.match(/\.\/(.*?)\//) Object.entries(plugins).map(async ([path, component]) => {
// if (found) { return [path, await component()]
// plugins.push(found[1]) }),
// console.log('loading a plugin:', found[1]) ) as [string, { Components: any, Tabs: any }][]
// }
// })
//})(require.context('./plugins/', true, /(main-tabs|main-components)\.vue$/))
import { ref, computed, onMounted } from 'vue' pluginsTabs.value = {}
pluginsComponents.value = {}
for (const [path, { Components, Tabs }] of resolvedPlugins) {
pluginsTabs.value[path] = Tabs
pluginsComponents.value[path] = Components
}
})
import { ref, shallowRef, computed, onMounted } from 'vue'
import type { AxiosProgressEvent } from 'axios' import type { AxiosProgressEvent } from 'axios'
import NekoCanvas from '@/component/main.vue' import NekoCanvas from '@/component/main.vue'
@ -347,22 +354,6 @@ import NekoMembers from './components/members.vue'
import NekoMedia from './components/media.vue' import NekoMedia from './components/media.vue'
import NekoChat from './components/chat.vue' import NekoChat from './components/chat.vue'
const pluginsTabs = computed(() => {
let x = {} as Record<string, any>
for (let p of plugins) {
x[p] = () => import('./plugins/' + p + '/main-tabs.vue')
}
return x
})
const pluginsComponents = computed(() => {
let x = {} as Record<string, any>
for (let p of plugins) {
x[p] = () => import('./plugins/' + p + '/main-components.vue')
}
return x
})
const neko = ref<typeof NekoCanvas>() const neko = ref<typeof NekoCanvas>()
const expanded = ref(!window.matchMedia('(max-width: 600px)').matches) // default to expanded on bigger screens const expanded = ref(!window.matchMedia('(max-width: 600px)').matches) // default to expanded on bigger screens