Skip to content

Commit

Permalink
修改全局loading消失时机
Browse files Browse the repository at this point in the history
  • Loading branch information
hooray committed Sep 9, 2024
1 parent 5c840a6 commit 97a97d8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/layouts/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,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>
Expand Down
5 changes: 5 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createRouter, createWebHashHistory } from 'vue-router'
import { loadingFadeOut } from 'virtual:app-loading'
import setupGuards from './guards'

// 路由相关数据
Expand All @@ -13,4 +14,8 @@ const router = createRouter({

setupGuards(router)

router.isReady().then(() => {
loadingFadeOut()
})

export default router
7 changes: 7 additions & 0 deletions src/types/shims.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ declare interface Window {
mozDevicePixelRatio: any
}

declare module 'virtual:app-loading' {
const loadingFadeOut: () => void
export {
loadingFadeOut,
}
}

declare const __SYSTEM_INFO__: {
pkg: {
dependencies: Recordable<string>
Expand Down
62 changes: 36 additions & 26 deletions vite/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
(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,
}
`)
return {
code,
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(),
Expand Down

0 comments on commit 97a97d8

Please sign in to comment.