Skip to content

Commit 306494f

Browse files
committed
refactor: 使用 vite-plugin-vue-app-loading 插件代替框架首屏loading
1 parent 575fcce commit 306494f

File tree

5 files changed

+17
-28
lines changed

5 files changed

+17
-28
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"vite-plugin-compression2": "^1.2.0",
9494
"vite-plugin-fake-server": "^2.1.1",
9595
"vite-plugin-svg-icons": "^2.0.1",
96+
"vite-plugin-vue-app-loading": "^0.2.1",
9697
"vue-tsc": "^2.1.4"
9798
},
9899
"simple-git-hooks": {

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/router/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createRouter, createWebHashHistory } from 'vue-router'
2+
import { loadingFadeOut } from 'virtual:app-loading'
23
import useSettingsStore from '@/store/modules/settings'
34
import useUserStore from '@/store/modules/user'
45
import useMenuStore from '@/store/modules/menu'
@@ -89,4 +90,8 @@ router.beforeEach((to, from, next) => {
8990
}
9091
})
9192

93+
router.isReady().then(() => {
94+
loadingFadeOut()
95+
})
96+
9297
export default router

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"resolveJsonModule": true,
2323
"types": [
2424
"vite/client",
25+
"vite-plugin-vue-app-loading/client",
2526
"element-plus/global"
2627
],
2728
"allowImportingTsExtensions": true,

vite/plugins.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
1212
import { vitePluginFakeServer } from 'vite-plugin-fake-server'
1313
import { compression } from 'vite-plugin-compression2'
1414
import archiver from 'archiver'
15+
import AppLoading from 'vite-plugin-vue-app-loading'
1516
import TurboConsole from 'unplugin-turbo-console/vite'
1617
import banner from 'vite-plugin-banner'
1718
import boxen from 'boxen'
@@ -96,34 +97,7 @@ export default function createVitePlugins(viteEnv, isBuild = false) {
9697
}
9798
})(),
9899

99-
{
100-
name: 'vite-plugin-loading',
101-
enforce: 'pre',
102-
transformIndexHtml: {
103-
handler: async html => html.replace(/<\/body>/, `${
104-
`<div data-app-loading>${await fs.readFileSync(path.resolve(process.cwd(), 'loading.html'), 'utf8')}</div>`
105-
}</body>`),
106-
order: 'pre',
107-
},
108-
transform: (code, id) => {
109-
if (/src\/main.ts$/.test(id)) {
110-
code = code.concat(`
111-
const loadingEl = document.querySelector('[data-app-loading]')
112-
if (loadingEl) {
113-
loadingEl.style['pointer-events'] = 'none'
114-
loadingEl.style.visibility = 'hidden'
115-
loadingEl.style.opacity = 0
116-
loadingEl.style.transition = 'all 0.5s ease-out'
117-
loadingEl.addEventListener('transitionend', () => loadingEl.remove(), { once: true })
118-
}
119-
`)
120-
return {
121-
code,
122-
map: null,
123-
}
124-
}
125-
},
126-
},
100+
AppLoading('loading.html'),
127101

128102
// https://github.com/unplugin/unplugin-turbo-console
129103
TurboConsole(),

0 commit comments

Comments
 (0)