refactor: 修改全局loading消失时机

This commit is contained in:
Hooray Hu 2024-09-09 10:19:38 +08:00
parent 76c66f9bc9
commit a5b3c5660d
4 changed files with 50 additions and 28 deletions

View File

@ -111,7 +111,7 @@ const enableAppSetting = import.meta.env.VITE_APP_SETTING === 'true'
<Topbar />
<div class="main">
<RouterView v-slot="{ Component, route }">
<Transition name="slide-right" mode="out-in" appear>
<Transition name="slide-right" mode="out-in">
<KeepAlive :include="keepAliveStore.list">
<component :is="Component" v-show="!isLink" :key="route.fullPath" />
</KeepAlive>

View File

@ -1,4 +1,5 @@
import { createRouter, createWebHashHistory } from 'vue-router'
import { loadingFadeOut } from 'virtual:app-loading'
import setupGuards from './guards'
// 路由相关数据
@ -13,4 +14,8 @@ const router = createRouter({
setupGuards(router)
router.isReady().then(() => {
loadingFadeOut()
})
export default router

View File

@ -3,6 +3,13 @@ declare interface Window {
mozDevicePixelRatio: any
}
declare module 'virtual:app-loading' {
const loadingFadeOut: () => void
export {
loadingFadeOut,
}
}
declare const __SYSTEM_INFO__: {
pkg: {
version: Recordable<string>

View File

@ -124,34 +124,44 @@ export default function createVitePlugins(viteEnv, isBuild = false) {
}
})(),
{
name: 'vite-plugin-loading',
enforce: 'pre',
transformIndexHtml: {
handler: async html => html.replace(/<\/body>/, `${
`<div data-app-loading>${await fs.readFileSync(path.resolve(process.cwd(), 'loading.html'), 'utf8')}</div>`
}</body>`),
order: 'pre',
},
transform: (code, id) => {
if (/src\/main.ts$/.test(id)) {
code = code.concat(`
const loadingEl = document.querySelector('[data-app-loading]')
if (loadingEl) {
loadingEl.style['pointer-events'] = 'none'
loadingEl.style.visibility = 'hidden'
loadingEl.style.opacity = 0
loadingEl.style.transition = 'all 0.5s ease-out'
loadingEl.addEventListener('transitionend', () => loadingEl.remove(), { once: true })
}
`)
return {
code,
map: null,
(function () {
const virtualModuleId = 'virtual:app-loading'
const resolvedVirtualModuleId = `\0${virtualModuleId}`
return {
name: 'vite-plugin-loading',
resolveId(id) {
if (id === virtualModuleId) {
return resolvedVirtualModuleId
}
}
},
},
},
load(id) {
if (id === resolvedVirtualModuleId) {
return {
code: `
export function loadingFadeOut() {
const loadingEl = document.querySelector('[data-app-loading]')
if (loadingEl) {
loadingEl.style['pointer-events'] = 'none'
loadingEl.style.visibility = 'hidden'
loadingEl.style.opacity = 0
loadingEl.style.transition = 'all 0.5s ease-out'
loadingEl.addEventListener('transitionend', () => loadingEl.remove(), { once: true })
}
}
`,
map: null,
}
}
},
enforce: 'pre',
transformIndexHtml: {
handler: async html => html.replace(/<\/body>/, `${
`<div data-app-loading>${await fs.readFileSync(path.resolve(process.cwd(), 'loading.html'), 'utf8')}</div>`
}</body>`),
order: 'pre',
},
}
})(),
// https://github.com/unplugin/unplugin-turbo-console
TurboConsole(),