Skip to content

Commit

Permalink
feat: auto module
Browse files Browse the repository at this point in the history
  • Loading branch information
ntnyq committed Apr 17, 2022
1 parent f59e107 commit bfe4c9a
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 26 deletions.
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<meta name="revisit-after" content="7 days" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="keywords" content="ntnyq">
<meta name="description" content="ntnyq's website">
<meta name="theme-color" content="#ffffff" />
<link rel="shortcut icon" href="/static/icons/favicon.png" type="image/png" />
<link href="https://fonts.googleapis.com/css2?family=Fira+Code&family=Inter:wght@200;400;600&display=swap" rel="stylesheet" />
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"vite-ssg-sitemap": "^0.2.2"
},
"dependencies": {
"@gtm-support/vue-gtm": "^1.4.0",
"@vueuse/core": "^8.2.6",
"@vueuse/head": "^0.7.6",
"nprogress": "^0.2.0",
Expand Down
20 changes: 20 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/components/ThemeToggle.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<template>
<a
<span
@click="toggleDark"
title="Toggle Color Scheme"
class="select-none"
class="select-none cursor-pointer"
>
<IRiMoonLine v-if="isDark" />
<IRiSunLine v-else />
</a>
</span>
</template>

<script setup lang="ts">
Expand Down
28 changes: 11 additions & 17 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import pages from '~pages'
import { ViteSSG } from 'vite-ssg'
import type { RouterScrollBehavior } from 'vue-router'
import App from './App.vue'

import '@unocss/reset/tailwind.css'
import 'uno.css'
import '@/styles/main.css'
import '@/styles/prose.css'
import '@/styles/markdown.css'
import pages from '~pages'
import { ViteSSG } from 'vite-ssg'
import NPprogress from 'nprogress'
import type { RouterScrollBehavior } from 'vue-router'
import App from './App.vue'

const routes = pages.map(route => ({
...route,
Expand All @@ -20,16 +20,10 @@ const scrollBehavior: RouterScrollBehavior = (_, __, savedPosition) =>

export const createApp = ViteSSG(
App,
{ routes, scrollBehavior },
({ router, isClient }) => {
if (isClient) {
router.beforeEach(() => {
NPprogress.start()
})

router.afterEach(() => {
NPprogress.done()
})
}
}
{ routes, scrollBehavior, base: import.meta.env.BASE_URL },
ctx => {
Object.values(import.meta.globEager(`./modules/*.ts`)).forEach(i =>
i.install?.(ctx),
)
},
)
6 changes: 6 additions & 0 deletions src/modules/gtm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createGtm } from '@gtm-support/vue-gtm'
import type { UserModule } from '@/types'

export const install: UserModule = ({ app }) => {
app.use(createGtm({ id: `GTM-T497H2P` }))
}
11 changes: 11 additions & 0 deletions src/modules/nprogress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import NProgress from 'nprogress'
import { type UserModule } from '@/types'

export const install: UserModule = ({ isClient, router }) => {
if (!isClient) return
router.beforeEach((to, from) => {
if (to.path === from.path) return
NProgress.start()
})
router.afterEach(() => NProgress.done())
}
8 changes: 8 additions & 0 deletions src/modules/pwa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { type UserModule } from '@/types'

export const install: UserModule = async ({ isClient, router }) => {
if (!isClient) return
await router.isReady()
const { registerSW } = await import(`virtual:pwa-register`)
registerSW({ immediate: true })
}
3 changes: 3 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { type ViteSSGContext } from 'vite-ssg'

export type UserModule = (ctx: ViteSSGContext) => void
7 changes: 1 addition & 6 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,7 @@ export default defineConfig({
presets: [
presetUno(),
presetIcons({
extraProperties: {
dispaly: `inline-block`,
height: `1.2em`,
width: `1.2em`,
'vertical-align': `text-bottom`,
},
warn: true,
}),
],
}),
Expand Down

0 comments on commit bfe4c9a

Please sign in to comment.