diff --git a/src/main.ts b/src/main.ts index 73630a9..ef86b25 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,22 +1,14 @@ import './styles/main.postcss' -// import routes generated by Voie import routes from 'vite-plugin-pages/client' -// progress bar -import NProgress from 'nprogress' import { ViteSSG } from 'vite-ssg' -import installPlugins from './plugins' import App from './App.vue' // https://github.com/antfu/vite-ssg export const createApp = ViteSSG( App, { routes }, - ({ app, router, isClient }) => { - installPlugins(app) - - if (isClient) { - router.beforeEach(() => { NProgress.start() }) - router.afterEach(() => { NProgress.done() }) - } + (ctx) => { + // install all modules under `modules/` + Object.values(import.meta.globEager('./modules/*.ts')).map(i => i.install?.(ctx)) }, ) diff --git a/src/modules/README.md b/src/modules/README.md new file mode 100644 index 0000000..2176f99 --- /dev/null +++ b/src/modules/README.md @@ -0,0 +1,11 @@ +## Modules + +A custom user module system. Place a `.ts` file with the following template, it will be installed automatically. + +```ts +import { UserModule } from '/~/types' + +export const install: UserModule = ({ app, router, isClient }) => { + // do something +} +``` diff --git a/src/plugins/i18n.ts b/src/modules/i18n.ts similarity index 82% rename from src/plugins/i18n.ts rename to src/modules/i18n.ts index de1c00b..7b60763 100644 --- a/src/plugins/i18n.ts +++ b/src/modules/i18n.ts @@ -1,5 +1,5 @@ -import { App } from 'vue' import { createI18n } from 'vue-i18n' +import { UserModule } from '/~/types' // import i18n resources // https://vitejs.dev/guide/features.html#glob-import @@ -11,7 +11,7 @@ const messages = Object.fromEntries( }), ) -export default (app: App) => { +export const install: UserModule = ({ app }) => { const i18n = createI18n({ legacy: false, locale: 'en', diff --git a/src/modules/nprogress.ts b/src/modules/nprogress.ts new file mode 100644 index 0000000..77d3a86 --- /dev/null +++ b/src/modules/nprogress.ts @@ -0,0 +1,9 @@ +import NProgress from 'nprogress' +import { UserModule } from '/~/types' + +export const install: UserModule = ({ isClient, router }) => { + if (isClient) { + router.beforeEach(() => { NProgress.start() }) + router.afterEach(() => { NProgress.done() }) + } +} diff --git a/src/pages/README.md b/src/pages/README.md index 2a3ab17..e3504dd 100644 --- a/src/pages/README.md +++ b/src/pages/README.md @@ -5,7 +5,7 @@ Check out [`vite-plugin-pages`](https://github.com/hannoeru/vite-plugin-pages) f ### Path Aliasing -You can use `/~/` aliasing to `./src/` folder. +`/~/` is aliased to `./src/` folder. For example, instead of having diff --git a/src/plugins/index.ts b/src/plugins/index.ts deleted file mode 100644 index 0acee62..0000000 --- a/src/plugins/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { App } from 'vue' -import installI18n from './i18n' - -export default (app: App) => { - installI18n(app) -} diff --git a/src/vue-shim.d.ts b/src/shims-vue.d.ts similarity index 100% rename from src/vue-shim.d.ts rename to src/shims-vue.d.ts diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..045d456 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,3 @@ +import { ViteSSGContext } from 'vite-ssg' + +export type UserModule = (ctx: ViteSSGContext) => void